From: Ian Jackson Date: Wed, 26 Aug 2020 22:26:14 +0000 (+0100) Subject: refactor rocket config X-Git-Tag: otter-0.2.0~1047 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=33ff868f6ba83a2a2aa304af481636ac08a6281d;p=otter.git refactor rocket config --- diff --git a/src/bin/server.rs b/src/bin/server.rs index f72ad859..b6a0e967 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -83,7 +83,27 @@ fn main() { .enable(Frame::Deny) .enable(Referrer::NoReferrer); - let r = rocket::ignite() + let config = config(); + + let mut cbuilder = rocket::config::Config::build( + if config.debug { + rocket::config::Environment::Development + } else { + eprintln!("requesting Production"); + rocket::config::Environment::Production + } + ); + + if config.debug { + cbuilder = cbuilder.address("127.0.0.1") + } + cbuilder = cbuilder.workers(config.rocket_workers); + if let Some(port) = config.http_port { + cbuilder = cbuilder.port(port); + } + let rconfig = cbuilder.finalize()?; + + let r = rocket::custom(rconfig) .attach(helmet) .attach(Template::fairing()) .mount("/", routes![ diff --git a/src/global.rs b/src/global.rs index 6ff4af20..4f6dd640 100644 --- a/src/global.rs +++ b/src/global.rs @@ -930,6 +930,8 @@ pub struct ServerConfigSpec { pub save_directory: Option, pub command_socket: Option, pub debug: Option, + pub http_port: Option, + pub rocket_workers: Option, } #[derive(Debug,Clone)] @@ -937,6 +939,8 @@ pub struct ServerConfig { pub save_directory: String, pub command_socket: String, pub debug: bool, + pub http_port: Option, + pub rocket_workers: u16, } impl TryFrom for ServerConfig { @@ -945,6 +949,7 @@ impl TryFrom for ServerConfig { fn try_from(spec: ServerConfigSpec) -> ServerConfig { let ServerConfigSpec { save_directory, command_socket, debug, + http_port, rocket_workers, } = spec; let save_directory = save_directory @@ -957,9 +962,12 @@ impl TryFrom for ServerConfig { } let debug = debug.unwrap_or(cfg!(debug_assertions)); + let rocket_workers = rocket_workers.unwrap_or( + if debug { 20 } else { 1000 }); ServerConfig { save_directory, command_socket, debug, + http_port, rocket_workers, } } }