Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (73 loc) · 2.88 KB
/
Copy pathindex.js
File metadata and controls
88 lines (73 loc) · 2.88 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
87
88
'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 allowedOrigins = [
/^https?:\/\/(?:.*\.)?topcoder\.com$/i,
/^https?:\/\/(?:.*\.)?topcoder-dev\.com$/i
]
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}))
// Handle CORS for trusted Topcoder domains
app.use((req, res, next) => {
const origin = req.headers.origin
if (origin && allowedOrigins.some(pattern => pattern.test(origin))) {
res.setHeader('Access-Control-Allow-Origin', origin)
res.setHeader('Access-Control-Allow-Credentials', 'true')
res.setHeader('Vary', 'Origin')
}
const requestHeaders = req.headers['access-control-request-headers']
if (requestHeaders) {
res.setHeader('Access-Control-Allow-Headers', requestHeaders)
} else {
res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type, Content-Length, X-Requested-With')
}
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
if (req.method === 'OPTIONS') {
res.writeHead(204)
res.end()
return
}
next()
})
// 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`)
})
})
})