-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (52 loc) · 1.86 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
const express = require('express')
const esClient = require('./lib/elasticsearch/client')
const loadConfig = require('./lib/load-config')
const { preflightCheck } = require('./lib/preflight_check')
const { loadNyplCoreData } = require('./lib/load_nypl_core')
const swaggerDocs = require('./swagger.v1.1.x.json')
const pjson = require('./package.json')
const app = express()
// Tell express to trust x-forwarded-proto and x-forwarded-host headers when
// origin is local. This means req.hostname and req.protocol will
// return the actual host and proto of the original request when forwarded
// by the trusted proxy (Imperva). This is essential for building a valid login
// redirect_uri
// See https://expressjs.com/en/4x/api.html#trust.proxy.options.table
app.set('trust proxy', 'loopback')
app.init = async () => {
await loadConfig.loadConfig()
await loadNyplCoreData()
preflightCheck()
// Load logger after running above to ensure we respect LOG_LEVEL if set
app.logger = require('./lib/logger')
require('./lib/resources')(app)
// routes
require('./routes/resources')(app)
require('./routes/misc')(app)
app.esClient = esClient
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS')
res.header('Access-Control-Allow-Headers', 'Content-Type')
next()
})
app.get('/', function (req, res) {
res.send(pjson.version)
})
// Just testing route
app.get('/api/v0.1/discovery', function (req, res) {
res.send(pjson.version)
})
app.get('/api/v0.1/discovery/swagger', function (req, res) {
res.send(swaggerDocs)
})
return app
}
app.start = async () => {
await app.init()
const port = process.env.PORT || 3000
return app.listen(port, function () {
app.logger.info('Server started on port ' + port)
})
}
module.exports = app