forked from mangalam-research/wed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·143 lines (121 loc) · 3.68 KB
/
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
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
var express = require("express");
var path = require("path");
var url = require("url");
var fs = require("fs");
var Buffer = require("buffer").Buffer;
var querystring = require("querystring");
var verbose = false;
var next_arg = 2;
if (process.argv[2] === "-v") {
verbose = true;
next_arg++;
}
var arg = process.argv[next_arg] || "0.0.0.0:8888";
var parts = arg.split(":");
var ip = parts[0];
var port = parts[1];
var cwd = process.cwd();
var app = express();
if (verbose)
app.use(express.logger());
app.use(express.compress());
app.use(express.static(cwd));
function writeResponse(response, status, data, type) {
if (verbose)
console.log('response message', data);
type = type || "text/plain";
var headers = {"Content-Type": type,
"Content-Length": Buffer.byteLength(data)};
response.writeHead(status, headers);
if (data) {
if (type !== "text/plain")
response.write(data, "binary");
else
response.write(data);
}
response.end();
}
function unlinkIfExists(path) {
if (fs.existsSync(path))
fs.unlinkSync(path);
}
var fail_on_save = false;
var fail_on_recover = false;
function dumpData(request, callback) {
var uri = url.parse(request.url).pathname;
var filename = path.join(cwd, uri);
var chunks = [];
request.on('data', function (chunk) {
chunks.push(chunk.toString());
});
request.on('end', function() {
var body = chunks.join('');
var writable = fs.createWriteStream(filename, {'flags': 'a'});
writable.write("\n***\n");
var decoded = querystring.parse(body);
if (verbose)
console.log('decoded body', decoded);
writable.write(JSON.stringify(decoded));
writable.end();
if (callback)
callback(decoded);
});
}
app.post("/build/ajax/log.txt", function (request, response) {
dumpData(request);
writeResponse(response, 200, "{}", "application/json");
});
app.post("/build/ajax/save.txt", function (request, response) {
dumpData(request, function (decoded) {
var status = 200;
var messages = [];
switch(decoded.command) {
case 'check':
break;
case 'save':
if (!fail_on_save)
messages.push({type: 'save_successful'});
else
status = 400;
break;
case 'recover':
if (!fail_on_recover)
messages.push({type: 'save_successful'});
else
status = 400;
break;
default:
status = 400;
}
var msg = {messages: messages};
var stringified = JSON.stringify(msg);
writeResponse(response, status, stringified, "application/json");
});
});
app.post("/build/ajax/control", function(request, response) {
dumpData(request, function (decoded) {
var status = 200;
switch(decoded.command) {
case 'reset':
unlinkIfExists(path.join(cwd, "/build/ajax/log.txt"));
unlinkIfExists(path.join(cwd, "/build/ajax/save.txt"));
unlinkIfExists(path.join(cwd, "/build/ajax/control"));
fail_on_save = false;
fail_on_recover = false;
break;
case 'fail_on_save':
fail_on_save = decoded.value;
break;
case 'fail_on_recover':
fail_on_recover = decoded.value;
break;
default:
status = 400;
}
writeResponse(response, status, "{}", "application/json");
});
});
app.listen(port, ip);
console.log("http://" + ip + ":" + port);
// LocalWords: url querystring ajax txt json