forked from jedahan/collections-api
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (64 loc) · 1.7 KB
/
server.js
File metadata and controls
76 lines (64 loc) · 1.7 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
'use strict'
var ratelimit, oneDay, oneMonth, oneYear, limit
const koa = require('koa')
const response_time = require('koa-response-time')
const logger = require('koa-logger')
const cors = require('koa-cors')
const etag = require('koa-etag')
const fresh = require('koa-fresh')
const compress = require('koa-compress')
const mask = require('koa-json-mask')
const router = require('koa-router')
const markdown = require('koa-markdown')
const serve = require('koa-static')
const getIds = require('./libs/getIds')
const getObject = require('./libs/getObject')
const app = koa()
console.log('Environment:' + app.env)
var cache = ratelimit = function () {
return function * (next) {
return (yield next)
}
}
if (app.env !== 'development') {
console.log('cache ON')
cache = require('koa-redis-cache')
oneDay = 60 * 60 * 24
oneMonth = oneDay * 30
oneYear = oneDay * 365
console.log('limits ON')
limit = require('koa-better-ratelimit')
ratelimit = function (next) {
return limit({
duration: 1000 * 60,
max: 8
})
}
}
app.use(response_time())
app.use(logger())
app.use(cors())
app.use(etag())
app.use(fresh())
app.use(compress())
app.use(serve('static'))
app.use(mask())
app.use(router(app))
app.get('/', markdown({
baseUrl: '/',
root: __dirname,
indexName: 'Readme'
}))
app.get('/object/:id', cache({
expire: oneYear
}), ratelimit(), getObject)
app.get('/search/:term', cache({
expire: oneMonth
}), ratelimit(), getIds)
app.get('/search', cache({
expire: oneMonth
}), ratelimit(), getIds)
app.get('/random', require('./libs/getRandom'))
app.listen(process.env.PORT || 5000, function () {
return console.log(`[${process.pid}] listening on :${+this._connectionKey.split(':')[2]}`)
})