Skip to content

Commit 9a33ad4

Browse files
committed
fix hostname issue + config
1 parent 2ba0438 commit 9a33ad4

File tree

6 files changed

+38
-24
lines changed

6 files changed

+38
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Build: `docker build --tag pw:1.0 .`
66

77
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`
88

9-
Add parameter `-e ROCKET_HOST_NAME=<domain>`, for example `-e ROCKET_HOST_NAME=mozaic.zeus.gent`, to set the domain used in the application.
9+
Add parameter `-e PW_HOST_NAME=<domain>`, for example `-e PW_HOST_NAME=mozaic.zeus.gent`, to set the domain used in the application.

backend/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ rand = { version = "0.7.3", default-features = true }
1313
async-std = { version = "1.7.0", features = ["attributes"] }
1414
futures = { version = "0.3.8", features = ["executor", "thread-pool"] }
1515

16+
figment = "0.9.4"
17+
1618
serde = "1.0.117"
1719
serde_derive = "1.0.117"
1820
serde_json = "1.0"

backend/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Planetwars backend
22

3-
Change hostname in info slides with ROCKET_HOST_NAME env variable, or in `Rocket.toml`.
3+
Change hostname in info slides with PW_PORT, PW_HOST_NAME or PW_ADDRESS env variable.
44

55
The main planetwars server that instanciates planetwars matches etc...

backend/Rocket.toml

Lines changed: 0 additions & 13 deletions
This file was deleted.

backend/src/main.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ extern crate futures;
1111
extern crate mozaic;
1212
extern crate rand;
1313

14+
extern crate figment;
15+
1416
extern crate tracing;
1517
extern crate tracing_futures;
1618
extern crate tracing_subscriber;
@@ -22,6 +24,7 @@ extern crate rocket_contrib;
2224
#[macro_use]
2325
extern crate educe;
2426

27+
use figment::{providers::{Serialized, Env}};
2528
use tracing_subscriber::{EnvFilter, FmtSubscriber};
2629

2730
use std::net::SocketAddr;
@@ -46,6 +49,24 @@ use rocket_contrib::templates::{Engines, Template};
4649

4750
use std::collections::HashMap;
4851

52+
/// Config for the planetwars server
53+
#[derive(Deserialize, Serialize, Debug)]
54+
pub struct PWConfig {
55+
host_name: String,
56+
address: String,
57+
port: u16,
58+
}
59+
60+
impl Default for PWConfig {
61+
fn default() -> Self {
62+
Self {
63+
host_name: String::from("localhost"),
64+
address: String::from("0.0.0.0"),
65+
port: 8000,
66+
}
67+
}
68+
}
69+
4970
/// Calculate viewbox from array of points (used in map preview), added to Tera engine.
5071
/// So this function can be called in template.
5172
fn calc_viewbox(value: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> {
@@ -111,15 +132,20 @@ async fn rocket() -> rocket::Rocket {
111132
let mut routes = Vec::new();
112133
routes::fuel(&mut routes);
113134

114-
rocket::ignite()
135+
let figment = rocket::Config::figment()
136+
.merge(Serialized::defaults(PWConfig::default())) // Extend but not overwrite
137+
.merge(Env::prefixed("PW_")); // Overwrite
138+
139+
rocket::custom(figment)
115140
.manage(gm)
116141
.manage(pool)
117142
.manage(Games::new())
143+
.attach(AdHoc::config::<PWConfig>()) // Manage the config
144+
.mount("/", routes)
118145
.attach(AdHoc::on_attach("Assets Config", async move |rocket| {
119-
// let host_name = rocket.config()
120-
// .get_string("host_name")
121-
// .unwrap_or("mozaic.zeus.gent".to_string());
122-
let host_name = "mozaic.zeus.gent".to_string();
146+
let pw_config = rocket.figment().extract::<PWConfig>().unwrap_or_default();
147+
println!("PW Config {:?}", pw_config);
148+
let host_name = pw_config.host_name.clone();
123149

124150
let tera = Template::custom(move |engines: &mut Engines| {
125151
engines.tera.register_filter("calc_viewbox", calc_viewbox);
@@ -129,7 +155,6 @@ async fn rocket() -> rocket::Rocket {
129155

130156
Ok(rocket.attach(tera))
131157
}))
132-
.mount("/", routes)
133158
}
134159

135160
/// Creates the actual game_manager

backend/templates/info/info_6.html.tera

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ How to connect
1616

1717
<pre class="commands">
1818
<code>
19-
$ wget {{ "" | get_host_name}}/bot/runner.py
20-
$ wget {{ "" | get_host_name}}/bot/simple.py
21-
$ python3 runner.py -p 9142 --host {{ "" | get_host_name}}\
19+
$ wget {{ get_host_name() }}/bot/runner.py
20+
$ wget {{ get_host_name() }}/bot/simple.py
21+
$ python3 runner.py -p 9142 --host {{ get_host_name() }}\
2222
-n &lt;Your name&gt; -i &lt;Id from the lobby&gt; \
2323
python3 simple.py
2424
</code>

0 commit comments

Comments
 (0)