-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
143 lines (127 loc) · 4.29 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const express = require('express')
const morgan = require('morgan')
const responseTime = require('response-time')
const bodyParser = require('body-parser')
const corsMiddleware = require('cors')
const createEndpoints = require('./endpoint')
const createKeyAuth = require('./lib/key-auth')
const createCacheDealer = require('./lib/middleware/cache-dealer')
const createRouteChecker = require('./lib/middleware/route-checker')
const createCacheKey = require('./endpoint/circle/cache-key-adaptor')
const createPostUploader = require('./lib/middleware/post-uploader')
const createPutUploader = require('./lib/middleware/put-uploader')
module.exports = function (serviceLocator, backEndFactory) {
const { config, logger } = serviceLocator
const endpoint = createEndpoints(serviceLocator, backEndFactory)
const cacheDealer = createCacheDealer(config, backEndFactory)
const checkRoute = createRouteChecker(config)
const postUploader = createPostUploader(backEndFactory)
const putUploader = createPutUploader(backEndFactory)
const keyAuth = createKeyAuth(config)
const inDevelopmentMode = serviceLocator.env === 'development'
const logLevel = inDevelopmentMode ? 'dev' : 'combined'
const logOptions = {
stream: {
write: (data) => {
serviceLocator.logger.info((data + '').trim())
}
}
}
const app = express()
app
.disable('x-powered-by')
.use(morgan(logLevel, logOptions))
.use(
responseTime((req, res, time) => {
logger.info({ req: req.url, id: req.requestId }, 'end')
res.setHeader('X-Response-Time', `${time.toFixed(2)}ms`)
})
)
app.use(function (req, res, next) {
req.requestId = +Date.now() + Math.random()
logger.info({ req: req.url, id: req.requestId }, 'start')
res.setHeader('X-Request-Id', req.requestId)
return next()
})
app.use(function (req, res, next) {
res.set('D-Cache', 'MISS')
let closed = false
// res.on('close', function () {
// closed = true
// return next(new Error('Response was closed before end.'))
// })
if (!closed) return next()
})
app.use(
corsMiddleware({
maxAge: 3600,
origins: true,
methods: ['GET', 'PUT', 'POST'],
allowedHeaders: [
'X-Requested-With',
'Accept-Version',
'Content-Type',
'Request-Id',
'X-Api-Version',
'X-Request-Id',
'X-Requested-With',
'X-Darkroom-Key'
]
})
)
app.get('/_health', function (req, res) {
backEndFactory.isHealthy(function (error, healthy) {
if (!error && healthy) {
res.status(200).send('OK')
} else {
if (error) {
logger.error(error, 'health check failed')
}
res.status(500).send('ERROR')
}
})
})
app.get('/_version', endpoint.version)
app.get(
'/circle/*',
checkRoute,
createCacheDealer(config, backEndFactory, createCacheKey),
endpoint.circle
)
app.get('/info/*', checkRoute, cacheDealer, endpoint.info)
app.get('/original/*', checkRoute, endpoint.original)
app.get('/download/*', checkRoute, endpoint.download)
app.get(
'/:width(\\d+)/:height(\\d+)/:mode/*',
checkRoute,
cacheDealer,
endpoint.resize.mode
)
app.get(
'/:width(\\d+)/:height(\\d+)/*',
checkRoute,
cacheDealer,
endpoint.resize.both
)
app.get('/:width(\\d+)/*', checkRoute, cacheDealer, endpoint.resize.width)
app.post('/crop', keyAuth, bodyParser.json(), endpoint.crop)
app.post('/blur', keyAuth, bodyParser.json(), endpoint.blur)
app.get('/', (req, res) => res.sendStatus(418))
app.post('/', keyAuth, postUploader, endpoint.upload)
app.put('/', keyAuth, putUploader, endpoint.upload)
app.delete('/data/:id', keyAuth, endpoint.delete.data)
app.delete('/cache/:id', keyAuth, endpoint.delete.cache)
// This is being removed until a time when the '@clocklimited/darkroom' implementation is
// more streamy or a new version of DR is rolled out.
//server.post('/watermark', restify.bodyParser(), endpoint.watermark)
// eslint-disable-next-line
app.use((error, req, res, next) => {
if (error.statusCode && typeof error.toJSON === 'function') {
// is a RestifyError
return res.status(error.statusCode).json(error.toJSON())
}
logger.error(error, 'Unhandled error')
res.status(500).json(error)
})
return app
}