-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (39 loc) · 1.26 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
require('dotenv').config();
const Koa = require('koa');
const logger = require('koa-logger');
const bodyParser = require('koa-bodyparser');
const jwt = require('koa-jwt');
const cors = require('@koa/cors');
const http = require('http');
const SocketIO = require('socket.io');
const router = require('./routes');
const db = require('./db');
const sccpClient = require('./sccpClient');
const io = require('./io');
const notifications = require('./notifications');
const port = process.env.API_PORT || process.env.PORT || 3500;
const secret = process.env.SECRET || 'averyveryverysecretsecret';
// Koa application Setup
const app = new Koa();
// Middleware Setup
app.use(logger());
app.use(bodyParser());
app.use(cors({ credentials: true }));
app.use(jwt({ secret }).unless({ path: [/^\/api\/(login|singup|backdoor)/] }));
// Router Setup
app.use(router.routes());
app.use(router.allowedMethods());
// SocketIO Setup
const server = http.Server(app.callback());
io.initialize(SocketIO(server));
// Notifications Setup
notifications.initialize();
async function main() {
await db.initialize();
await sccpClient.initialize();
server.listen(port);
console.log(`Server running at port: ${port}\n`); // eslint-disable-line no-console
}
main().catch((error) => {
throw error;
});