juniper/examples/server.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2016-09-11 11:41:29 -05:00
extern crate iron;
extern crate mount;
extern crate logger;
extern crate rustc_serialize;
#[macro_use] extern crate juniper;
use std::env;
2016-09-11 11:41:29 -05:00
use mount::Mount;
use logger::Logger;
use iron::prelude::*;
use juniper::EmptyMutation;
2016-09-11 11:41:29 -05:00
use juniper::iron_handlers::{GraphQLHandler, GraphiQLHandler};
use juniper::tests::model::Database;
2016-09-11 11:41:29 -05:00
fn context_factory(_: &mut Request) -> Database {
Database::new()
2016-09-11 11:41:29 -05:00
}
fn main() {
let mut mount = Mount::new();
let graphql_endpoint = GraphQLHandler::new(
context_factory,
Database::new(),
EmptyMutation::<Database>::new(),
);
2016-09-11 11:41:29 -05:00
let graphiql_endpoint = GraphiQLHandler::new("/graphql");
2016-09-18 08:55:30 -05:00
mount.mount("/", graphiql_endpoint);
2016-09-11 11:41:29 -05:00
mount.mount("/graphql", graphql_endpoint);
let mut chain = Chain::new(mount);
2017-02-04 04:37:01 -06:00
setup_logger(&mut chain);
2016-09-11 11:41:29 -05:00
let host = env::var("LISTEN").unwrap_or("0.0.0.0:8080".to_owned());
2016-09-11 11:41:29 -05:00
println!("GraphQL server started on {}", host);
Iron::new(chain).http(host.as_str()).unwrap();
2016-09-11 11:41:29 -05:00
}
2017-02-04 04:37:01 -06:00
// Temporary fix - Iron's logger middleware does not work on Windows
#[cfg(not(windows))]
fn setup_logger(chain: &mut Chain) {
let (logger_before, logger_after) = Logger::new(None);
chain.link_before(logger_before);
chain.link_after(logger_after);
}
2017-02-04 04:37:01 -06:00
#[cfg(windows)]
fn setup_logger(_: &mut Chain) {
}