make it compile, but can't run and break hostname config

This commit is contained in:
ajuvercr 2020-11-11 22:21:56 +01:00
parent dcf1173cb8
commit ba0b2c7655
3 changed files with 32 additions and 29 deletions

View file

@ -8,18 +8,18 @@ edition = "2018"
[dependencies] [dependencies]
mozaic = { git = "https://github.com/ZeusWPI/MOZAICP" } mozaic = { git = "https://github.com/ZeusWPI/MOZAICP" }
rand = { version = "0.6.5", default-features = true } rand = { version = "0.7.3", default-features = true }
async-std = { version = "1.5.0", features = ["attributes"] } async-std = { version = "1.7.0", features = ["attributes"] }
futures = { version = "0.3.1", features = ["executor", "thread-pool"] } futures = { version = "0.3.8", features = ["executor", "thread-pool"] }
serde = "1.0.100" serde = "1.0.117"
serde_derive = "1.0.100" serde_derive = "1.0.117"
serde_json = "1.0" serde_json = "1.0"
tracing = "0.1.9" tracing = "0.1.21"
tracing-futures = "0.1.0" tracing-futures = "0.2.4"
tracing-subscriber = "0.1.5" tracing-subscriber = "0.2.15"
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "async" } rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "master" }
rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket", branch = "async", features = ["handlebars_templates", "tera_templates"] } rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket", branch = "master", features = ["handlebars_templates", "tera_templates"] }
educe = { version = "0.4.2", features = ["Debug", "Default", "Hash", "Clone", "Copy"] } educe = { version = "0.4.13", features = ["Debug", "Default", "Hash", "Clone", "Copy"] }

View file

@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, async_closure)] #![feature(proc_macro_hygiene, async_closure, decl_macro)]
extern crate serde; extern crate serde;
#[macro_use] #[macro_use]
@ -86,16 +86,19 @@ fn get_colour(value: &Value, _: &HashMap<String, Value>) -> tera::Result<Value>
)); ));
} }
struct HostNameFilter(String); #[async_std::main]
async fn main(){
impl tera::Filter for HostNameFilter { let rocket = rocket().await;
fn filter(&self, _: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> { rocket.launch().await.expect("ROCKET CRASHED");
Ok(Value::String(self.0.clone()))
} }
fn get_host_name(host_name: &str) -> impl Fn(&HashMap<String, Value>) -> tera::Result<Value> + Sync + Send {
let host_name = host_name.to_string();
move |_| Ok(Value::String(host_name.clone()))
} }
/// Async main function, starting logger, graph and rocket /// Async main function, starting logger, graph and rocket
#[launch]
async fn rocket() -> rocket::Rocket { async fn rocket() -> rocket::Rocket {
let fut = graph::set_default(); let fut = graph::set_default();
@ -104,10 +107,11 @@ async fn rocket() -> rocket::Rocket {
.finish(); .finish();
tracing::subscriber::set_global_default(sub).unwrap(); tracing::subscriber::set_global_default(sub).unwrap();
let pool = ThreadPool::new().unwrap(); let pool = ThreadPool::builder().create().unwrap();
pool.spawn_ok(fut.map(|_| ())); pool.spawn_ok(fut.map(|_| ()));
let gm = create_game_manager("0.0.0.0:9142", pool.clone()).await; let gm = create_game_manager("0.0.0.0:9142", pool.clone()).await;
async_std::task::sleep(std::time::Duration::from_millis(100)).await;
async_std::task::sleep(std::time::Duration::from_millis(200)).await;
let mut routes = Vec::new(); let mut routes = Vec::new();
routes::fuel(&mut routes); routes::fuel(&mut routes);
@ -116,17 +120,16 @@ async fn rocket() -> rocket::Rocket {
.manage(gm) .manage(gm)
.manage(pool) .manage(pool)
.manage(Games::new()) .manage(Games::new())
.attach(AdHoc::on_attach("Assets Config", async move |mut rocket| { .attach(AdHoc::on_attach("Assets Config", async move |rocket| {
let host_name = rocket.config().await // let host_name = rocket.config()
.get_str("host_name") // .get_string("host_name")
.unwrap_or("mozaic.zeus.gent") // .unwrap_or("mozaic.zeus.gent".to_string());
.to_string(); let host_name = "mozaic.zeus.gent".to_string();
let tera = Template::custom(move |engines: &mut Engines| { let tera = Template::custom(move |engines: &mut Engines| {
let filter = HostNameFilter(host_name.clone());
engines.tera.register_filter("calc_viewbox", calc_viewbox); engines.tera.register_filter("calc_viewbox", calc_viewbox);
engines.tera.register_filter("get_colour", get_colour); engines.tera.register_filter("get_colour", get_colour);
engines.tera.register_filter("get_host_name", filter); engines.tera.register_function("get_host_name", get_host_name(&host_name));
}); });
Ok(rocket.attach(tera)) Ok(rocket.attach(tera))
@ -139,7 +142,7 @@ async fn rocket() -> rocket::Rocket {
async fn create_game_manager(tcp: &str, pool: ThreadPool) -> game::Manager { async fn create_game_manager(tcp: &str, pool: ThreadPool) -> game::Manager {
let addr = tcp.parse::<SocketAddr>().unwrap(); let addr = tcp.parse::<SocketAddr>().unwrap();
let (gmb, handle) = game::Manager::builder(pool.clone()); let (gmb, handle) = game::Manager::builder(pool.clone());
pool.spawn_ok(handle.map(|_| ())); pool.spawn_ok(handle.map(|_| {println!("I'm done")}));
let ep = TcpEndpoint::new(addr, pool.clone()); let ep = TcpEndpoint::new(addr, pool.clone());
let gmb = gmb.add_endpoint(ep, "TCP endpoint"); let gmb = gmb.add_endpoint(ep, "TCP endpoint");

View file

@ -77,7 +77,7 @@ async fn post_game(
state: State<'_, Games>, state: State<'_, Games>,
) -> Result<Json<GameRes>, String> { ) -> Result<Json<GameRes>, String> {
let game = build_builder( let game = build_builder(
tp.clone(), tp.inner().clone(),
game_req.nop, game_req.nop,
game_req.max_turns, game_req.max_turns,
&game_req.map, &game_req.map,