-
Notifications
You must be signed in to change notification settings - Fork 11
/
coregi.js
95 lines (85 loc) · 2.51 KB
/
coregi.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
var async = require('async');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var express = require('express');
var etcd = require('./lib/etcd');
var fleetctl = require('./lib/fleetctl');
var favicon = require('serve-favicon');
var http = require('http');
var log = require('./lib/logger')();
var path = require('path');
var routes = require('./routes');
var coregi = module.exports = {};
coregi.init = function init(config, callback) {
if(!config) {
callback(new Error('Cannot initialize CoreGI. No config provided'));
}
if(!config.fleetctl) {
callback(new Error('Cannot initialize CoreGI. No fleetctl config provided'));
}
coregi.config = config;
log.info({
config: config
}, 'Starting coregi application');
coregi.app = express();
// view engine setup
coregi.app.set('views', path.join(__dirname, 'site/views'));
//app.set('view engine', 'hbs');
coregi.app.set('view engine', 'html');
coregi.app.engine('html', require('hbs').__express);
coregi.app.use(favicon(__dirname + '/site/img/favicon.ico'));
coregi.app.use(bodyParser.json());
coregi.app.use(bodyParser.urlencoded({extended: true}));
coregi.app.use(cookieParser());
coregi.app.use(express.static(path.join(__dirname, 'site')));
// set express port
coregi.app.set('port', process.env.PORT || 3000);
// error handlers
coregi.app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
// express routes
coregi.app.use('/', routes);
// init modules
async.waterfall([
function(callback) {
fleetctl.init(coregi.config.fleetctl, callback);
},
function(callback) {
etcd.init(coregi.config.etcd, callback);
}
], function(err) {
if(err) {
log.error({err: err}, 'Error - CoreGI: init');
callback(new Error({err: err}, 'Error: Starting CoreGI'));
}
else {
callback(null);
}
});
};
coregi.start = function start() {
// load modules
async.waterfall([
function(callback) {
fleetctl.startCron(callback);
},
function(callback) {
etcd.startCron(callback);
},
function(callback) {
http.createServer(coregi.app).listen(coregi.app.get('port'), callback);
},
function(callback) {
log.info({port: coregi.app.get('port')}, 'CoreGI: Express server listening on port '+coregi.app.get('port'));
}
], function(err) {
if(err) {
log.error({err: err}, 'Error - CoreGI: start');
}
});
};