-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
184 lines (154 loc) · 4.66 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* app.js -- application setup
* Copyright 2014 Sergei Ianovich
*
* Licensed under AGPL-3.0 or later, see LICENSE
* Process Control Service Web Interface
*/
var path = require('path');
var express = require('express');
var logger = require('morgan');
var bodyParser = require('body-parser');
var method_override = require('method-override');
var csrf = require('csurf');
var mongoose = require('mongoose');
var fs = require('fs');
var clientSessions = require("client-sessions");
var devices = require('./routes/device');
var sites = require('./routes/site');
var systems = require('./routes/system');
var users = require('./routes/user');
var sessions = require('./routes/session');
var auth = require('./routes/_auth');
var authUser = auth.authenticate;
var states = require('./routes/state');
var User = require('./models/user');
var config = require('./config');
var app = express();
var i18n = require('i18n-t')({ supported_languages: ['ru'] });
app.disable('x-powered-by');
app.set('port', config.port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
if (process.env.NODE_ENV === 'production') {
var out = fs.createWriteStream(config.log, {flags: 'a'});
var err = fs.createWriteStream(config.log, {flags: 'a'});
var stdout = process.stdout;
var stderr = process.stderr;
process.__defineGetter__("stdout", function () {
return out;
});
process.__defineGetter__("stderr", function () {
return err;
});
app.use(logger('short'));
} else if (process.env.NODE_ENV !== 'test') {
app.use(logger('dev'));
}
app.use(express.static(path.join(__dirname, 'app/')));
app.use('/states', states.create);
app.use(bodyParser());
app.use(method_override());
app.use(clientSessions({
secret: config.secret,
cookieName: 'session',
}));
app.use(csrf());
app.use(function (req, res, next) {
res.locals.csrf = req.csrfToken();
res.cookie('XSRF-TOKEN', res.locals.csrf);
next();
});
app.use(function (req, res, next) {
res.locals.e = function (path) {
if (res.locals.err && res.locals.err.errors)
return res.locals.err.errors[path];
};
next();
});
app.use(function (req, res, next) {
res.locals.messages = req.session.messages || [];
req.session.messages = [];
next();
});
app.use(function (req, res, next) {
res.json_ng = function (obj) {
var app = this.app;
var replacer = app.get('json replacer');
var spaces = app.get('json spaces');
var body = ")]}',\n" + JSON.stringify(obj, replacer, spaces);
this.set('Content-Type', 'application/json');
this.send(body)
}
next();
});
app.use(i18n.express());
if (process.env.NODE_ENV === 'test')
app.i18n = i18n.request();
app.use(function (req, res, next) {
req.root = User.createRoot(config.saEmail, req.t('user.root'), config.saPasswordHash);
next();
});
app.get('/signin', sessions.new);
app.post('/signin', sessions.create);
app.del('/signout', sessions.destroy);
app.param('user', users.load);
app.get('/users', users.index);
app.get('/signup', users.new);
app.get('/users/:user', users.show);
app.post('/users/:user', users.update);
app.post('/users', users.create);
app.param('device', devices.load);
app.get('/devices', devices.index);
app.get('/devices/:device', devices.show);
app.post('/devices/:device', devices.update);
app.post('/devices', devices.create);
app.get('/devices/:device/setpoints', systems.setpoints);
app.get('/devices/:device/states', states.index);
app.param('site', sites.load);
app.get('/sites', sites.index);
app.get('/sites/:site', sites.show);
app.post('/sites/:site', sites.update);
app.post('/sites', sites.create);
app.param('system', systems.load);
app.get('/sites/:site/systems', systems.index);
app.get('/sites/:site/systems/:system', systems.show);
app.post('/sites/:site/systems/:system', systems.update);
app.post('/sites/:site/systems', systems.create);
app.get('/', authUser, function(req, res) {
var title = 'asutp.io';
if (req.operator)
title = req.operator.name;
res.render('index', {
pretty: true,
title: title,
operator: req.operator,
});
});
app.use(function (err, req, res, next) {
if (err) {
res.send(err.status || 500);
if (!process.env.NODE_ENV)
throw err;
} else {
next();
}
});
var t;
mongoose.connect(config.dbUrl);
mongoose.connection.on('error', function (e) {
console.error(e);
if (t) return;
t = setTimeout(function () {
t = null;
mongoose.connect(config.dbUrl);
}, 10000);
})
mongoose.connection.on('connected', function () {
if (process.env.NODE_ENV !== 'test') {
console.log('Connected to mongodb at "' + config.dbUrl +'"');
}
})
process.on('exit', function() {
mongoose.connection.close();
});
module.exports = app;