diff --git a/README.md b/README.md index 1e263b3..6e6c497 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,4 @@ Build: `docker build --tag pw:1.0 .` Run: `docker run --rm -p 8123:8123 -p 9142:9142 -p 3012:3012 -v $(pwd)/backend/games:/planetwars/backend/games --name planetwars pw:1.0` -Add parameter `-e ROCKET_HOST_NAME=`, for example `-e ROCKET_HOST_NAME=mozaic.zeus.gent`, to set the domain used in the application. +Add parameter `-e PW_HOST_NAME=`, for example `-e PW_HOST_NAME=mozaic.zeus.gent`, to set the domain used in the application. diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 97c39c5..d620ba6 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -13,6 +13,8 @@ rand = { version = "0.7.3", default-features = true } async-std = { version = "1.7.0", features = ["attributes"] } futures = { version = "0.3.8", features = ["executor", "thread-pool"] } +figment = "0.9.4" + serde = "1.0.117" serde_derive = "1.0.117" serde_json = "1.0" diff --git a/backend/README.md b/backend/README.md index 68f954d..c1240f3 100644 --- a/backend/README.md +++ b/backend/README.md @@ -1,5 +1,5 @@ # Planetwars backend -Change hostname in info slides with ROCKET_HOST_NAME env variable, or in `Rocket.toml`. +Change hostname in info slides with PW_PORT, PW_HOST_NAME or PW_ADDRESS env variable. The main planetwars server that instanciates planetwars matches etc... diff --git a/backend/Rocket.toml b/backend/Rocket.toml deleted file mode 100644 index c4f0ef0..0000000 --- a/backend/Rocket.toml +++ /dev/null @@ -1,13 +0,0 @@ -[development] -address = "localhost" -port = 8000 -host_name = "localhost" - -[staging] -address = "0.0.0.0" -port = 80 - -[production] -address = "0.0.0.0" -port = 8123 -host_name = "mozaic.zeus.gent" diff --git a/backend/src/main.rs b/backend/src/main.rs index 39f3dd3..3d5cc92 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -11,6 +11,8 @@ extern crate futures; extern crate mozaic; extern crate rand; +extern crate figment; + extern crate tracing; extern crate tracing_futures; extern crate tracing_subscriber; @@ -22,6 +24,7 @@ extern crate rocket_contrib; #[macro_use] extern crate educe; +use figment::{providers::{Serialized, Env}}; use tracing_subscriber::{EnvFilter, FmtSubscriber}; use std::net::SocketAddr; @@ -46,6 +49,24 @@ use rocket_contrib::templates::{Engines, Template}; use std::collections::HashMap; +/// Config for the planetwars server +#[derive(Deserialize, Serialize, Debug)] +pub struct PWConfig { + host_name: String, + address: String, + port: u16, +} + +impl Default for PWConfig { + fn default() -> Self { + Self { + host_name: String::from("localhost"), + address: String::from("0.0.0.0"), + port: 8000, + } + } +} + /// Calculate viewbox from array of points (used in map preview), added to Tera engine. /// So this function can be called in template. fn calc_viewbox(value: &Value, _: &HashMap) -> tera::Result { @@ -111,15 +132,20 @@ async fn rocket() -> rocket::Rocket { let mut routes = Vec::new(); routes::fuel(&mut routes); - rocket::ignite() + let figment = rocket::Config::figment() + .merge(Serialized::defaults(PWConfig::default())) // Extend but not overwrite + .merge(Env::prefixed("PW_")); // Overwrite + + rocket::custom(figment) .manage(gm) .manage(pool) .manage(Games::new()) + .attach(AdHoc::config::()) // Manage the config + .mount("/", routes) .attach(AdHoc::on_attach("Assets Config", async move |rocket| { - // let host_name = rocket.config() - // .get_string("host_name") - // .unwrap_or("mozaic.zeus.gent".to_string()); - let host_name = "mozaic.zeus.gent".to_string(); + let pw_config = rocket.figment().extract::().unwrap_or_default(); + println!("PW Config {:?}", pw_config); + let host_name = pw_config.host_name.clone(); let tera = Template::custom(move |engines: &mut Engines| { engines.tera.register_filter("calc_viewbox", calc_viewbox); @@ -129,7 +155,6 @@ async fn rocket() -> rocket::Rocket { Ok(rocket.attach(tera)) })) - .mount("/", routes) } /// Creates the actual game_manager diff --git a/backend/templates/info/info_6.html.tera b/backend/templates/info/info_6.html.tera index a949a76..28fef3e 100644 --- a/backend/templates/info/info_6.html.tera +++ b/backend/templates/info/info_6.html.tera @@ -16,9 +16,9 @@ How to connect
         
-$ wget {{ "" | get_host_name}}/bot/runner.py
-$ wget {{ "" | get_host_name}}/bot/simple.py
-$ python3 runner.py -p 9142 --host {{ "" | get_host_name}}\
+$ wget {{ get_host_name() }}/bot/runner.py
+$ wget {{ get_host_name() }}/bot/simple.py
+$ python3 runner.py -p 9142 --host {{ get_host_name() }}\
     -n <Your name> -i <Id from the lobby> \
     python3 simple.py