-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_server.js
133 lines (110 loc) · 3.75 KB
/
rest_server.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
127
128
129
130
131
132
133
var file_helper = require('./helpers/file');
var express = require('express');
var server = express();
var nohm = require('nohm').Nohm;
var registry = require(__dirname+'/registry');
var RedisSessionStore = require('connect-redis')(express);
var cp = require('child_process');
var fs = require('fs');
// load the tvdb model for global use via the registry
var tvdb = nohm.factory('tvdb', 1, function (err, model) {
registry.tvdb = tvdb;
tvdb.getMirrors(function () {
var timedUpdate = function () {
process.stdout.write('Updating... ');
var update_log = fs.createWriteStream('update.log', {
flags: 'a'
});
update_log.on('error', function (err) {
console.log('Error in update_shows.js', err);
});
var update_process = cp.spawn(__dirname+'/node', [__dirname+'/update_shows.js'], {
env: process.env
});
update_process.stdout.pipe(update_log);
update_process.stderr.pipe(update_log);
update_process.on('exit', function () {
console.log('Done.');
setTimeout(function () {
timedUpdate();
}, registry.config.thetvdb.refresh_timers.data);
});
};
timedUpdate();
});
});
module.exports = server;
if (server.set('env') === 'production' || server.set('env') === 'staging') {
express.logger.token('remote-addr', function(req) {
// x-real-ip from nginx proxy or the default from connect
return req.headers['x-real-ip'] || req.socket && (req.socket.remoteAddress || (req.socket.socket && req.socket.socket.remoteAddress));
});
server.use(express.logger({ immediate: true }));
} else {
server.use(express.logger({ format: 'dev' }));
server.use(express.responseTime());
}
server.use(express.bodyParser());
server.use(express.methodOverride());
server.use(express.cookieParser());
server.use(express.session({
store: new RedisSessionStore({
client: registry.redis_sessions
}),
secret: registry.config.sessions.secret,
cookie: {
secure: (server.set('env') === 'production' || server.set('env') === 'staging')
},
proxy: (server.set('env') === 'production' || server.set('env') === 'staging')
}));
server.use(express.csrf());
server.use(function (req, res, next) {
res.ok = function (data) {
data = data || {};
res.json({result: 'success', data: data});
};
//console.log(req.method, req.url);
req.loaded = {};
req.user = nohm.factory('User');
if ( req.session.userdata && ! isNaN(req.session.userdata.id)) {
req.user.load(req.session.userdata.id, function (err) {
if (err) {
req.session.logged_in = false;
req.session.userdata = undefined;
}
next();
});
} else {
next();
}
});
var controller_files = file_helper.getFiles(__dirname, '/controllers/');
controller_files.forEach(function (val) {
var name = val.match(/^\/controllers\/([\w]*)Controller.js$/)[1];
server.use('/'+name, require(__dirname+val));
});
server.all('*', function (req, res, next) {
var notFoundError = new Error('Resource not available with given METHOD and URL.');
console.log(req.method, req.url);
notFoundError.code = 404;
next(notFoundError);
});
server.use(function (err, req, res, next) {
if (err && err instanceof Error) {
console.log('responding with error: '+err.name);
console.dir(err.message);
var code = err.code || 500;
var data = err.data || {error: {name: err.name, msg: err.message}};
res.json({result: 'error', data: data}, code);
if (['ReferenceError', 'TypeError', 'SyntaxError'].indexOf(err.name) >= 0) {
console.log(err.stack);
}
} else {
console.log('uncaught error');
console.dir(err);
next(err);
}
});
server.on('mount', function (parent){
console.log('mounted REST server');
});