This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
89 lines (71 loc) · 2.42 KB
/
server.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
import SwaggerExpress from 'swagger-express-mw'
import SwaggerUi from 'swagger-tools/middleware/swagger-ui'
import volosCache from 'volos-cache-memory'
import compression from 'compression'
import express from 'express'
import deepcopy from 'deepcopy'
import * as auth from './api/helpers/auth'
const VOLOS_RESOURCE = 'x-volos-resources'
import requestIp from 'request-ip'
import * as logger from './api/helpers/logger'
const config = {
appRoot: __dirname,
port: process.env.PORT || 8000,
swaggerSecurityHandlers: {Bearer: auth.verifyToken}
}
console.log(process.version);
const app = express()
app.use(compression())
app.use(requestIp.mw())
app.use(logger.logRequest)
SwaggerExpress.create(config, (err, swaggerExpress) => {
if (err) {
throw err
}
let cacheOptions = swaggerExpress.runner.swagger[VOLOS_RESOURCE].cache.options
let cacheName = swaggerExpress.runner.swagger[VOLOS_RESOURCE].cache.name
let cache = volosCache.create(cacheName, cacheOptions)
cacheOptions.key = getCacheKey
// eliminate path that has x-hide property
let swaggerObject = deepcopy(swaggerExpress.runner.swagger)
let new_paths = Object.keys(swaggerObject.paths).reduce((obj, path) => {
if (!swaggerObject.paths[path]['x-hide']) {
obj[path] = swaggerObject.paths[path]
}
return obj
}, {})
swaggerObject.paths = new_paths
app.use(SwaggerUi(swaggerObject))
// Serve the Swagger documents and Swagger UI
// app.use(swaggerExpress.runner.swaggerTools.swaggerUi());
// install middleware
app.use(swaggerExpress.runner.swaggerTools.swaggerMetadata())
app.use(swaggerExpress.runner.swaggerTools.swaggerSecurity(config.swaggerSecurityHandlers))
app.use(cache.expressMiddleware().cache({key: getCacheKey}))
// Provide the security handlers
swaggerExpress.register(app)
app.listen(config.port, () => {
console.log(`Open maps API is up on http://localhost:${config.port}`)
})
})
/**
* Extracts and returns data from shapefiles
* @param {object} req request object
* @return {sting} cacheKey
*/
const getCacheKey = (req) => {
let cacheKey = null
let url = req.originalUrl
if (url.indexOf('/api/v1/') !== -1) {
if (url.substring(url.length - 1) === '/') {
url = url.substring(0, url.length - 1)
}
let urlParts = url.split('/')
cacheKey = urlParts[3]
urlParts.slice(4, urlParts.length).forEach(part => {
cacheKey += '_' + part
})
}
return cacheKey
}
export default app