29
29
clippy:: wildcard_dependencies
30
30
) ]
31
31
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 } ;
50
34
use diesel:: pg:: PgConnection ;
51
35
use diesel:: r2d2:: { ConnectionManager , Pool } ;
52
-
53
36
use failure:: Error ;
54
- use futures:: Future ;
55
-
56
37
use juniper:: graphiql:: graphiql_source;
57
38
use juniper:: http:: GraphQLRequest ;
58
-
39
+ use serde:: { Deserialize , Serialize } ;
40
+ use std:: sync:: Arc ;
59
41
use structopt:: StructOpt ;
60
-
61
42
use wundergraph:: scalar:: WundergraphScalarValue ;
62
43
63
44
#[ derive( Debug , StructOpt ) ]
@@ -73,65 +54,28 @@ struct Opt {
73
54
#[ derive( Serialize , Deserialize , Debug ) ]
74
55
pub struct GraphQLData ( GraphQLRequest < WundergraphScalarValue > ) ;
75
56
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 {
82
59
schema : Arc < wundergraph_bench:: Schema < PgConnection > > ,
83
60
pool : Arc < Pool < ConnectionManager < PgConnection > > > ,
84
61
}
85
62
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 > {
117
64
let html = graphiql_source ( "/graphql" ) ;
118
65
Ok ( HttpResponse :: Ok ( )
119
66
. content_type ( "text/html; charset=utf-8" )
120
67
. body ( html) )
121
68
}
122
69
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) ?) )
135
79
}
136
80
137
81
#[ allow( clippy:: print_stdout) ]
@@ -147,36 +91,29 @@ fn main() {
147
91
let mutation = wundergraph_bench:: api:: Mutation :: default ( ) ;
148
92
let schema = wundergraph_bench:: Schema :: new ( query, mutation) ;
149
93
150
- let sys = actix:: System :: new ( "wundergraph-bench" ) ;
151
-
152
94
let schema = Arc :: new ( schema) ;
153
95
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 } ;
157
97
let url = opt. socket ;
158
98
159
99
// 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 ( || {
169
110
HttpResponse :: Found ( )
170
111
. header ( "location" , "/graphiql" )
171
112
. finish ( )
172
- } )
173
- } )
113
+ } ) )
174
114
} )
175
- . workers ( num_cpus:: get ( ) * 2 )
176
115
. bind ( & url)
177
116
. expect ( "Failed to start server" )
178
- . start ( ) ;
179
-
180
- println ! ( "Started http server: http://{}" , url) ;
181
- let _ = sys. run ( ) ;
117
+ . run ( )
118
+ . unwrap ( ) ;
182
119
}
0 commit comments