-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
219 lines (178 loc) · 5.1 KB
/
server.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import 'idempotent-babel-polyfill';
import { createServer } from 'http';
import BitScoop from 'bitscoop-sdk';
import _ from 'lodash';
import bodyParser from 'body-parser';
import config from 'config';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import express from 'express';
import expressPlayground from 'graphql-playground-middleware-express';
import apolloServerExpress from 'apollo-server-express';
import graphqlSubscriptions from 'graphql-subscriptions';
import graphql from 'graphql';
import subscriptionsTransportWs from 'subscriptions-transport-ws';
import mongoose from 'mongoose';
import csrf from './lib/middleware/csrf.js';
import views from './lib/views/index.js';
import cookieAuthorization from './lib/middleware/cookie-authorization.js';
import keyAuthorization from './lib/middleware/key-authorization.js';
import tokenAuthorization from './lib/middleware/token-authorization.js';
import wsCookieAuthorization from './lib/middleware/ws-cookie-authorization.js';
import meta from './lib/middleware/meta.js';
import { crudAPI } from './schema/index.js';
import { loadValidator } from './lib/validator.js';
const { ApolloServer } = apolloServerExpress;
const { PubSub } = graphqlSubscriptions;
const { execute, subscribe } = graphql;
const { SubscriptionServer } = subscriptionsTransportWs;
const BITSCOOP_API_KEY = config.bitscoop.api_key;
const MONGODB_URI = config.mongodb.address;
const server = express();
const wsServer = createServer(function(req, res) {
res.writeHead(200);
res.end();
});
const httpListenPort = 3000;
const wsListenPort = 3001;
const opts = {
autoReconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 1000,
autoIndex: false
};
const bitscoop = new BitScoop(BITSCOOP_API_KEY, config.bitscoop.arguments);
let corsConfig = {
origin: config.cors.address,
credentials: config.cors.credentials,
};
server.use(
cors(corsConfig)
);
mongoose.connect(MONGODB_URI, opts);
const mongooseConnect = mongoose.connection;
const pubSub = new PubSub();
mongooseConnect.on('error', e => {
if (e.message.code === 'ETIMEDOUT') {
console.log(e); //eslint-disable-line no-console
mongoose.connect(MONGODB_URI, opts);
}
console.log(e); //eslint-disable-line no-console
});
mongooseConnect.once('open', () => {
console.log(`MongoDB successfully connected to ${MONGODB_URI}`); //eslint-disable-line no-console
});
const apolloServer = new ApolloServer({
schema: crudAPI.schema,
tracing: true,
context: ({ req, res }) => ({
req: req,
res: res,
}) ,
playground: false,
path: '/gql',
formatError: error => ({
message: error.message,
locations: error.locations,
stack: error.stack ? error.stack.split('\n') : [],
path: error.path
})
});
loadValidator(config.validationSchemas)
.then(async function(validate) {
global.env = {
bitscoop: bitscoop,
validate: validate,
pubSub: pubSub
};
server.use(
crudAPI.uri,
meta,
bodyParser.json({
limit: '15MB'
}),
cookieParser(),
tokenAuthorization,
keyAuthorization,
cookieAuthorization,
csrf.validate
);
apolloServer.applyMiddleware({
app: server,
path: '/gql',
cors: corsConfig,
});
// http://localhost:3000/gql-p/
server.get(`${crudAPI.uri}-p`, async (req, res, next) => {
let csrftoken, sessionid;
let cookie = req.headers.cookie;
let cookieSplit = cookie ? cookie.split('; '): [];
_.each(cookieSplit, function(cookie) {
let keyVal = cookie.split('=');
if (keyVal[0] === 'sessionid') {
sessionid = keyVal[1];
}
});
if (sessionid != null) {
let session = await mongooseConnect.db.collection('sessions').findOne({
token: sessionid
});
csrftoken = csrf.tokens.create(session.csrf_secret);
}
expressPlayground.default({
endpoint: crudAPI.uri,
headers: {
'X-CSRF-Token': csrftoken
},
subscriptionsEndpoint: 'wss://api.lifescope.io/subscriptions',
settings: {
'request.credentials': 'include'
}
})(req, res, next)
});
server.use(
'/',
meta,
bodyParser.json({
limit: '15MB'
}),
bodyParser.urlencoded({
limit: '15MB'
}),
cookieParser(),
cookieAuthorization,
views
);
server.listen(httpListenPort, '0.0.0.0');
console.log('Lifescope API listening on: ' + httpListenPort + ' at ' + new Date()); //eslint-disable-line no-console
wsServer.listen(wsListenPort, '0.0.0.0', function() {
console.log('WS Server running on ' + wsListenPort); //eslint-disable-line no-console
SubscriptionServer.create({
schema: crudAPI.schema,
execute: execute,
subscribe: subscribe,
onConnect: async function(connectionParams, webSocket, context) {
let cookies = {};
let cookie = _.get(context, 'request.headers.cookie');
if (cookie != null) {
let split = cookie.split('; ');
_.each(split, function(item) {
let split = item.split('=');
cookies[split[0]] = split[1];
});
let user = await wsCookieAuthorization(cookies.sessionid);
return {
user: user
};
}
else {
return {
}
}
}
}, {
server: wsServer,
path: '/subscriptions'
});
});
});