-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
executable file
·114 lines (105 loc) · 3.64 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
113
114
/**
* The application entry point
*/
require('./src/bootstrap')
const config = require('config')
const express = require('express')
const cross = require('cors')
const bodyParser = require('body-parser')
const _ = require('lodash')
const http = require('http')
const swaggerUi = require('swagger-ui-express')
const jsyaml = require('js-yaml')
const fs = require('fs')
const path = require('path')
const logger = require('./src/common/logger')
const errorMiddleware = require('./src/common/error.middleware')
const routes = require('./src/route')
const authenticator = require('tc-core-library-js').middleware.jwtAuthenticator
const errors = require('./src/common/errors')
const app = express()
const httpServer = http.Server(app)
const { checkIfExists } = require('./src/common/helper')
const models = require('./src/models')
app.set('port', config.PORT)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cross())
const apiRouter = express.Router({})
// load all routes
_.each(routes, (verbs, url) => {
_.each(verbs, (def, verb) => {
const actions = []
const { method } = def
if (!method) {
throw new Error(`${verb.toUpperCase()} ${url} method is undefined`)
}
let access = []
// Authentication and Authorization
if (def.auth) {
// default access roles
access = def.access || []
actions.push((req, res, next) => {
authenticator(_.pick(config, ['AUTH_SECRET', 'VALID_ISSUERS']))(req, res, next)
})
actions.push((req, res, next) => {
if (!req.authUser) {
return next(errors.newAuthError('Action is not allowed for invalid token'))
}
req.auth = req.authUser
if (req.authUser.roles) {
// all access are allowed
if (_.isEmpty(access)) {
next()
} else if (!checkIfExists(access, req.authUser.roles)) {
res.forbidden = true
next(errors.newPermissionError('You are not allowed to perform this action'))
} else {
next()
}
} else if (req.authUser.scopes) {
if (_.isNil(def.scopes) || _.isEmpty(def.scopes)) {
next()
} else if (!checkIfExists(def.scopes, req.authUser.scopes)) {
next(errors.newPermissionError('You are not allowed to perform this action!'))
} else {
next()
}
} else if ((_.isArray(def.access) && def.access.length > 0) || (_.isArray(def.scopes) && def.scopes.length > 0)) {
next(errors.newAuthError('You are not authorized to perform this action'))
} else {
next()
}
})
}
actions.push(async (req, res, next) => {
try {
await method(req, res, next)
} catch (e) {
next(e)
}
})
logger.info(`Endpoint discovered : [${access}] ${verb.toLocaleUpperCase()} /${config.API_VERSION}${url}`)
apiRouter[verb](`/${config.API_VERSION}${url}`, actions)
})
})
app.use('/', apiRouter)
const spec = fs.readFileSync(path.join(__dirname, 'docs/swagger.yaml'), 'utf8')
const swaggerDoc = jsyaml.safeLoad(spec)
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc))
app.use(errorMiddleware())
app.use('*', (req, res) => {
const pathKey = req.baseUrl.substring(config.API_VERSION.length + 1)
const route = routes[pathKey]
if (route) {
res.status(405).json({ message: 'The requested method is not supported.' })
} else {
res.status(404).json({ message: 'The requested resource cannot found.' })
}
});
(async () => {
await models.init()
httpServer.listen(app.get('port'), () => {
logger.info(`Express server listening on port ${app.get('port')}`)
})
})()