-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
58 lines (51 loc) · 1.76 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
48
49
50
51
52
53
54
55
56
57
58
const _ = require("lodash")
const path = require("path");
const env = process.env.NODE_ENV || 'development';
const config = require(`./config/env/${env}.config.json`);
const utilities = require("./src/utilities");
utilities.Registry.set("config", config);
utilities.Registry.set("env", env);
// initializing databases and clients
let mongoConn = (new utilities.DBClient.MongoDB.Client(config.mongo_instances.primary_1, {})).connect();
utilities.Registry.set("mongodb", mongoConn);
// initializing models on the mongodb connection
const schemaList = require("./src/models");
utilities.Registry.set("schemas", schemaList);
let models = {};
_.each(schemaList, (value, key) => {
models[key] = mongoConn.model(key, value.schema);
});
utilities.Registry.set("models", models);
const Koa = require('koa');
const { koaBody } = require('koa-body');
const app = new Koa();
require('koa-qs')(app, 'extended');
// koa body middleware
app.use(koaBody());
// CORS middleware
app.use(async (ctx, next) => {
try {
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', '*');
ctx.set('Access-Control-Allow-Headers', '*');
await next();
} catch (error) {
console.log('Process.Error', error);
ctx.status = error.status || 500;
ctx.body = {
success: false,
message: 'Internal Server error, dev team has been notified. Please try again after sometime!!'
};
ctx.app.emit('error', error);
}
});
// route middleware and initialization
const routesList = require('./src/routes');
_.each(routesList, (router, key) => {
app.use(router.routes());
app.use(router.allowedMethods());
});
let server = app.listen(config.application.port, () => {
console.log(`[Started] Application started listening on port ${config.application.port} mode`);
});
module.exports = server;