use std::net::SocketAddr; use bus::Bus; mod util; mod app; mod system; mod protocol; mod manager; use app::App; use hyper::body::Bytes; use hyper_util::rt::TokioIo; use system::{cache::WebCache, net::Stream}; #[derive(Clone)] struct HttpServiceArgs { bus:Bus, cache:WebCache, } async fn service_http(mut request:hyper::Request, args:HttpServiceArgs) -> Result>, std::convert::Infallible> // Serve cached files and upgrade websocket connections. // { use hyper::{Response, body::Bytes, header::{CONTENT_TYPE, CACHE_CONTROL}}; use http_body_util::Full; println!("Serving: {}", request.uri().path()); if hyper_tungstenite::is_upgrade_request(&request) { if let Ok((response, websocket)) = hyper_tungstenite::upgrade(&mut request, None) { tokio::task::spawn(async move { match websocket.await { Ok(websocket) => manager::handle_ws(websocket, args).await, Err(_) => Err(()), }.ok() }); Ok(response) } else { Ok(Response::builder() .status(401) .body(Full::new(Bytes::new())) .unwrap()) } } else { match request.uri().path() { "/" => Ok(Response::builder() .header(CONTENT_TYPE, "text/html") .header(CACHE_CONTROL, "no-cache") .body(Full::new(Bytes::from(args.cache.fetch("/.html").unwrap().data))).unwrap()), _ => match args.cache.fetch(request.uri().path()) { Some(data) => Ok(Response::builder() .header(CONTENT_TYPE, &data.mime) .header(CACHE_CONTROL, "no-cache") .body(Full::new(Bytes::from(data.data))).unwrap()), None => Ok(Response::builder() .status(404) .body(Full::new(Bytes::new())).unwrap()) } } } } async fn handle_http(stream:system::net::tls::TlsStream, addr:SocketAddr, args:HttpServiceArgs) -> Result<(),()> // Hand off socket connection to Hyper server. // { use hyper::server::conn::http1; use hyper::service::service_fn; println!("Connection from {}", addr.to_string()); let io = TokioIo::new(stream.to_stream()); let conn = http1::Builder::new() .serve_connection(io, service_fn(move |req| { service_http(req, args.clone()) })); conn.with_upgrades().await.ok(); Ok(()) } #[tokio::main(flavor = "multi_thread", worker_threads = 12)] async fn main() { use system::net::{ Server, tls::*, }; // Initialize application data. let app; if let Ok(result) = App::init() { app = result; } else { println!("fatal: failed to initialize server."); return; } // Initialize central bus and data serivce. let b_main :Bus = Bus::new_as(bus::Mode::Transmitter); match b_main.connect() { Ok(bus) => { tokio::spawn(async move { manager::thread_system(app, bus).await; }); } Err(_) => { println!("fatal: failed to initialize bus."); return; } } // Initialize HTTPS service. match b_main.connect() { Ok(bus) => { let cache = WebCache::new(); cache.cache_file("text/html", "/.html", "www/.html").ok(); cache.cache_whitespace_minimize("/.html").ok(); cache.cache_file_group("text/css", "/.css", &[ "www/css/main.css", "www/css/util.css", "www/css/ui.css", "www/css/form.css", "www/css/game.css", ]).ok(); cache.cache_file_group("text/javascript", "/.js", &[ "www/js/const.js", "www/js/util.js", "www/js/game_asset.js", "www/js/game.js", "www/js/interface.js", "www/js/ui.js", "www/js/scene.js", "www/js/system.js", "www/js/main.js", ]).ok(); cache.cache_file("image/png", "/favicon.png", "www/asset/favicon.png").ok(); let asset_path = std::path::Path::new("www/asset"); for asset in [ "promote.svg", "omen_dawn.svg", "dragon_dawn.svg", "castle_dawn.svg", "tower_dawn.svg", "lance_dawn.svg", "knight_dawn.svg", "militia_dawn.svg", "omen_dusk.svg", "dragon_dusk.svg", "castle_dusk.svg", "tower_dusk.svg", "lance_dusk.svg", "knight_dusk.svg", "militia_dusk.svg", ] { if cache.cache_file("image/svg+xml", &format!("/asset/{}", asset), asset_path.join(asset)).is_err() { println!("error: failed to load: {}", asset); } } let mut server = TlsServer::new(); if server.add_cert("omen.kirisame.com", "cert/fullchain.pem", "cert/privkey.pem").await.is_ok() { println!("Loaded cert file."); } match server.bind("0.0.0.0:38612").await { Ok(_) => { println!("Listener bind successful."); tokio::spawn(async move { while server.accept(handle_http, HttpServiceArgs { bus:bus.connect().unwrap(), cache:cache.clone(), }).await.is_ok() { } }); } Err(_) => { println!("error: failed to bind port 38612."); } } } Err(_) => { println!("error: failed to initialize HTTPS service."); } } loop { tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; } }