Skip to content

Commit

Permalink
fix hostname issue + config
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvercr committed Nov 14, 2020
1 parent 2ba0438 commit 9a33ad4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
@@ -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...
13 changes: 0 additions & 13 deletions backend/Rocket.toml

This file was deleted.

37 changes: 31 additions & 6 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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> {
Expand Down Expand Up @@ -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);
Expand All @@ -129,7 +155,6 @@ async fn rocket() -> rocket::Rocket {

Ok(rocket.attach(tera))
}))
.mount("/", routes)
}

/// Creates the actual game_manager
Expand Down
6 changes: 3 additions & 3 deletions backend/templates/info/info_6.html.tera
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 9a33ad4

Please sign in to comment.