-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (111 loc) · 3.76 KB
/
Copy pathindex.js
File metadata and controls
129 lines (111 loc) · 3.76 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
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const expressAccessToken = require('express-access-token');
const cors = require('cors');
const ipfilter = require('express-ipfilter').IpFilter
const mongoose = require('mongoose');
const app = express();
const port = 3000;
const path = __dirname + '/views/';
const { Parser } = require('json2csv');
const Models = require('./models/model.js');
const blacklist = ['78.54.214.87'];
const dbUrl = process.env.ENV == 'production'
? 'mongodb://mongodb:27017/eventLogs'
: 'mongodb://localhost:27017/eventLogs';
mongoose.set('strictQuery', true);
// Connect to MongoDB
mongoose.connect(dbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Configure CORS options
const corsOptions = {
origin: ['http://itv21.informatik.htw-dresden.de:3000',
'http://localhost:4200',
'http://localhost:3000',
'http://localhost:4220',
'http://localhost:4210',
'http://itv21.informatik.htw-dresden.de:4210',
'http://itv21.informatik.htw-dresden.de:4220',
'http://itv21.informatik.htw-dresden.de:4000'],
allowedHeaders: ['Content-Type', 'Origin', 'X-Requested-With', 'Accept', 'Authorization']
};
const accessTokens = process.env.ENV == 'production'
? [ process.env.ACCESS_TOKEN ]
: [ "123" ];
const firewall = (req, res, next) => {
const authorized = accessTokens.includes(req.accessToken);
if(!authorized) return res.status(403).send('Forbidden');
next();
};
// Use cors middleware
app.use(cors(corsOptions));
app.use(bodyParser.json({ limit: '200kb' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.json()); // Use express.json() middleware to parse JSON bodies
app.use(ipfilter(blacklist));
app.set('view engine', 'ejs');
app.post('/log/:schema', expressAccessToken,
firewall,
(req, res) => {
if (!req.is('application/json')) {
// Send error here
res.sendStatus(400);
} else {
// Create a new event log
const eventLog = new Models[req.params["schema"]](req.body);
// Save the event log to the database
eventLog.save()
.then(() => {
res.send('{ "msg": "Event data logged" }')
})
.catch(err => {
res.status(500).send(err)
});
}
});
app.get('/display/:schema',
async (req, res) => {
try {
const allLogs = await Models[req.params["schema"]].find().lean();
const results = {
'results': (allLogs) ? allLogs : null
};
res.render('logs', results);
} catch (error) {
console.log('error:', error.message);
res.status(500).send(error.message);
}
});
app.get('/export/:schema', async (req, res) => {
try {
const fields = Object.keys(Models[req.params["schema"]].schema.obj);
const opts = { fields };
const json2csv = new Parser(opts);
const allLogs = await Models[req.params["schema"]].find({});
let data = json2csv.parse(allLogs);
res.attachment('logging_data.csv');
res.status(200).send(data);
} catch (error) {
console.log('error:', error.message);
res.status(500).send(error.message);
}
});
app.get('/schema/:model', async (req, res) => {
try {
const fields = Object.keys(Models[req.params["model"]].schema.obj);
const results = {
'results': fields
};
res.render('schema', results);
} catch (error) {
console.log('error:', error.message);
res.status(500).send(error.message);
}
});
app.listen(port, () => {
console.log(`Listening on port ${port}`)
});