-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (57 loc) · 1.64 KB
/
index.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
'use strict'
var http = require('http')
var koa = require('koa')
var logger = require('koa-logger')
var bodyParser = require('koa-bodyparser')
var compress = require('koa-compress')
var cluster = require('cluster')
var auth = require('./auth')
var config = require('./config')
var entities = require('./entities/api')
var mqtt = require('./mqtt')
// display only once, regardless of clustering
if (cluster.isMaster) {
console.dir(config)
if (!config.SECRET) {
console.warn('! No `SECRET` specified! Running in unsecured mode ...')
}
}
if (config.CLUSTERED && cluster.isMaster) {
let numCpus = require('os').cpus().length
for (let i = 0; i < numCpus; i++) {
cluster.fork()
}
} else {
const app = koa()
app.use(logger())
app.use(compress({
threshold: 1024
}))
app.use(function * (next) {
this.isPrivilegedRequest = this.request.method !== 'GET'
this.hasSecretPrivilege = !config.SECRET || config.SECRET === this.request.headers.secret
if (this.isPrivilegedRequest && !this.hasSecretPrivilege) {
this.throw(401)
} else {
yield next
}
})
app.use(function * (next) {
if (!this.hasSecretPrivilege) {
this.user = yield auth(this.request.headers)
if (!this.user) {
this.throw(401)
}
}
yield next
})
app.use(bodyParser())
app.use(entities.routes())
let server = module.exports = http.createServer(app.callback())
mqtt.init(server, () => {
console.log(`* MQTT Broker listening on port ${config.MQTT_PORT} ...`)
})
server.listen(config.API_PORT, () => {
console.log(`* DOCumenT ORiented REST Api started on port ${config.API_PORT} ...`)
})
}