-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
90 lines (81 loc) · 3.16 KB
/
main.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
var express = require('express');
var cookies = require("cookie-parser")
const Twig = require("twig");
const bodyParser = require('body-parser');
const game = require('./game');
var crypto = require("crypto");
var app = express();
const dbManager = require('./db');
var config = new dbManager.Config();
var db = new dbManager.SQLite();
const auth = require('./auth');
const frontend = require('./frontend')
const path = require('path');
auth.db = db;
app.use('/static', express.static(path.resolve(__dirname, 'web/static')));
app.use('/static', express.static('custom'));
app.use('/image', express.static('image'));
app.use(express.static(path.resolve(__dirname, 'public')));
app.use(bodyParser.json());
app.use(cookies())
app.set('views', path.resolve(__dirname, 'web/templates'));
frontend.registerRoutes(app, db, auth, config, game);
app.get('/api', function (req, res) {
res.json({"message": "Hello World!"});
auth.checkAuthorization()
});
app.get('/api/checkAuthorization', function (req, res) {
if (req.query.token) {
auth.checkAuthorization(req, res).then((value) => {
if (value) {
res.json({"message": "Token is valid", "valid": true});
}
}).catch(() => {});
}
});
app.post('/api/register/token', function (req, res) {
if (!config.registrationEnabled(dbManager.REGTYPE_TOKEN)) {
res.status(403).json({"error": "Token based registration is not allowed"})
return
}
let token = crypto.randomBytes(32).toString('hex');
db.registerUser(token, dbManager.REGTYPE_TOKEN);
res.json({"message": "Created user.", "token": token});
});
app.get('/api/register', function (req, res) {
res.json(config.getRegistrationOptions());
});
app.put('/api/positions', function (req, res) {
// Body has to have id, long and lat
auth.checkAuthorization(req, res).then((() => {
if ('id' in req.body && 'long' in req.body && 'lat' in req.body) {
db.setPos(auth.getToken(req), req.body['id'], req.body['lat'], req.body['long']).then(() => {
db.getPositions(auth.getToken(req)).then((result) => {
res.json({"message": "Saved position", positions: result})
})
}).catch(() => res.status(400).json({"error": "Could not set. Probably wrong data"}))
} else {
res.status(400).json({"error": "Wrong Body!"})
}
})).catch(() => {});
});
app.get('/api/positions', function (req, res) {
auth.checkAuthorization(req, res).then((() => {
db.getPositions(auth.getToken(req)).then((result) => {
res.json({"message": "There are positions", positions: result})
})
})).catch(() => {});
});
// Validates the positions
app.post('/api/positions', function (req, res) {
auth.checkAuthorization(req, res).then(() => {
game.getDistance(db, config, auth.getToken(req)).then((response) => {
res.json(response);
}).catch((response) => {
res.status(428).json(response);
})
}).catch(() => {});
});
app.listen(config.getWebserverPort(), config.getBindAddress(), function () {
console.log(`Running app on port ${config.getWebserverPort()}!`);
});