This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
112 lines (92 loc) · 2.89 KB
/
app.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
const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const { graphqlKoa, graphiqlKoa } = require('apollo-server-koa')
const depthLimit = require('graphql-depth-limit')
const reqLogger = require('koa-pino-logger')
const cors = require('@koa/cors')
const session = require('koa-session')
const schema = require('./schema')()
const loaders = require('./data/loaders')
const acl = require('./data/middleware/acl')
const routes = require('./routes')
const { valid } = require('./data/session')
module.exports = async (dbPool, logger, serversPool) => {
const app = new Koa()
const router = new Router()
const sessionConfig =
{
key: process.env.SESSION_NAME,
renew: true,
httpOnly: true,
decode (str) {
const body = Buffer.from(str, 'base64').toString('utf8')
const json = JSON.parse(body)
if (json.playerId && json.playerId.type === 'Buffer') json.playerId = Buffer.from(json.playerId.data)
return json
},
sameSite: 'lax',
domain: process.env.SESSION_DOMAIN,
valid (session, data) {
return valid(data)
}
}
app.keys = [process.env.SESSION_KEY]
app.use(async (ctx, next) => {
try {
await next()
} catch (err) {
ctx.log.error(err)
ctx.status = err.status || 500
ctx.body = { error: err.expose ? err.message : 'Internal Server Error' }
ctx.app.emit('error', err, ctx)
}
})
app.use(async (ctx, next) => {
ctx.state.dbPool = dbPool
ctx.state.serversPool = serversPool
ctx.state.loaders = loaders(ctx)
return next()
})
app.use(reqLogger())
app.use(bodyParser())
app.use(cors({ origin: process.env.SITE_HOST, credentials: true, maxAge: 600 }))
app.use(session(sessionConfig, app))
app.use(acl)
const graphqlOpts = async ({ state, session, log }) => {
return {
schema,
context: { state, session, log },
cacheControl: true,
formatError (error) {
if (error.originalError && error.originalError.exposed) {
return error
}
if (error.originalError && error.originalError.code === 'ERR_GRAPHQL_CONSTRAINT_VALIDATION') {
const { fieldName, message } = error.originalError
return { ...error, message: `${fieldName} ${message}` }
}
logger.error(error)
return { message: 'Internal Server Error' }
},
validationRules: [depthLimit(10)]
}
}
router.post('/graphql', graphqlKoa(graphqlOpts))
router.get('/graphql', graphqlKoa(graphqlOpts))
if (process.env.NODE_ENV !== 'production') {
router.get('/graphiql', graphiqlKoa({
endpointURL: '/graphql'
}))
}
routes(router)
app.use(router.routes())
app.use(router.allowedMethods())
app.use(async (ctx) => {
if (ctx.status === 404) {
ctx.status = 404
ctx.body = { error: 'Not Found' }
}
})
return app
}