nci/app.js

157 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-04-10 19:23:52 +00:00
'use strict';
var env = process.env.NODE_ENV || 'development',
db = require('./db'),
http = require('http'),
2015-05-12 21:07:03 +00:00
nodeStatic = require('node-static'),
path = require('path'),
2015-05-18 18:25:08 +00:00
fs = require('fs'),
Steppy = require('twostep').Steppy,
_ = require('underscore'),
2015-05-21 21:09:16 +00:00
reader = require('./lib/reader'),
2015-06-13 18:59:32 +00:00
notifier = require('./lib/notifier'),
2015-06-14 13:40:20 +00:00
project = require('./lib/project'),
2015-07-05 18:03:58 +00:00
libLogger = require('./lib/logger'),
EventEmitter = require('events').EventEmitter;
2015-04-10 19:23:52 +00:00
var app = new EventEmitter(),
logger = libLogger('app'),
httpApi = require('./httpApi')(app);
2015-07-05 18:03:58 +00:00
2015-07-28 20:56:27 +00:00
var staticServer = new nodeStatic.Server(path.join(__dirname, 'static'));
var server = http.createServer(function(req, res) {
if (req.url.indexOf('/api/') === 0) {
return httpApi(req, res);
}
2015-04-10 20:17:03 +00:00
if (req.url.indexOf('/data.io.js') === -1) {
if (/(js|css|fonts)/.test(req.url)) {
2015-04-10 19:23:52 +00:00
staticServer.serve(req, res);
} else {
// serve index for all app pages
if (env === 'development') {
var jade = require('jade');
// Compile a function
var index = jade.compileFile(__dirname + '/views/index.jade');
res.write(index({env: env}));
res.end();
} else {
staticServer.serve(req, res);
}
2015-04-10 19:23:52 +00:00
}
}
});
var socketio = require('socket.io')(server);
var dataio = require('./dataio')(socketio);
2015-04-10 19:23:52 +00:00
app.server = server;
app.dataio = dataio;
2015-04-10 19:23:52 +00:00
2015-05-18 20:27:02 +00:00
app.lib = {};
app.lib.reader = reader;
2015-05-21 21:09:16 +00:00
app.lib.notifier = notifier;
2015-07-05 18:03:58 +00:00
app.lib.logger = libLogger;
2015-05-12 21:07:03 +00:00
2015-07-20 20:57:41 +00:00
var configDefaults = {
2015-07-20 21:01:55 +00:00
notify: {},
2015-08-18 21:29:30 +00:00
http: {host: '127.0.0.1', port: 3000, url: 'http://127.0.0.1:3000'}
2015-07-20 20:57:41 +00:00
};
2015-05-18 18:25:08 +00:00
Steppy(
function() {
app.config = {};
app.config.paths = {};
// path to root dir (with projects, builds etc)
app.config.paths.data = path.join(process.cwd(), 'data');
app.config.paths.projects = path.join(app.config.paths.data, 'projects');
app.config.paths.builds = path.join(app.config.paths.data, 'builds');
2015-07-12 12:14:28 +00:00
app.config.paths.db = path.join(app.config.paths.data, 'db');
app.config.paths.preload = path.join(app.config.paths.data, 'preload.json');
var buildDirExistsCallback = this.slot();
2015-05-18 18:25:08 +00:00
fs.exists(app.config.paths.builds, function(isExists) {
buildDirExistsCallback(null, isExists);
});
var preloadExistsCallback = this.slot();
fs.exists(app.config.paths.preload, function(isExists) {
preloadExistsCallback(null, isExists);
2015-05-18 18:25:08 +00:00
});
},
function(err, isBuildsDirExists, isPreloadExists) {
2015-05-18 18:25:08 +00:00
if (!isBuildsDirExists) {
fs.mkdir(app.config.paths.builds, this.slot());
2015-05-18 20:27:02 +00:00
} else {
this.pass(null);
2015-05-18 18:25:08 +00:00
}
2015-05-12 21:07:03 +00:00
if (isPreloadExists) {
var preload = require(app.config.paths.preload);
// register rc plugins
_(preload.plugins).each(function(plugin) {
logger.log('Preload plugin "%s"', plugin);
require(plugin).register(app);
});
}
2015-05-18 20:27:02 +00:00
reader.load(app.config.paths.data, 'config', this.slot());
},
function(err, mkdirResult, config) {
2015-05-18 18:25:08 +00:00
_(app.config).defaults(config);
2015-07-20 20:57:41 +00:00
_(app.config).defaults(configDefaults);
2015-05-18 18:25:08 +00:00
2015-07-05 18:03:58 +00:00
logger.log('Server config:', JSON.stringify(app.config, null, 4));
2015-05-18 18:25:08 +00:00
2015-07-12 12:14:28 +00:00
db.init(app.config.paths.db, {
db: require(app.config.storage.backend),
valueEncoding: 'json'
}, this.slot());
2015-06-13 18:59:32 +00:00
},
function() {
2015-06-14 13:40:20 +00:00
// load all projects for the first time
2015-06-13 18:59:32 +00:00
project.loadAll(app.config.paths.projects, this.slot());
},
function(err, projects) {
2015-06-14 13:40:20 +00:00
// note that `app.projects` is live variable
2015-06-13 18:59:32 +00:00
app.projects = projects;
2015-07-05 18:03:58 +00:00
logger.log('Loaded projects: ', _(app.projects).pluck('name'));
2015-06-14 13:40:20 +00:00
require('./distributor').init(app, this.slot());
},
function(err, distributor) {
app.distributor = distributor;
// register other plugins
require('./lib/notifier/console').register(app);
_(app.config.plugins).each(function(plugin) {
logger.log('Load plugin "%s"', plugin);
require(plugin).register(app);
});
notifier.init(app.config.notify, this.slot());
require('./projectsWatcher').init(app, this.slot());
2015-05-21 21:09:16 +00:00
2015-07-12 08:39:22 +00:00
require('./scheduler').init(app, this.slot());
// notify about first project loading
_(app.projects).each(function(project) {
app.emit('projectLoaded', project);
});
2015-05-18 20:27:02 +00:00
// init resources
2015-05-18 18:25:08 +00:00
require('./resources')(app);
},
function(err) {
2015-07-20 21:01:55 +00:00
var host = app.config.http.host,
port = app.config.http.port;
logger.log('Start http server on %s:%s', host, port);
app.server.listen(port, host);
},
2015-05-18 18:25:08 +00:00
function(err) {
if (err) throw err;
}
);