fix hostname issue + config

This commit is contained in:
ajuvercr 2020-11-14 18:21:27 +01:00
parent 2ba043862f
commit 9a33ad42fc
6 changed files with 38 additions and 24 deletions

View file

@ -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=<domain>`, for example `-e ROCKET_HOST_NAME=mozaic.zeus.gent`, to set the domain used in the application.
Add parameter `-e PW_HOST_NAME=<domain>`, for example `-e PW_HOST_NAME=mozaic.zeus.gent`, to set the domain used in the application.

View file

@ -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"

View file

@ -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...

View file

@ -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"

View file

@ -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<String, Value>) -> tera::Result<Value> {
@ -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::<PWConfig>()) // 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::<PWConfig>().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

View file

@ -16,9 +16,9 @@ How to connect
<pre class="commands">
<code>
$ 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 &lt;Your name&gt; -i &lt;Id from the lobby&gt; \
python3 simple.py
</code>