summaryrefslogtreecommitdiffstats
path: root/webd/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'webd/src/main.rs')
-rw-r--r--webd/src/main.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/webd/src/main.rs b/webd/src/main.rs
new file mode 100644
index 0000000..c2cf0ff
--- /dev/null
+++ b/webd/src/main.rs
@@ -0,0 +1,61 @@
1//! Browser gateway for zagan-bet.
2//!
3//! nginx terminates TLS, serves the static app and proxies `/ws` here. This
4//! process owns one upstream TLS connection and one `ClientCore` per browser
5//! session, so the browser never speaks the game wire protocol and never sees
6//! a certificate.
7
8mod config;
9mod session;
10
11use std::net::TcpListener;
12use std::sync::Arc;
13use std::thread;
14
15use config::Config;
16
17fn main() {
18 let config = match Config::from_env() {
19 Ok(config) => config,
20 Err(error) => {
21 eprintln!("webd: {error}");
22 std::process::exit(2);
23 }
24 };
25
26 let listener = match TcpListener::bind(&config.bind) {
27 Ok(listener) => listener,
28 Err(error) => {
29 eprintln!("webd: could not bind {}: {error}", config.bind);
30 std::process::exit(1);
31 }
32 };
33
34 println!(
35 "webd listening on ws://{} for origin {}; upstream {} pinned to {}; grace {}s",
36 config.bind,
37 config.origin,
38 config.upstream,
39 config.fingerprint,
40 config.grace.as_secs()
41 );
42
43 let registry = session::new_registry();
44 for stream in listener.incoming() {
45 let stream = match stream {
46 Ok(stream) => stream,
47 Err(error) => {
48 eprintln!("webd: accept failed: {error}");
49 continue;
50 }
51 };
52 let _ = stream.set_nodelay(true);
53 let config = config.clone();
54 let registry = Arc::clone(&registry);
55 thread::spawn(move || {
56 if let Err(error) = session::accept(stream, config, registry) {
57 eprintln!("webd: {error}");
58 }
59 });
60 }
61}