-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
60 lines (47 loc) · 1.97 KB
/
index.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
'use strict'
const fs = require('fs')
const path = require('path')
const http = require('http')
const config = require('config')
const app = require('connect')()
const bodyParser = require('body-parser')
const swaggerTools = require('swagger-tools')
const jsyaml = require('js-yaml')
const MessageBusService = require('./service/MessageBusService')
const logger = require('./common/logger')
const AuthService = require('./service/AuthService')
const serverPort = config.PORT
// swaggerRouter configuration
const options = {
swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: path.join(__dirname, './controllers'),
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
}
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
const spec = fs.readFileSync(path.join(__dirname, 'api/swagger.yaml'), 'utf8')
const swaggerDoc = jsyaml.safeLoad(spec)
// Extending payload size
app.use(bodyParser.json({limit: '2mb', extended: true}))
app.use(bodyParser.urlencoded({limit: '2mb', extended: true}))
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata())
// Authentication
app.use(middleware.swaggerSecurity({
Bearer: AuthService()
}))
// Validate Swagger requests
app.use(middleware.swaggerValidator())
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options))
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi())
MessageBusService.init()
.then(() => {
http.createServer(app).listen(serverPort, function () {
logger.info(`Your server is listening on port ${serverPort} (http://localhost:${serverPort})`)
logger.info(`Swagger-ui is available on http://localhost:${serverPort}/docs`)
})
})
})