extern crate fanta; extern crate futures; extern crate serde; extern crate serde_json; extern crate tokio_proto; extern crate tokio_service; #[macro_use] extern crate serde_derive; #[macro_use] extern crate lazy_static; mod context; use futures::future; use fanta::{App, MiddlewareChain, MiddlewareReturnValue}; use context::{generate_context, Ctx}; lazy_static! { static ref APP: App = { let mut _app = App::::create(generate_context); _app.get("/json", vec![json]); _app.get("/plaintext", vec![plaintext]); _app.set404(vec![not_found_404]); _app }; } fn not_found_404(context: Ctx, _chain: &MiddlewareChain) -> MiddlewareReturnValue { let mut context = Ctx::new(context); context.body = " ( ͡° ͜ʖ ͡°) What're you looking for here? ".to_owned(); context.set_header("Content-Type", "text/html"); context.status_code = 404; Box::new(future::ok(context)) } #[derive(Serialize)] struct JsonStruct<'a> { message: &'a str } fn json(mut context: Ctx, _chain: &MiddlewareChain) -> MiddlewareReturnValue { let json = JsonStruct { message: "Hello, World!" }; let val = serde_json::to_string(&json).unwrap(); context.body = val; context.set_header("Server", "fanta"); context.set_header("Content-Type", "application/json"); Box::new(future::ok(context)) } fn plaintext(mut context: Ctx, _chain: &MiddlewareChain) -> MiddlewareReturnValue { let val = "Hello, World!".to_owned(); context.body = val; context.set_header("Server", "fanta"); context.set_header("Content-Type", "text/plain"); Box::new(future::ok(context)) } fn main() { println!("Starting server..."); App::start(&APP, "0.0.0.0".to_string(), "4321".to_string()); }