-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·134 lines (108 loc) · 3.72 KB
/
app.js
File metadata and controls
executable file
·134 lines (108 loc) · 3.72 KB
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
#!/usr/bin/env node
// environment:
// PORT (default: 8081)
// MUELLDATA (default: ./static_muelldata.json)
// MUELLCONF (default: ./storage.json)
// file will be written
var express = require('express');
var cors = require('cors');
var app = express();
var nconf = require('nconf');
var body_parser = require('body-parser');
muelldata = process.env.MUELLDATA || './static_muelldata.json'
muellconf = process.env.MUELLCONF || './storage.json'
port = process.env.PORT || 8081
console.log("starting muellshack with data: "+ muelldata + " config: " + muellconf + " on port " + port)
app.use(cors());
app.use(body_parser.json());
var config = require(muelldata);
nconf.file({file: muellconf });
var muell_log = nconf.get('muell_log');
var muell_status = nconf.get('muell_status');
app.post('/muellshack/:muellarg/', function(req, res, next) {
var muellarg = req.params.muellarg;
var next_muelldate = getNextMuelldate(muellarg)['date'];
var muelldata = config[muellarg];
if (!(muellarg in config))
return next();
if (muelldata.indexOf(next_muelldate) === -1)
return next();
if (!muell_status[muellarg])
muell_status[muellarg] = {};
if (!muell_status[muellarg][next_muelldate])
muell_status[muellarg][next_muelldate] = {};
if (req.body.hasOwnProperty("main_action_done"))
muell_status[muellarg][next_muelldate].main_action_done = req.body.main_action_done;
if (req.body.hasOwnProperty("mail_sended"))
muell_status[muellarg][next_muelldate].mail_sended = req.body.mail_sended;
const limit = 5;
var dat_limit = new Date(next_muelldate);
dat_limit = new Date(dat_limit.setDate(dat_limit.getDate() - limit));
res.type('application/json'); // set content-type
if (new Date() < dat_limit) {
log_msg(muellarg, next_muelldate, '403 - Forbidden', req.body);
res.status(403).send('Forbidden');
} else {
log_msg(muellarg, next_muelldate, 'put event', req.body);
res.json({'message':'Thanks for using muellshack'});
}
});
app.get('/muellshack/:muellarg', function(req, res, next) {
var muellarg = req.params.muellarg;
var output = getNextMuelldate(muellarg);
output[muellarg] = output.date;
if (!output[muellarg])
return next();
output['main_action_done'] = false;
output['mail_sended'] = false;
var value = get_muell_status(muellarg, output[muellarg]);
if (value.hasOwnProperty("main_action_done"))
output['main_action_done'] = value.main_action_done;
if (value.hasOwnProperty("mail_sended"))
output['mail_sended'] = value.mail_sended;
res.type('application/json'); // set content-type
res.json(output);
});
app.listen(port);
function getNextMuelldate(muell_type) {
var muelldata = config[muell_type];
var retval = {date: null, muelltype: muell_type};
if (!(muell_type in config))
return retval;
for (i = 0; i < muelldata.length; i++) {
date_i = new Date(muelldata[i])
date_i.setHours(23,59);
if (date_i > new Date())
{
retval.date = muelldata[i]
break;
}
}
return retval;
}
function get_muell_status(muell_type,muell_date) {
if (muell_status.hasOwnProperty(muell_type) && muell_status[muell_type].hasOwnProperty(muell_date)) {
return muell_status[muell_type][muell_date];
}
return {"date":muell_date};
}
function log_msg(muellarg, next_muelldate, event_msg, message) {
muell_log.push({
'event_date': new Date(),
'muell_type':muellarg,
'muell_date':next_muelldate,
'event_msg': event_msg,
'message': message
});
nconf.save();
}
function exitHandler(options, err) {
nconf.save();
if (options.exit) process.exit();
}
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
//catches sigterm event
process.on('SIGTERM', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));