-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
114 lines (101 loc) · 2.76 KB
/
server.js
File metadata and controls
114 lines (101 loc) · 2.76 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
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
const Koa = require("koa");
const config = require("./config");
const mongoose = require("mongoose");
const cors = require("koa-cors");
const koaSwagger = require("koa2-swagger-ui").koaSwagger;
const yamljs = require("yamljs");
const path = require("path");
const fs = require("fs");
const router = require("./routing/router.js");
const deviceManager = require("./createDevices");
const niclaDevice = require("./deviceSchemas/nicla").device;
const bleNanoDeivce = require("./deviceSchemas/bleNano").device;
const seeedDevice = require("./deviceSchemas/seeed").device;
const openEarable_v13 = require("./deviceSchemas/openEarable_v1.3.0.js").device;
const bleNanoV2 = require("./deviceSchemas/bleNanoV2.js").device;
const {MQ} = require("./messageBroker/publisher")
const logger = require('koa-logger');
// create server
const server = new Koa();
server.use(logger())
// connect to Mongo
mongoose.connect(config.DATABASE_URI + config.DB_COLLECTION_BACKEND, {
useNewUrlParser: true,
}).catch(err => {
console.log("Could not connect to mongodb");
process.exit(1);
})
// Connect to RabbitMQ
console.log("Connecting to RabbitMQ...")
MQ.init()
.then(() => console.log("Init RabiitMQ successful"))
.catch((err) => {
console.log("Could not connect to RabiitMQ.");
process.exit(1);
});
deviceManager
.clearDevices()
.then(() => {
Promise.all(
[deviceManager.addDevice(niclaDevice)],
deviceManager.addDevice(bleNanoDeivce),
deviceManager.addDevice(seeedDevice),
deviceManager.addDevice(openEarable_v13),
[deviceManager.addDevice(bleNanoV2)]
).then(() => {
console.log("Added devices");
});
})
.catch((err) => {
console.log(err);
process.exit();
});
// setup koa middlewares
server.use(cors(
{
origin: config.HOST,
credentials: true
}
));
const spec = yamljs.load("./docs/docs.yaml");
server.use(
koaSwagger({
routePrefix: "/docs",
title: "Explorer",
swaggerOptions: { spec },
favicon: "/api/docs/favicon.ico",
hideTopbar: true,
})
);
server.use((ctx, next) => {
if (
ctx.path == "/api/docs/favicon.ico" &&
ctx.method == "GET" &&
ctx.method != "Head"
) {
ctx.body = fs.readFileSync(path.join(__dirname, "/docs/favicon.ico"));
ctx.status = 200;
return ctx;
}
return next();
});
// catch errors
server.use(async (ctx, next) => {
try {
await next();
} catch (error) {
console.log(error);
ctx.body = { error: error.message };
ctx.status = error.status || 500;
}
});
// routing
server.use(router.routes());
// catch all middleware, only land here
// if no other routing rules match
// make sure it is added after everything else
server.use((ctx) => {
ctx.body = { error: "Not Found" };
ctx.status = 404;
});
module.exports = server.listen(3001);