@@ -11,6 +11,8 @@ extern crate futures;
1111extern crate mozaic;
1212extern crate rand;
1313
14+ extern crate figment;
15+
1416extern crate tracing;
1517extern crate tracing_futures;
1618extern crate tracing_subscriber;
@@ -22,6 +24,7 @@ extern crate rocket_contrib;
2224#[ macro_use]
2325extern crate educe;
2426
27+ use figment:: { providers:: { Serialized , Env } } ;
2528use tracing_subscriber:: { EnvFilter , FmtSubscriber } ;
2629
2730use std:: net:: SocketAddr ;
@@ -46,6 +49,24 @@ use rocket_contrib::templates::{Engines, Template};
4649
4750use 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.
5172fn 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
0 commit comments