-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
146 lines (125 loc) · 4.23 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
var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var filesystem = require('fs')
var SubsProvider = require('./subs_provider').SubsProvider;
var subs_provider = new SubsProvider();
var args = process.argv.slice(2);
var accessLogStream = filesystem.createWriteStream(__dirname + '/logs/access.log', {flags: 'a'})
var rating_file = __dirname + '/ratings/ratings.log';
var app = express();
//app.use(BodyParser.json());
morgan.token('remote-addr',function(req,res){ return get_ip(req); });
morgan.token('remote-user',function(req,res){ return req.headers['proxy-user']; });
app.use(morgan('combined', {stream: accessLogStream}));
app.use(express.static(__dirname + '/'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
manifest = {"/fa15":"/tmp/old/fa15"};
for(var m in manifest){
console.log(m,manifest[m]);
app.use(m, express.static(manifest[m]));
app.get(m, function(req, res){
console.log(m,get_ip(req));
res.redirect(m+'/text/box.xml');
});
}
app.get('/', function(req, res){
console.log(get_ip(req));
res.redirect('/text/box.xml');
});
app.get('/text/', function(req, res){
res.redirect('/text/box.xml');
});
app.post('/homework/rate', function(req, res){
console.log("RATE",req.body);
var date = new Date();
var s = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate() + ":" + date.getHours() + ":" + date.getMinutes() + "; ";
if(req.body.r) s += req.body.r + ";";
if(req.body.comments) s += " " + req.body.comments.length + "," + req.body.comments;
s += "\n";
filesystem.appendFile(rating_file, s, function(err) { console.log("RATE ERR", err); });
res.redirect('/homework/thankyou.html');
});
app.post('/homework/get', function(req, res){
console.log("GET",req.body);
var hwid = get_homework_data(req).hwid;
subs_provider.get(get_user_data(req), get_homework_data(req), function(error, result){
res.send({"error":error,"id":hwid,"data":result});
});
});
app.get('/homework', function(req, res){
console.log(JSON.stringify(req.headers));
res.redirect('/homework/homework.html');
});
app.post('/homework/saveall', function(req, res){
console.log("SAVEALL",get_user_data(req),req.body);
var tried = 0;
var saved = 0;
var total = req.body.total;
console.log(req.body);
for(var idx in req.body){
if(idx == 'total') continue;
console.log(req.body[idx]);
subs_provider.save(get_user_data(req), req.body[idx], function(error, result){
tried++;
if(!error) saved++
console.log(saved,tried,total);
if(tried == total){
console.log("DONE");
if(saved < tried) error = "Did not save data: " + error;
console.log(error,result);
res.send({"error":error,"success":result});
}
});
}
});
app.post('/homework/save', function(req, res){
console.log("AAAA",req.body);
subs_provider.save(get_user_data(req), get_homework_data(req), function(error, result){
res.send({"error":error,"success":result});
});
});
app.post('/homework/submit', function(req, res){
console.log("AAAA",req.body);
subs_provider.del_comment(get_user_data(req), get_homework_data(req), function(error, result){
res.send(error + ", " + result);
});
});
var get_ip = function(req){
return req.header('x-forwarded-for') || req.connection.remoteAddress;
}
var get_homework_data = function(req){
return {'hwid':req.body.hwid,
'pid':req.body.pid,
'text':req.body.text};
}
var get_user_data = function(req){
return {'ip':get_ip(req),
//*/
'username':req.headers['proxy-user']};
/*/
'username':'test_user'};
//*/
}
if(args.length == 1){
server = http.createServer(app).listen(args[0]);
console.log("Server running on:" + args[0]);
}
else if(args.length == 2){
server = http.createServer(app).listen(args[0]);
server.on('connection', function (sock) {
if(sock.remoteAddress != args[1]){
console.log("Denied: " + sock.remoteAddress);
sock.end("Access denied");
}
});
console.log("Server running on:" + args[0] + " listening to "+args[1]);
}
else{
server = http.createServer(app).listen(61453);
console.log("Server running on :61453");
}