-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
78 lines (61 loc) · 1.97 KB
/
app.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
// configure the environment variables
require('dotenv').config()
global.__basedir = __dirname
// set the port
const PORT = process.env.PORT
// for async errors
import 'express-async-errors'
// bring in the database connection
import db from './src/models'
// swagger
import swaggerUI from 'swagger-ui-express'
import YAML from 'yamljs'
const swaggerDocument = YAML.load('./swagger.yaml')
// initialize the express app
import express, {Application, Request, Response} from 'express'
const app : Application = express()
// logger
import log from './src/utils/logger'
// security packages
import helmet from 'helmet'
import cors from 'cors'
const xss = require('xss-clean')
import rateLimit from 'express-rate-limit'
// import middlewares
import Routes from './src/routes'
import {errorHandlerMiddleware} from './src/middleware/error-handler'
import {notFound} from './src/middleware/not-found'
import deserializeUser from './src/middleware/deserialize-user'
// security middlewares
app.set("trust proxy", 1)
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(helmet())
app.use(cors())
app.use(xss())
app.use(rateLimit({ windowMs: 60 * 1000, max: 60 }))
// deserialize user
app.use(deserializeUser)
// documentation route
app.get('/', (req: Request, res: Response) => {
res.send('<h1>Welcome to the twitter gram documentation</h1><p>click <a href="/api-docs">here</a> to visit the docs</p>')
})
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument))
// app routes
app.use('/api/v1', Routes)
// not found middleware
app.use(notFound)
// error handler middleware
app.use(errorHandlerMiddleware)
// start the express app
const start = async () => {
try {
await db.sequelize.authenticate()
log.info("connected to the database")
app.listen(PORT, () => log.info(`Server started on port ${process.env.PORT}`))
} catch (err) {
log.error("could not connect to the database", err)
process.exit(1)
}
}
start()