2929 clippy:: wildcard_dependencies
3030) ]
3131
32- use actix;
33-
34- use structopt;
35-
36- #[ macro_use]
37- extern crate serde;
38-
39- use num_cpus;
40- use serde_json;
41- use wundergraph_bench;
42-
43- use std:: sync:: Arc ;
44-
45- use actix:: { Actor , Addr , Handler , Message , SyncArbiter , SyncContext } ;
46- use actix_web:: {
47- http, server, App , AsyncResponder , FutureResponse , HttpRequest , HttpResponse , Json , State ,
48- } ;
49-
32+ use actix_web:: web:: { Data , Json } ;
33+ use actix_web:: { middleware, web, App , HttpResponse , HttpServer } ;
5034use diesel:: pg:: PgConnection ;
5135use diesel:: r2d2:: { ConnectionManager , Pool } ;
52-
5336use failure:: Error ;
54- use futures:: Future ;
55-
5637use juniper:: graphiql:: graphiql_source;
5738use juniper:: http:: GraphQLRequest ;
58-
39+ use serde:: { Deserialize , Serialize } ;
40+ use std:: sync:: Arc ;
5941use structopt:: StructOpt ;
60-
6142use wundergraph:: scalar:: WundergraphScalarValue ;
6243
6344#[ derive( Debug , StructOpt ) ]
@@ -73,65 +54,28 @@ struct Opt {
7354#[ derive( Serialize , Deserialize , Debug ) ]
7455pub struct GraphQLData ( GraphQLRequest < WundergraphScalarValue > ) ;
7556
76- impl Message for GraphQLData {
77- type Result = Result < String , Error > ;
78- }
79-
80- #[ allow( missing_debug_implementations) ]
81- pub struct GraphQLExecutor {
57+ #[ derive( Clone ) ]
58+ struct AppState {
8259 schema : Arc < wundergraph_bench:: Schema < PgConnection > > ,
8360 pool : Arc < Pool < ConnectionManager < PgConnection > > > ,
8461}
8562
86- impl GraphQLExecutor {
87- fn new (
88- schema : Arc < wundergraph_bench:: Schema < PgConnection > > ,
89- pool : Arc < Pool < ConnectionManager < PgConnection > > > ,
90- ) -> Self {
91- Self { schema, pool }
92- }
93- }
94-
95- impl Actor for GraphQLExecutor {
96- type Context = SyncContext < Self > ;
97- }
98-
99- impl Handler < GraphQLData > for GraphQLExecutor {
100- type Result = Result < String , Error > ;
101-
102- fn handle ( & mut self , msg : GraphQLData , _: & mut Self :: Context ) -> Self :: Result {
103- let ctx = self . pool . get ( ) ?;
104- // let ctx = MyContext::new(self.pool.get()?);
105- let res = msg. 0 . execute ( & * self . schema , & ctx) ;
106- let res_text = serde_json:: to_string ( & res) ?;
107- Ok ( res_text)
108- }
109- }
110-
111- struct AppState {
112- executor : Addr < GraphQLExecutor > ,
113- }
114-
115- #[ cfg_attr( feature = "clippy" , allow( needless_pass_by_value) ) ]
116- fn graphiql ( _req : & HttpRequest < AppState > ) -> Result < HttpResponse , Error > {
63+ fn graphiql ( ) -> Result < HttpResponse , Error > {
11764 let html = graphiql_source ( "/graphql" ) ;
11865 Ok ( HttpResponse :: Ok ( )
11966 . content_type ( "text/html; charset=utf-8" )
12067 . body ( html) )
12168}
12269
123- #[ cfg_attr( feature = "clippy" , allow( needless_pass_by_value) ) ]
124- fn graphql ( ( st, data) : ( State < AppState > , Json < GraphQLData > ) ) -> FutureResponse < HttpResponse > {
125- st. executor
126- . send ( data. 0 )
127- . from_err ( )
128- . and_then ( |res| match res {
129- Ok ( user) => Ok ( HttpResponse :: Ok ( )
130- . content_type ( "application/json" )
131- . body ( user) ) ,
132- Err ( _) => Ok ( HttpResponse :: InternalServerError ( ) . into ( ) ) ,
133- } )
134- . responder ( )
70+ fn graphql (
71+ Json ( GraphQLData ( data) ) : Json < GraphQLData > ,
72+ st : Data < AppState > ,
73+ ) -> Result < HttpResponse , Error > {
74+ let ctx = st. get_ref ( ) . pool . get ( ) ?;
75+ let res = data. execute ( & st. get_ref ( ) . schema , & ctx) ;
76+ Ok ( HttpResponse :: Ok ( )
77+ . content_type ( "application/json" )
78+ . body ( serde_json:: to_string ( & res) ?) )
13579}
13680
13781#[ allow( clippy:: print_stdout) ]
@@ -147,36 +91,29 @@ fn main() {
14791 let mutation = wundergraph_bench:: api:: Mutation :: default ( ) ;
14892 let schema = wundergraph_bench:: Schema :: new ( query, mutation) ;
14993
150- let sys = actix:: System :: new ( "wundergraph-bench" ) ;
151-
15294 let schema = Arc :: new ( schema) ;
15395 let pool = Arc :: new ( pool) ;
154- let addr = SyncArbiter :: start ( num_cpus:: get ( ) + 1 , move || {
155- GraphQLExecutor :: new ( schema. clone ( ) , pool. clone ( ) )
156- } ) ;
96+ let data = AppState { schema, pool } ;
15797 let url = opt. socket ;
15898
15999 // Start http server
160- server:: new ( move || {
161- App :: with_state ( AppState {
162- executor : addr. clone ( ) ,
163- } )
164- . resource ( "/graphql" , |r| r. method ( http:: Method :: POST ) . with ( graphql) )
165- . resource ( "/graphql" , |r| r. method ( http:: Method :: GET ) . with ( graphql) )
166- . resource ( "/graphiql" , |r| r. method ( http:: Method :: GET ) . h ( graphiql) )
167- . default_resource ( |r| {
168- r. get ( ) . f ( |_| {
100+ println ! ( "Started http server: http://{}" , url) ;
101+
102+ HttpServer :: new ( move || {
103+ App :: new ( )
104+ . data ( data. clone ( ) )
105+ . wrap ( middleware:: Logger :: default ( ) )
106+ . route ( "/graphql" , web:: get ( ) . to ( graphql) )
107+ . route ( "/graphql" , web:: post ( ) . to ( graphql) )
108+ . route ( "/graphiql" , web:: get ( ) . to ( graphiql) )
109+ . default_service ( web:: route ( ) . to ( || {
169110 HttpResponse :: Found ( )
170111 . header ( "location" , "/graphiql" )
171112 . finish ( )
172- } )
173- } )
113+ } ) )
174114 } )
175- . workers ( num_cpus:: get ( ) * 2 )
176115 . bind ( & url)
177116 . expect ( "Failed to start server" )
178- . start ( ) ;
179-
180- println ! ( "Started http server: http://{}" , url) ;
181- let _ = sys. run ( ) ;
117+ . run ( )
118+ . unwrap ( ) ;
182119}
0 commit comments