-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathserver.ts
More file actions
86 lines (78 loc) · 3.19 KB
/
server.ts
File metadata and controls
86 lines (78 loc) · 3.19 KB
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
import { ApolloServer } from '@apollo/server'
import { expressMiddleware } from '@apollo/server/express4'
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'
import express from 'express'
import cors from 'cors'
import * as http from 'http'
import bodyParser from 'body-parser'
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache'
import { applyMiddleware } from 'graphql-middleware'
import { graphqlSchema } from './graphql/resolvers.js'
import MutableAreaDataSource from './model/MutableAreaDataSource.js'
import ChangeLogDataSource from './model/ChangeLogDataSource.js'
import MutableMediaDataSource from './model/MutableMediaDataSource.js'
import MutableClimbDataSource from './model/MutableClimbDataSource.js'
import TickDataSource from './model/TickDataSource.js'
import permissions from './auth/permissions.js'
import { createContext } from './auth/middleware.js'
import { localDevBypassAuthContext } from './auth/local-dev/middleware.js'
import localDevBypassAuthPermissions from './auth/local-dev/permissions.js'
import MutableOrgDS from './model/MutableOrganizationDataSource.js'
import UserDataSource from './model/UserDataSource.js'
import BulkImportDataSource from './model/BulkImportDataSource.js'
/**
* Create a GraphQL server
*/
export async function createServer (): Promise<{ app: express.Application, server: ApolloServer }> {
const schema = applyMiddleware(
graphqlSchema,
(process.env.LOCAL_DEV_BYPASS_AUTH === 'true' ? localDevBypassAuthPermissions : permissions).generate(graphqlSchema)
)
const dataSources = ({
climbs: MutableClimbDataSource.getInstance(),
areas: MutableAreaDataSource.getInstance(),
bulkImport: BulkImportDataSource.getInstance(),
organizations: MutableOrgDS.getInstance(),
ticks: TickDataSource.getInstance(),
history: ChangeLogDataSource.getInstance(),
media: MutableMediaDataSource.getInstance(),
users: UserDataSource.getInstance()
})
const app = express()
const httpServer = http.createServer(app)
const server = new ApolloServer({
introspection: true,
schema,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
cache: new InMemoryLRUCache({
max: 50,
maxSize: 1024 * 1024 * 10
})
})
// server must be started before applying middleware
await server.start()
const context = process.env.LOCAL_DEV_BYPASS_AUTH === 'true' ? localDevBypassAuthContext : createContext
app.get('/health', (req, res) => {
const memUsage = process.memoryUsage()
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
memory: {
rss: `${Math.round(memUsage.rss / 1024 / 1024)}MB`,
heapTotal: `${Math.round(memUsage.heapTotal / 1024 / 1024)}MB`,
heapUsed: `${Math.round(memUsage.heapUsed / 1024 / 1024)}MB`,
external: `${Math.round(memUsage.external / 1024 / 1024)}MB`
}
})
})
app.use('/',
bodyParser.json({ limit: '5mb' }),
cors<cors.CorsRequest>(),
express.json(),
expressMiddleware(server, {
context: async ({ req }) => ({ dataSources, ...await context({ req }) })
})
)
await new Promise<void>(resolve => httpServer.listen({ port: 4000 }, resolve))
return { app, server }
}