-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
132 lines (115 loc) · 3.77 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
var express = require('express');
var mongodb = require('mongodb');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var fs = require('fs');
Object.assign = require('object-assign');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/controllers'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan('combined'));
var db = null;
var dbDetails = new Object();
var ObjectID = mongodb.ObjectID;
var dbParams = null;
var connection = require('./config/db.js');
mongodb.connect(connection.url, function (err, conn) {
db = conn;
dbDetails.databaseName = conn.databaseName;
dbDetails.url = connection.label;
dbDetails.type = 'MongoDB';
dbParams = {
database: conn,
objectId: ObjectID,
};
var auth = require('./routes/auth.js')(dbParams);
app.use('/auth', auth);
var pages = require('./routes/pages.js')(dbParams);
app.use('/pages', pages);
var users = require('./routes/users.js')(dbParams);
app.use('/users', users);
var messages = require('./routes/messages.js')(dbParams);
app.use('/messages', messages);
var images = require('./routes/images.js')(dbParams);
app.use('/images', images);
var settings = require('./routes/settings.js')(dbParams);
app.use('/settings', settings);
var visitors = require('./routes/visitors.js')(dbParams);
app.use('/visitors', visitors);
var logins = require('./routes/logins.js')(dbParams);
app.use('/logins', logins);
var backup = require('./routes/backup.js')(dbParams);
app.use('/backup', backup);
var todos = require('./routes/todos.js')(dbParams);
app.use('/todos', todos);
console.log('Connected to MongoDB at: %s', connection.url);
});
app.get('/', function (req, res) {
res.sendFile('index.html');
});
app.get('/templates/:name', function (req, res) {
res.sendFile(__dirname + '/templates/' + req.params.name + '.html');
});
app.get('/page/:index', function (req, res) {
db.collection('pages', function (err, collection) {
collection.findOne({
index: req.params.index
}, function (err, result) {
res.send(result);
});
});
});
app.get('/img/:name', function (req, res) {
var file = process.env.HOME + '/upload/' + req.params.name;
if (fs.existsSync(file)) {
res.sendFile(file);
}
else {
res.sendFile(process.env.HOME + '/public/file_not_found.png')
}
});
app.get('/setting/:name', function (req, res) {
db.collection('settings', function (err, collection) {
collection.findOne({
name: req.params.name
}, function (err, result) {
res.send(result);
});
});
});
app.get('/stats/:excluded', function (req, res) {
var stats = {};
db.collection('messages', function (err, collection) {
collection.count({ accept: { $ne: 'true' } }, function (err, result) {
stats.messages = result;
db.collection('logins', function (err, collection) {
collection.count({ ip: { $nin: req.params.excluded.split(', ') } }, function (err, result) {
stats.logins = result;
db.collection('visitors', function (err, collection) {
collection.count({ ip: { $nin: req.params.excluded.split(', ') } }, function (err, result) {
stats.visitors = result;
res.json(stats);
});
});
});
});
});
});
});
app.post('/visitor', function (req, res) {
if (req.body.ip !== null && req.body.url.length) {
db.collection('visitors').insertOne({
ip: req.body.ip,
host: req.body.host,
referer: req.body.referer,
url: req.body.url,
date: Date.now()
}, function (err, result) {});
}
res.json({});
});
app.listen(connection.port, connection.ip);
console.log('Server running on http://%s:%s', connection.ip, connection.port);
module.exports = app;