From 591adcd00003b65a56e4f4b2fad8d0267cccb6ad Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sun, 11 Jul 2021 14:25:52 +0000 Subject: [PATCH] Move routes into fairings --- src/graphql.rs | 42 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 55 +++++++------------------------------------------- src/rest.rs | 18 +++++++++++++++++ 3 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 src/graphql.rs create mode 100644 src/rest.rs diff --git a/src/graphql.rs b/src/graphql.rs new file mode 100644 index 0000000..9d0ee33 --- /dev/null +++ b/src/graphql.rs @@ -0,0 +1,42 @@ +use rocket::fairing::AdHoc; +use rocket::{ response::content, State }; +use juniper::{ EmptySubscription }; + +use crate::model::Database; +use crate::schema::{ Schema, Query, Mutations }; + +#[rocket::get("/")] +fn graphiql() -> content::Html { + juniper_rocket::graphiql_source("/graphql", None) +} + +#[rocket::get("/graphql?")] +fn get_graphql_handler( + context: &State, + request: juniper_rocket::GraphQLRequest, + schema: &State, +) -> juniper_rocket::GraphQLResponse { + request.execute_sync(&*schema, &*context) +} + +#[rocket::post("/graphql", data = "")] +fn post_graphql_handler( + context: &State, + request: juniper_rocket::GraphQLRequest, + schema: &State, +) -> juniper_rocket::GraphQLResponse { + request.execute_sync(&*schema, &*context) +} + +pub fn stage() -> AdHoc { + let schema = Schema::new( + Query, + Mutations, + EmptySubscription::::new(), + ); + AdHoc::on_ignite("Add graphql endpoints", |rocket| async { + rocket + .mount("/", rocket::routes![graphiql, get_graphql_handler, post_graphql_handler]) + .manage(schema) + }) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f306464..23a8af9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,60 +1,19 @@ extern crate rocket; -use rocket::{ response::content, Rocket, State }; -use juniper::{ EmptySubscription }; - -mod model; -mod schema; - +use rocket::Rocket; use model::Database; -use schema::{ Schema, Query, Mutations }; -#[rocket::get("/")] -fn graphiql() -> content::Html { - juniper_rocket::graphiql_source("/graphql", None) -} - -#[rocket::get("/graphql?")] -fn get_graphql_handler( - context: &State, - request: juniper_rocket::GraphQLRequest, - schema: &State, -) -> juniper_rocket::GraphQLResponse { - request.execute_sync(&*schema, &*context) -} - -#[rocket::post("/graphql", data = "")] -fn post_graphql_handler( - context: &State, - request: juniper_rocket::GraphQLRequest, - schema: &State, -) -> juniper_rocket::GraphQLResponse { - request.execute_sync(&*schema, &*context) -} - -#[rocket::get("/rest/")] -fn get_rest(id: String, context: &State) -> Option { - match context.get_person(&id) { - None => None, - Some(person) => Some(person.name().unwrap().to_string()) - } -} +mod graphql; +mod model; +mod rest; +mod schema; #[rocket::main] async fn main() { - let schema = Schema::new( - Query, - Mutations, - EmptySubscription::::new(), - ); - Rocket::build() + .attach(graphql::stage()) + .attach(rest::stage()) .manage(Database::new()) - .manage(schema) - .mount( - "/", - rocket::routes![graphiql, get_graphql_handler, post_graphql_handler, get_rest], - ) .launch() .await .expect("server to launch"); diff --git a/src/rest.rs b/src/rest.rs new file mode 100644 index 0000000..2e920fe --- /dev/null +++ b/src/rest.rs @@ -0,0 +1,18 @@ +use rocket::fairing::AdHoc; +use rocket::State; +use crate::model::Database; + +#[rocket::get("/rest/")] +fn get_rest(id: String, context: &State) -> Option { + match context.get_person(&id) { + None => None, + Some(person) => Some(person.name().unwrap().to_string()) + } +} + +pub fn stage() -> AdHoc { + AdHoc::on_ignite("Add REST endpoint", |rocket| async { + rocket + .mount("/", rocket::routes![get_rest]) + }) +} \ No newline at end of file