-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmain.rs
37 lines (32 loc) · 1.07 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use async_graphql::{http::GraphiQLSource, EmptySubscription, Schema};
use async_graphql_axum::GraphQL;
use axum::{
response::{Html, IntoResponse},
routing::get,
Router,
};
use files::{MutationRoot, QueryRoot, Storage};
use hyper::Method;
use tower_http::cors::{CorsLayer, AllowOrigin};
use tokio::net::TcpListener;
async fn graphiql() -> impl IntoResponse {
Html(GraphiQLSource::build().endpoint("/").finish())
}
#[tokio::main]
async fn main() {
let schema = Schema::build(QueryRoot, MutationRoot, EmptySubscription)
.data(Storage::default())
.finish();
println!("GraphiQL IDE: http://localhost:8000");
let app = Router::new()
.route("/", get(graphiql).post_service(GraphQL::new(schema)))
.layer(
CorsLayer::new()
.allow_origin(AllowOrigin::predicate(|_, _| true))
.allow_methods(vec![Method::GET, Method::POST]),
);
let listener = TcpListener::bind("127.0.0.1:8000").await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}