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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use actix_web::{
guard,
web::{self, ServiceConfig},
HttpResponse,
};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
ObjectType, SubscriptionType,
};
use std::any::Any;
use std::sync::Mutex;
use crate::auth::middleware::AuthCheck;
use crate::graphql::{get_schema_for_subscriptions, PublishMutation, SubscriptionQuery};
use crate::options::{AuthCheckBlockState, Options};
use crate::pubsub::PubSub;
use crate::routes::{graphql, graphql_ws};
use crate::errors::*;
pub fn create_subscriptions_server<C, Q, M, S>(
opts: Options<C, Q, M, S>,
) -> Result<impl FnOnce(&mut ServiceConfig) + Clone>
where
C: Any + Send + Sync + Clone,
Q: Clone + ObjectType + 'static,
M: Clone + ObjectType + 'static,
S: Clone + SubscriptionType + 'static,
{
let subscriptions_server_data = match opts.subscriptions_server_data {
Some(subscriptions_server_data) => subscriptions_server_data,
None => bail!(ErrorKind::InvokedSubscriptionsServerWithInvalidOptions),
};
let schema = get_schema_for_subscriptions(opts.schema, opts.ctx);
let auth_middleware = match opts.authentication_block_state {
AuthCheckBlockState::AllowAll => AuthCheck::new(&opts.jwt_secret).allow_all(),
AuthCheckBlockState::AllowMissing => AuthCheck::new(&opts.jwt_secret).allow_missing(),
AuthCheckBlockState::BlockUnauthenticated => {
AuthCheck::new(&opts.jwt_secret).block_unauthenticated()
}
};
let graphql_endpoint = subscriptions_server_data.endpoint;
let playground_endpoint = opts.playground_endpoint;
let jwt_secret = opts.jwt_secret;
let configurer = move |cfg: &mut ServiceConfig| {
cfg.data(schema.clone())
.data(Mutex::new(PubSub::default()))
.service(
web::resource(&graphql_endpoint)
.guard(guard::Post())
.wrap(AuthCheck::new(&jwt_secret).block_unauthenticated())
.to(graphql::<SubscriptionQuery, PublishMutation, S>),
)
.service(
web::resource(&graphql_endpoint)
.guard(guard::Get())
.guard(guard::Header("upgrade", "websocket"))
.to(graphql_ws::<SubscriptionQuery, PublishMutation, S>),
);
let graphql_endpoint_for_closure = graphql_endpoint;
let graphiql_closure = move || {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(playground_source(
GraphQLPlaygroundConfig::new(&graphql_endpoint_for_closure)
.subscription_endpoint(&graphql_endpoint_for_closure),
))
};
match playground_endpoint {
Some(playground_endpoint) if cfg!(debug_assertions) => {
cfg.service(
web::resource(playground_endpoint)
.guard(guard::Get())
.to(graphiql_closure),
);
}
Some(playground_endpoint) => {
cfg.service(
web::resource(playground_endpoint)
.guard(guard::Get())
.wrap(auth_middleware.clone())
.to(graphiql_closure),
);
}
None => (),
};
};
Ok(configurer)
}