nci/app.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

2015-04-10 19:23:52 +00:00
'use strict';
2015-05-12 21:07:03 +00:00
var http = require('http'),
nodeStatic = require('node-static'),
jade = require('jade'),
path = require('path'),
2015-05-18 18:25:08 +00:00
fs = require('fs'),
Steppy = require('twostep').Steppy,
_ = require('underscore'),
reader = require('./lib/reader');
2015-04-10 19:23:52 +00:00
var staticServer = new nodeStatic.Server('./static');
var server = http.createServer(function(req, res, next) {
2015-04-10 19:23:52 +00:00
// serve index for all app pages
2015-04-10 20:17:03 +00:00
if (req.url.indexOf('/data.io.js') === -1) {
2015-05-09 20:19:25 +00:00
if (!req.url.match(/(js|css|fonts)/)) {
2015-04-10 20:17:03 +00:00
// Compile a function
var index = jade.compileFile(__dirname + '/views/index.jade');
res.write(index());
res.end();
} else {
2015-04-10 19:23:52 +00:00
staticServer.serve(req, res);
}
}
});
var socketio = require('socket.io')(server);
var dataio = require('./dataio')(socketio);
2015-04-10 19:23:52 +00:00
var app = {
server: server,
dataio: dataio
};
2015-04-10 19:23:52 +00:00
2015-05-12 21:07:03 +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');
var stepCallback = this.slot();
fs.exists(app.config.paths.builds, function(isExists) {
stepCallback(null, isExists);
});
reader.load(app.config.paths.data, 'config', this.slot());
},
function(err, isBuildsDirExists, config) {
if (!isBuildsDirExists) {
fs.mkdir(app.config.paths.builds, this.slot());
}
2015-05-12 21:07:03 +00:00
2015-05-18 18:25:08 +00:00
_(app.config).defaults(config);
console.log('Server config:', JSON.stringify(app.config.nodes, null, 4));
require('./resources')(app);
},
function(err) {
if (err) throw err;
}
);
app.server.listen(3000);