-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (91 loc) · 3.27 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
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
115
116
117
118
119
120
"use strict";
/** --------------------------START MODULE DEPENDENCIES ------------------------------*/
const config = require('./config');
const restify = require('restify');
const mongoose = require('mongoose');
const morgan = require('morgan');
const path = require('path');
const fs = require('fs');
const rfs = require('rotating-file-stream');
const corsMiddleware = require('restify-cors-middleware');
const log = require('./logger').log;
const filterJsonRequest = require('./src/middlewares/filterJsonRequest');
const filterUnauthorizedRequest = require('./src/middlewares/filterUnauthorizedRequest');
/** ---------------------------END MODULE DEPENDENCIES -------------------------------*/
/** --------------------------START ROUTERS -----------------------------------------*/
const userRouter = require('./src/routes/userRouter');
const utilRouter = require('./src/routes/utilRouter');
const authenRouter = require('./src/routes/authenRouter');
const adminRouter = require('./src/routes/adminRouter');
const rootRouter = require('./src/routes/rootRouter');
/** ---------------------------END ROUTERS -----------------------------------------*/
global.__basedir = config.root_dir;
const MAX_UPLOAD_FILE_SIZE = 4 * 1024 * 1024 * 1024;
/**
* Initialize Server HTTP
*/
const server = restify.createServer({
name: config.name,
// version: config.version,
log: log
});
/** ---------------------------START MIDDLEWARE ----------------------------------*/
const cors = corsMiddleware({
preflightMaxAge: 5, //Optional
origins: config.cors,
allowHeaders: ['API-Token', 'Authorization'],
exposeHeaders: ['API-Token-Expiry']
});
server.pre(cors.preflight);
server.use(cors.actual);
server.use(restify.plugins.bodyParser({
maxBodySize: MAX_UPLOAD_FILE_SIZE,
maxFileSize: MAX_UPLOAD_FILE_SIZE
}));
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser({
mapParams: true
}));
server.use(restify.plugins.fullResponse());
let logDirectory = path.join(__dirname, 'logs');
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
let accessLogStream = rfs('access.log', {
interval: '1d', // rotate daily
path: logDirectory
});
// const accessLogStream = fs.createWriteStream(path.join(__dirname, 'logs/access.log'), { flags: 'a' });
server.use(morgan('combined', {
stream: accessLogStream
}));
server.use(filterUnauthorizedRequest.unless({
path: config.public_url
}));
server.use(filterJsonRequest);
/** -----------------------------END MIDDLEWARE -----------------------------------*/
/**
* Start Server HTTP, Connect to DB & Require Routes
*/
server.listen(config.port, () => {
// establish connection to mongodb
mongoose.Promise = global.Promise;
if (config.db.authorizationEnabled) {
mongoose.connect(config.db.uri, {
auth: config.db.auth
});
} else {
mongoose.connect(config.db.uri);
}
const db = mongoose.connection;
db.on('error', err => {
log.error(err);
process.exit(1);
});
db.once('open', () => {
userRouter(server);
// utilRouter(server);
authenRouter(server);
rootRouter(server);
adminRouter(server);
console.log(`Server is listening on port ${config.port}`);
})
});