Move routes into fairings
This commit is contained in:
parent
015ecaff85
commit
591adcd000
|
@ -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<String> {
|
||||
juniper_rocket::graphiql_source("/graphql", None)
|
||||
}
|
||||
|
||||
#[rocket::get("/graphql?<request>")]
|
||||
fn get_graphql_handler(
|
||||
context: &State<Database>,
|
||||
request: juniper_rocket::GraphQLRequest,
|
||||
schema: &State<Schema>,
|
||||
) -> juniper_rocket::GraphQLResponse {
|
||||
request.execute_sync(&*schema, &*context)
|
||||
}
|
||||
|
||||
#[rocket::post("/graphql", data = "<request>")]
|
||||
fn post_graphql_handler(
|
||||
context: &State<Database>,
|
||||
request: juniper_rocket::GraphQLRequest,
|
||||
schema: &State<Schema>,
|
||||
) -> juniper_rocket::GraphQLResponse {
|
||||
request.execute_sync(&*schema, &*context)
|
||||
}
|
||||
|
||||
pub fn stage() -> AdHoc {
|
||||
let schema = Schema::new(
|
||||
Query,
|
||||
Mutations,
|
||||
EmptySubscription::<Database>::new(),
|
||||
);
|
||||
AdHoc::on_ignite("Add graphql endpoints", |rocket| async {
|
||||
rocket
|
||||
.mount("/", rocket::routes![graphiql, get_graphql_handler, post_graphql_handler])
|
||||
.manage(schema)
|
||||
})
|
||||
}
|
55
src/main.rs
55
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<String> {
|
||||
juniper_rocket::graphiql_source("/graphql", None)
|
||||
}
|
||||
|
||||
#[rocket::get("/graphql?<request>")]
|
||||
fn get_graphql_handler(
|
||||
context: &State<Database>,
|
||||
request: juniper_rocket::GraphQLRequest,
|
||||
schema: &State<Schema>,
|
||||
) -> juniper_rocket::GraphQLResponse {
|
||||
request.execute_sync(&*schema, &*context)
|
||||
}
|
||||
|
||||
#[rocket::post("/graphql", data = "<request>")]
|
||||
fn post_graphql_handler(
|
||||
context: &State<Database>,
|
||||
request: juniper_rocket::GraphQLRequest,
|
||||
schema: &State<Schema>,
|
||||
) -> juniper_rocket::GraphQLResponse {
|
||||
request.execute_sync(&*schema, &*context)
|
||||
}
|
||||
|
||||
#[rocket::get("/rest/<id>")]
|
||||
fn get_rest(id: String, context: &State<Database>) -> Option<String> {
|
||||
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::<Database>::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");
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
use rocket::fairing::AdHoc;
|
||||
use rocket::State;
|
||||
use crate::model::Database;
|
||||
|
||||
#[rocket::get("/rest/<id>")]
|
||||
fn get_rest(id: String, context: &State<Database>) -> Option<String> {
|
||||
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])
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue