forked from bdotdub/hummingbird
-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.js
98 lines (79 loc) · 2.24 KB
/
monitor.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
require.paths.unshift(__dirname + '/lib');
require.paths.unshift(__dirname);
require.paths.unshift(__dirname + '/deps/express/lib')
var sys = require('sys'),
fs = require('fs'),
mongo = require('deps/node-mongodb-native/lib/mongodb'),
svc = require('service_json'),
weekly = require('weekly');
require('express');
require('express/plugins');
db = new mongo.Db('hummingbird', new mongo.Server('localhost', 27017, {}), {});
db.open(function(p_db) {
configure(function(){
set('root', __dirname);
set('db', db);
use(Static);
use(Cookie);
use(Logger);
try {
var configJSON = fs.readFileSync(__dirname + "/config/app.json");
} catch(e) {
sys.log("File config/app.json not found. Try: `cp config/app.json.sample config/app.json`");
}
sys.log("Started server with config: ");
sys.puts(configJSON);
var config = JSON.parse(configJSON.toString());
this.server.port = config.monitor_port;
for(var i in config) {
set(i, config[i]);
}
});
get('/', function(){
authenticate(this);
this.render('index.html.ejs');
});
get('/weekly', function() {
authenticate(this);
this.render('weekly.html.ejs');
});
get('/login', function() {
this.render('login.html.ejs');
});
post('/login', function() {
if(this.params.post.password == set('password')) {
this.cookie('not_secret', this.params.post.password);
sys.log("Auth succeeded for " + this.params.post.username);
this.redirect('/');
} else {
sys.log("Auth failed for " + this.params.post.username);
this.redirect('/login');
}
});
get('/sale_list', function() {
authenticate(this);
var self = this;
if(set('sales_uri')) {
svc.fetchJSON(set('sales_uri'), function(data) {
self.contentType('json');
self.respond(200, data);
});
} else {
self.respond(500, "No sales uri");
}
});
get('/week.json', function() {
authenticate(this);
var self = this;
weekly.findByDay(Express.settings['db'], function(data) {
self.contentType('json');
self.respond(200, data);
});
});
run();
});
var authenticate = function(req) {
if(set('password') != req.cookies['not_secret']) {
req.redirect('/login');
}
};