-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
126 lines (103 loc) · 5 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
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
121
122
123
124
125
126
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var fs = require('fs');
/*********************************************************************************************************/
// Load Config and do some things
/*********************************************************************************************************/
var config_default = JSON.parse(fs.readFileSync('config.default').toString());
var config_local = {};
try {
config_local = JSON.parse(fs.readFileSync('config.local').toString())
} catch(e) {
console.log('Failed trying to load Local config data - using default instead');
}
// Need to use prototype and fix this ugly mess eventually
var CONFIG = {};
CONFIG.mongo_server = noe(coalesce(coalesce(config_local, 'mongo', {}), 'server', '')) ? config_default.mongo.server : config_local.mongo.server;
CONFIG.mongo_port = noe(coalesce(coalesce(config_local, 'mongo', {}), 'port', '')) ? config_default.mongo.port : config_local.mongo.port;
CONFIG.server_port = noe(coalesce(coalesce(config_local, 'server', {}), 'port', '')) ? config_default.server.port : config_local.server.port;
CONFIG.local_server = noe(coalesce(coalesce(config_local, 'local', {}), 'server', '')) ? config_default.local.server : config_local.local.server;
CONFIG.local_port = noe(coalesce(coalesce(config_local, 'local', {}), 'port', '')) ? config_default.local.port : config_local.local.port;
CONFIG.logs_save = noe(coalesce(coalesce(config_local, 'logs', {}), 'save', '')) ? config_default.logs.save : config_local.logs.save;
CONFIG.logs_path = noe(coalesce(coalesce(config_local, 'logs', {}), 'path', '')) ? config_default.logs.path : config_local.logs.path;
CONFIG.log_retain = noe(coalesce(coalesce(config_local, 'logs', {}), 'retain', '')) ? config_default.logs.retain : config_local.logs.retain;
CONFIG.account_create = noe(coalesce(coalesce(config_local, 'account', {}), 'create', '')) ? config_default.account.create : config_local.account.create;
fs.writeFileSync('./public/javascripts/config.js',
'var CONFIG = ' + JSON.stringify({
server: CONFIG.local_server,
account_create: CONFIG.account_create,
log_retain: CONFIG.log_retain
}
));
/*********************************************************************************************************/
var mongoose = require('mongoose');
var accountAPI = require('./routes/account');
var projects = require('./routes/projects');
var project_log = require('./routes/project_log');
var groups = require('./routes/group');
var app = express();
mongoose.connect('mongodb://' + CONFIG.mongo_server + '/vishwakarma');
// all environments
app.set('port', process.env.NODE_PORT || CONFIG.server_port || 80);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session({
secret: 'keyboard cat'
}));
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function (req, resp) {
resp.sendfile(__dirname + '/public/vishwakarma.html')
});
app.set('account_create', CONFIG.account_create);
app.post('/login', accountAPI.login);
app.get('/authenticated', accountAPI.authenticated);
app.get('/logout', accountAPI.logout);
app.post('/accounts/register', accountAPI.register);
app.post('/accounts/reset', accountAPI.reset_password);
app.get('/users', accountAPI.get);
app.get('/:username/projects', projects.get);
app.get('/projects/:id', projects.get_project);
app.post('/projects/save', projects.save);
app.del('/projects/:_id/remove', projects.remove);
app.get('/projects/:project/groups', groups.get_groups_for_project);
app.post('/projects/project/groups/add', groups.add_groups_to_project);
app.get('/logs', project_log.get);
app.get('/logs/:id', project_log.get_log);
app.get('/groups', groups.get);
app.post('/groups/save', groups.save);
app.get('/groups/:group/users', groups.get_users_for_group);
app.post('/groups/users/add', groups.add_users_to_group);
/*******************/
var socket_config = {
logs_save: CONFIG.logs_save,
logs_path: CONFIG.logs_path
};
var server = http.createServer(app);
var socketServer = require('./socket_server.js')(server, socket_config);
server.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
// helper functions
function coalesce(obj, key, default_value) {
return obj.hasOwnProperty(key) ? obj[key] : default_value;
}
function noe(i) {
return [undefined, null, ''].indexOf(i) > -1;
}