-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (58 loc) · 1.62 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
const express = require('express');
const expressGraphQL = require('express-graphql');
const keystone = require('keystone');
const pinoColada = require('pino-colada');
const pinoHttp = require('pino-http');
const dev = process.env.NODE_ENV !== 'production';
const start = ({
config,
handle = () => {}
}) => {
const app = express();
const pretty = pinoColada();
pretty.pipe(process.stdout);
keystone.init(config.options);
keystone.import('models');
keystone.set('locals', config.locals);
keystone.set('nav', config.nav);
keystone.initDatabaseConfig();
keystone.initExpressSession();
app.use('/keystone', keystone.Admin.Server.createStaticRouter(keystone));
app.use(express.static('static'));
app.use(keystone.get('session options').cookieParser);
app.use(keystone.expressSession);
app.use(keystone.session.persist);
app.use(pinoHttp({stream: pretty}));
app.use('/keystone', keystone.Admin.Server.createDynamicRouter(keystone));
app.use(
'/graphql',
expressGraphQL(req => ({
schema: require('./schema'),
graphiql: dev,
rootValue: {request: req},
pretty: dev
}))
);
app.get('*', handle);
return new Promise((resolve, reject) => {
keystone.openDatabaseConnection(() => {
const server = app.listen(3000, err => {
if (err) {
return reject(err);
}
console.log('> Ready on http://localhost:3000');
return resolve(server);
});
});
});
};
const stop = server => new Promise(resolve => {
keystone.closeDatabaseConnection(() => {
server.close();
resolve();
});
});
module.exports = {
start,
stop
};