-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
70 lines (56 loc) · 1.69 KB
/
index.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
require('dotenv').config()
const express = require('express')
const http = require('http')
const helmet = require('helmet')
const {ApolloServer} = require('apollo-server-express')
const {typeDefs, resolvers} = require('./schema')
const createDataLoaders = require('./dataloaders')
const {
NODE_ENV,
PORT = 3000
} = process.env
const isProd = NODE_ENV === 'production'
const app = express()
app.enable('trust proxy', 'loopback')
const defaultCSP = helmet.contentSecurityPolicy.getDefaultDirectives()
const helmetOptions = {
contentSecurityPolicy: {
directives: {
...defaultCSP,
// Add GraphQl Playground CSP overrides in dev
...(isProd ? {} : {
'script-src': [
...defaultCSP['script-src'],
// GraphQL Playground inline script
"'sha256-jy0ROHCLlkmrjNmmholpRXAJgTmhuirtXREXGa8VmVU='",
'cdn.jsdelivr.net'
],
'img-src': [
...defaultCSP['img-src'],
'cdn.jsdelivr.net'
]
})
}
}
}
const helmetHsts = helmet(helmetOptions)
const helmetNoHsts = helmet({...helmetOptions, hsts: false})
app.use((req, res, next) => {
if (isProd && req.secure) {
return helmetHsts(req, res, next)
}
return helmetNoHsts(req, res, next)
})
const api = new ApolloServer({
typeDefs,
resolvers,
context: () => ({
dataLoaders: createDataLoaders()
})
})
api.applyMiddleware({app})
const httpServer = http.createServer(app)
api.installSubscriptionHandlers(httpServer)
httpServer.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
})