/
app.js
152 lines (122 loc) · 4.54 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
147
148
149
150
151
/*
* Copyright (C) 2012 Xavier Antoviaque <xavier@antoviaque.org>
*
* This software's license gives you freedom; you can copy, convey,
* propagate, redistribute and/or modify this program under the terms of
* the GNU Affero Gereral Public License (AGPL) as published by the Free
* Software Foundation (FSF), either version 3 of the License, or (at your
* option) any later version of the AGPL published by the FSF.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program in a file in the toplevel directory called
* "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
*/
// Imports /////////////////////////////////////////////////////
var restify = require('restify')
, Logger = require('bunyan')
, mime = require('mime')
, path = require('path')
, filed = require('filed');
// Database ////////////////////////////////////////////////////
var mongoose = require('mongoose')
, db = mongoose.connect('mongodb://localhost/forum')
, Thread = require('./models.js').Thread(db)
, Message = require('./models.js').Message(db);
// Views ///////////////////////////////////////////////////////
function get_thread(req, res, next) {
var send_result = function(err, thread_list) {
if (err) {
return next(err);
}
if(thread_list) {
if(thread_list.messages) {
thread_list.messages.sort(function(a, b) {
return ((a.date_added < b.date_added) ? -1 : (a.date_added > b.date_added) ? 1 : 0);
});
}
res.send(thread_list);
return next();
} else {
return next(new restify.ResourceNotFoundError("Could not find any such thread"));
}
};
if('_id' in req.params) {
Thread.findOne({'_id': req.params._id}, send_result);
} else {
Thread.find({}, send_result);
}
}
function post_thread(req, res, next) {
if(!req.body.title) {
return next(new restify.MissingParameterError("Missing required thread or message attribute in request body"));
}
new_thread = new Thread({title: req.body.title});
new_thread.save();
res.send(new_thread);
return next();
}
function post_message(req, res, next) {
if(!req.body.author || !req.body.text || !req.body.thread) {
return next(new restify.MissingParameterError("Missing required message attribute in request body"));
}
Thread.findOne({_id: req.body.thread}, function(err, thread) {
if (err) {
return next(err);
} else if(!thread) {
return next(new restify.ResourceNotFoundError("Could not find thread with id="+req.body.thread));
}
new_message = new Message({author: req.body.author,
text: req.body.text,
date_added: new Date()});
new_message.save();
thread.messages.push(new_message);
thread.save();
res.send(new_message);
return next();
})
}
// Server /////////////////////////////////////////////////////
var server = restify.createServer();
server.use(restify.acceptParser(server.acceptable))
.use(restify.authorizationParser())
.use(restify.dateParser())
.use(restify.queryParser({ mapParams: false }))
.use(restify.bodyParser({ mapParams: false }))
.use(restify.throttle({
burst: 10,
rate: 1,
ip: false,
xff: true,
}));
// Logging
server.on('after', restify.auditLogger({
log: new Logger({
name: 'mok',
streams: [{ level: "info", stream: process.stdout },
{ level: "info", path: 'log/server.log' }],
})
}));
// Routes /////////////////////////////////////////////////////
// Thread
server.get('/api/thread/', get_thread);
server.get('/api/thread/:_id', get_thread);
server.post('/api/thread/', post_thread);
// Message
server.post('/api/message/', post_message);
// Static Content /////////////////////////////////////////////
server.get(/\/(css|img|js)?.*/, restify.serveStatic({
directory: './static',
default: 'index.html'
}));
server.get('/', restify.serveStatic({
directory: './static',
default: 'index.html'
}));
server.listen(3001, function() {
console.log('%s listening at %s', server.name, server.url);
});