-
Notifications
You must be signed in to change notification settings - Fork 0
/
restserver.js
118 lines (80 loc) · 2.92 KB
/
restserver.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
/**
* restserver.js
* Created 30 May 2012 by Ross Adamson
* Copyright 2012 Ross Adamson. All rights reserverd.
*
* This is a node.js application that serves as the REST backend server
* for the timetrack application. It mainly saves and loads data from the
* file system.
*/
var fs = require('fs');
function include(file) {
with (global) {
eval(fs.readFileSync(file) + '');
}
}
function getDateString(date) {
return date.toString('yyyy-MM-dd');
}
function getDateFilename(dateString, backup) {
return 'data/' + dateString + '.dat' + (backup ? '.backup' : '');
}
function getEntriesForDate(dateString, useBackup) {
var filename = getDateFilename(dateString, useBackup);
try {
var data = fs.readFileSync(filename, 'utf8');
return JSON.parse(data);
} catch (exception) {
return { date: dateString, entries: [], saved: false };
}
}
function getEntriesForWeek(date) {
var weekBeginDate = date.saturday().addDays(-7);
var currentDate = weekBeginDate;
var week = [];
for (var i = 0; i < 7; ++i) {
var dateString = getDateString(currentDate);
week.push(getEntriesForDate(dateString, false));
currentDate.addDays(1);
}
return week;
}
include('date.js');
var journey = require('journey');
var router = new(journey.Router);
router.map(function() {
this.root.bind(function(request, response) {
response.send('Welcome');
});
this.get(/^date\/([0-9]{4}-[0-9]{2}-[0-9]{2})$/).bind(function (request, response, dateString) {
response.send(getEntriesForDate(dateString, false));
});
this.get(/^date\/backup\/([0-9]{4}-[0-9]{2}-[0-9]{2})$/).bind(function (request, response, dateString) {
response.send(getEntriesForDate(dateString, true));
});
this.post(/^date$/).bind(function (request, response, data) {
var filename = getDateFilename(data.date, false);
fs.writeFileSync(filename, data.dataString);
response.send(200);
});
this.post(/^date\/backup$/).bind(function (request, response, data) {
fs.writeFileSync(getDateFilename(data.date, false), data.dataString);
fs.writeFileSync(getDateFilename(data.date, true), data.dataString);
response.send(200);
});
this.get(/^week\/([0-9]{4}-[0-9]{2}-[0-9]{2})$/).bind(function (request, response, dateString) {
var week = getEntriesForWeek(Date.parse(dateString));
response.send({ week: JSON.stringify(week) });
});
});
require('http').createServer(function (request, response) {
var body = '';
request.addListener('data', function(chunk) { body += chunk });
request.addListener('end', function() {
router.handle(request, body, function(result) {
result.headers['Access-Control-Allow-Origin'] = '*';
response.writeHead(result.status, result.headers);
response.end(result.body);
});
});
}).listen(8080);