-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.ts
196 lines (160 loc) · 6.53 KB
/
application.ts
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
import 'reflect-metadata'
import { ConnectionManager, useContainer as typeormUseContainer } from 'typeorm'
import { Container } from 'typedi'
import config from './configuration'
import express, { Application } from 'express'
import LoggerInterface from './services/logger/LoggerInterface'
import JwtTokenStrategyFactory from './services/token/strategy/JwtTokenStrategyFactory'
import TokenStrategyInterface from './services/token/strategy/TokenStrategyInterface'
import { MongoConnectionOptions } from 'typeorm/driver/mongodb/MongoConnectionOptions'
import { MongoDriver } from 'typeorm/driver/mongodb/MongoDriver'
import NullLogger from './services/logger/NullLogger'
import ClassValidatorValidator from './services/validator/ClassValidatorValidator'
import WinstonLogger from './services/logger/WinstonLogger'
import { createServer, Server } from 'http'
import { ApolloServer } from '@apollo/server'
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'
import { expressMiddleware } from '@apollo/server/express4'
import { buildSchema } from 'type-graphql'
import { resolvers } from './resolvers'
import { scalars } from './scalars'
import { errors } from './errors'
import { subscribers } from './subscribers'
import Context from './context/Context'
import { entities } from './entities'
import { AuthCheckerService } from './services/auth/AuthCheckerService'
import { GraphQLError } from 'graphql/error'
import type { GraphQLFormattedError } from 'graphql/index'
import cors from 'cors'
import compression from 'compression'
import morgan from 'morgan'
import { DataSource } from 'typeorm/data-source/DataSource'
const serverlessExpress = require('@vendia/serverless-express')
typeormUseContainer(Container)
Container.set('env', config.env)
const loggerFormat = config.logger.format
Container.set('loggerFormat', loggerFormat)
Container.set('loggerLevel', config.logger.level)
Container.set('authPermissions', config.auth.permissions)
Container.set('validatorOptions', config.validator)
const logger: LoggerInterface = config.logger.enabled ? Container.get<WinstonLogger>(WinstonLogger) : new NullLogger()
Container.set('logger', logger)
Container.set('validator', Container.get<ClassValidatorValidator>(ClassValidatorValidator))
const projectDir = config.projectDir
const mongoLogging = config.db.type === 'mongodb' && config.db.logging
const connectionManager = new ConnectionManager()
Container.set(ConnectionManager, connectionManager)
const tokenStrategy: TokenStrategyInterface = Container.get<JwtTokenStrategyFactory>(JwtTokenStrategyFactory).create(config.jwt)
Container.set('tokenStrategy', tokenStrategy)
const dataSourceOptions: MongoConnectionOptions = {
type: config.db.type,
url: config.db.url,
synchronize: config.db.synchronize,
logging: config.db.logging,
entities,
subscribers,
monitorCommands: mongoLogging,
}
const db = connectionManager.create(dataSourceOptions)
const authChecker = Container.get<AuthCheckerService>(AuthCheckerService)
const buildApolloServer = async (server: Server = undefined): Promise<ApolloServer> => {
const schema = await buildSchema({
// @ts-ignore
resolvers,
scalarsMap: scalars,
container: Container,
authChecker: authChecker.getTypeGraphqlAuthChecker(),
emitSchemaFile: `${ projectDir }/schema.graphql`,
})
const plugins = []
if (server) {
plugins.push(ApolloServerPluginDrainHttpServer({ httpServer: server }))
}
return new ApolloServer<Context>({
schema,
plugins,
formatError: (formattedError: GraphQLFormattedError, error: GraphQLError) => {
for (const name in errors) {
for (const key of [ error.originalError.constructor.name, formattedError.extensions.code as string ]) {
if (key && errors[name].types.includes(key)) {
return { ...formattedError, extensions: { name, code: errors[name].code } }
}
}
}
return formattedError
},
})
}
const prepareExpress = async (app: Application, apolloServer: ApolloServer): Promise<Application> => {
app.use(morgan(loggerFormat, { stream: { write: logger.info.bind(logger) } }))
app.use(cors({ origin: config.client_url }))
app.use(express.json())
app.use(compression())
app.use(expressMiddleware(apolloServer, {
context: async ({ req }) => {
return {
user: await authChecker.getApolloContextUser(req),
}
},
}))
return app
}
const initializeDb = async (db: DataSource): Promise<void> => {
await db.initialize()
if (mongoLogging) {
const conn = (db.driver as MongoDriver).queryRunner.databaseConnection
conn.on('commandStarted', (event) => logger.debug('commandStarted', event))
conn.on('commandSucceeded', (event) => logger.debug('commandSucceeded', event))
conn.on('commandFailed', (event) => logger.error('commandFailed', event))
}
}
export const serverUp = async (): Promise<void> => {
const app = express()
const server = createServer(app)
await initializeDb(db)
const apolloServer = await buildApolloServer(server)
await apolloServer.start()
await prepareExpress(app, apolloServer)
const port = config.app.port
server.listen({ port }, () => logger.info(`Server is running on port ${ port }`))
const failureHandler = (error: string) => {
logger.error(error)
serverDown(server, () => process.exit(1))
}
process.on('uncaughtException', failureHandler)
process.on('unhandledRejection', failureHandler)
const successHandler = () => {
logger.info('SIGTERM received')
serverDown(server, () => process.exit(0))
}
process.on('SIGTERM', successHandler)
}
export const serverDown = async (server: Server, callback: () => {}): Promise<void> => {
server.close(() => {
logger.info('Server closed')
db.destroy().then(() => {
logger.info('Database connection closed')
callback && callback()
})
})
}
export const testServerUp = async (): Promise<Application> => {
const app = express()
await initializeDb(db)
const apolloServer = await buildApolloServer()
await apolloServer.start()
await prepareExpress(app, apolloServer)
return app
}
export const testServerDown = async (): Promise<void> => {
db.destroy().then(() => logger.info('Database connection closed'))
}
export const serverless = async (): Promise<Function> => {
const app = express()
await initializeDb(db)
const apolloServer = await buildApolloServer()
apolloServer.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests()
await prepareExpress(app, apolloServer)
// todo: add process signals processing/handlers
return serverlessExpress({ app })
}