-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
93 lines (80 loc) · 2.19 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
var express = require('express');
const https = require('https');
const { exec } = require("child_process");
const fs = require('fs');
var app = express();
const uuid = require('uuid')
var activeGames = {
}
var returnBoardWrapper = (ID, res) => {
if (ID.length == 1) {
returnBoard("", res)
return
}
returnBoard(ID[1], res)
}
var returnBoard = (ID, res) => {
if (!fs.existsSync(__dirname + "/boards/b" + ID + ".svg")) {
exec("python main.py -l -o ./boards/b".concat(ID).concat(".svg -i ").concat(ID), (error, stdout, stderr) => {
res.sendFile(__dirname + "/boards/b" + ID + ".svg");
});
}
else {
res.sendFile(__dirname + "/boards/b" + ID + ".svg");
}
}
var game = (ID, res) => {
if (ID.length == 2) {
if (ID[1] == "") {
res.redirect("../" + ID[0])
return
}
res.redirect("/" + ID.join("/") + "/")
}
if (ID.length == 1) {
var uu = uuid.v4()
res.redirect("./" + ID[0] + "/" + uu + '/')
activeGames[uu] = ""
return
}
if (ID.length > 2) {
if (!(ID[1] in activeGames)) {
console.log("unknown UUID")
res.redirect("..")
}
if (ID[2].length >= activeGames[ID[1]].length) {
activeGames[ID[1]] = ID[2]
returnBoard(ID[2], res)
return
}
res.redirect("/" + ID[0] + "/" + ID[1] + "/" + activeGames[ID[1]])
return
}
}
var mapDirs = {
"b": returnBoardWrapper,
"g": game
}
var resolve = (req, res) => {
ID = req.params['0'].slice(1)
args = req.params['0'].split("/")
args.shift()
console.log(args)
if (args.length < 1) {
res.send("See if you can figure out how to use this thing :p")
return
}
if (args[0] in mapDirs) {
mapDirs[args[0]](args, res)
}
else {
res.send("invalid URL")
}
}
app.get('*', resolve)
https.createServer({
key: fs.readFileSync('/etc/letsencrypt/live/heyo.ydns.eu/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/heyo.ydns.eu/fullchain.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/heyo.ydns.eu/chain.pem')
}, app)
.listen(8080);