forked from experiencedevops/nodemongoreact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (74 loc) · 2.13 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
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
'use strict'
//require('dotenv-extended').load();
/**
* Module Dependencies
*/
const restify = require('restify'),
bunyan = require('bunyan'),
winston = require('winston'),
bunyanWinston = require('bunyan-winston-adapter'),
MongoClient = require('mongodb').MongoClient
/**
* Logging
*/
const tsFormat = () => new Date().toString();
global.log = new winston.Logger({
transports: [
new winston.transports.Console({
level: 'info',
timestamp: tsFormat,
json: true,
colorize:true,
}),
new winston.transports.File({
filename: 'logs/access.log',
timestamp: tsFormat,
level: process.env.env === 'development' ? 'silly' : 'info',
json: true
})
],
})
/**
* Initialize Server
*/
global.server = restify.createServer({
name : "Experience Devops",
version : "0.0.1",
log : bunyanWinston.createAdapter(log),
})
/**
* Middleware
*/
server.use(restify.jsonBodyParser({ mapParams: true }))
server.use(restify.acceptParser(server.acceptable))
server.use(restify.queryParser({ mapParams: true }))
server.use(restify.CORS())
server.use(restify.fullResponse())
server.use(restify.authorizationParser())
restify.CORS.ALLOW_HEADERS.push('authorization');
restify.CORS.ALLOW_HEADERS.push('x-access-token');
/**
* Error Handling
*/
server.on('uncaughtException', (req, res, route, err) => {
log.error(err.stack)
res.send(err)
});
/**
* Lift Server, Connect to DB & Bind Routes
*/
server.listen(4000, function() {
//TODO - MONGODB AUTH AND Write Concern
MongoClient.connect('mongodb://mongodb:27017/experiencedevops', (err, database) => {
if(err) {
log.error('MongoDB default connection error: ' + err)
process.exit(1)
}
log.info(
'Server ready to accept connections on port %s ',
process.env.PORT
)
global.db = database
})
require('./routes')
})