-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
172 lines (149 loc) · 5.11 KB
/
main.js
File metadata and controls
172 lines (149 loc) · 5.11 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
author: Piotr Szleg
Website providing login and register functionality.
TODO:
- move functionality to module, comment
- handlebar.js
*/
var accounts = require("./accounts");
var posts = require("./posts");
var parser = require('./parser');
var express = require('express');
var session = require('express-session');
var bodyParser = require("body-parser");
var RateLimit = require('express-rate-limit');
var app = express();
app.use(bodyParser.json()); // Configures bodyParser to accept JSON
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(express.static('public'));
var sess = {
secret: 'This needs to be replaced in public release.',
cookie: {},
resave: true,
saveUninitialized: false
}
if (app.get('env') === 'production') {
app.set('trust proxy', 1) // trust first proxy
sess.cookie.secure = true // serve secure cookies
}
app.use(session(sess));
function limitErrorPage(req, res) {
errorPage(new Error("Please try again after an hour."), res);
}
function errorPage(error, res){
parser.run("error.html", {"message":error.message}, (result)=>res.send(result));
}
var standardLimiter = new RateLimit({
windowMs: 15*60*1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
delayMs: 0 // disable delaying - full speed until the max limit is reached
});
// apply to all requests
app.use(standardLimiter);
var stateChangeLimiter = new RateLimit({
windowMs: 60*60*1000, // 1 hour window
delayAfter: 1, // begin slowing down responses after the first request
delayMs: 3*1000, // slow down subsequent responses by 3 seconds per request
max: 5, // start blocking after 5 requests
handler : limitErrorPage
});
// only apply to requests that begin with /api/
app.use([/code\/create/, /code\/edit\/([a-z0-9]+)/, '/register', '/login'], stateChangeLimiter);
function canEditPost(post, request){
return post.author && request.session.user==post.author;
}
app.get("/", function (req, res) {
res.redirect('/code/create/');
});
app.get(/code\/create/, function (req, res) {
parser.run("edit.html", {user:req.session.user}, (result)=>res.send(result));
});
app.post(/code\/create/, function (req, res) {
posts.create(req.session.user, req.body.title, req.body.text, req.body.language, req.body.tags)
.then((result)=>res.redirect("/code/"+result))
.catch((err)=>errorPage(err, res));
});
app.get(/code\/all/, function (req, res) {
posts.all()
.then((result)=>parser.run("all.html", {rows:result, user:req.session.user}, (result)=>res.send(result)))
.catch((err)=>errorPage(err, res));
});
app.get(/code\/remove\/([a-z0-9]+)/, function (req, res) {// should be changed to post in the future
posts.get(req.params[0])
.then((result)=>{
if(canEditPost(result, req)){
posts.remove(req.params[0])
.then(()=>res.redirect("/code/all"))
.catch((err)=>errorPage(err, res));
} else {
errorPage(new Error("You can't remove this post."), res);
}
})
.catch((err)=>errorPage(err, res));
});
app.get(/code\/edit\/([a-z0-9]+)/, function (req, res) {
posts.get(req.params[0])
.then((result)=>{
if(canEditPost(result, req)){
parser.run("edit.html", Object.assign({user:req.session.user}, result), (result)=>res.send(result))
} else {
errorPage(new Error("You can't edit this post."), res);
}
})
.catch((err)=>errorPage(err, res));
});
app.post(/code\/edit\/([a-z0-9]+)/, function (req, res) {
posts.get(req.params[0])
.then((result)=>{
if(canEditPost(result, req)){
posts.edit(req.params[0], req.session.user, req.body.title, req.body.text, req.body.language, req.body.tags)
.then(()=>res.redirect("/code/"+req.params[0]))
.catch((err)=>errorPage(err, res));
} else {
errorPage(new Error("You can't edit this post."), res);
}
})
.catch((err)=>errorPage(err, res));
});
app.get(/code\/([a-z0-9]+)/, function (req, res) {
posts.get(req.params[0])
.then((result)=>
parser.run("view.html", Object.assign({user:req.session.user}, result), (result)=>res.send(result)))
.catch((err)=>errorPage(err, res));
});
// User registry routes
app.post('/register', function (req, res) {
accounts.register(req.body.user, req.body.password)
.then(()=>{
req.session.user=req.body.user;
res.redirect('/');
})
.catch((err)=>errorPage(err, res));
});
app.post('/login', function (req, res) {
accounts.login(req.body.user, req.body.password)
.then(()=>{
req.session.user=req.body.user;
res.redirect('/');
})
.catch((err)=>errorPage(err, res));
});
app.get('/logout', function (req, res) {
req.session.user=undefined;
res.redirect('/');
});
app.get('/userslist', function (req, res) {
accounts.list()
.then((result)=>parser.run("users.html", {rows:result, user:req.session.user}, (result)=>res.send(result)))
.catch((err)=>errorPage(err, res));
});
var server = app.listen(8081, function () {
var host = server.address().address;
var port = server.address().port;
if(host==="::"){
host="localhost";
}
console.log("App listening at http://%s:%s", host, port);
});