-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (83 loc) · 2.57 KB
/
index.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
const express = require("express");
const path = require("path");
const db = require("./src/db");
const rawg = require("./src/rawg")
const {port} = require("./config.js");
const {cookieKey} = require("./config.js");
const {adminPassword} = require("./config.js");
const session = require("express-session");
const helmet = require("helmet");
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(session({
secret:cookieKey,
name:'bartering',
saveUninitialized:false,
resave:true
})
);
app.use(helmet());
function securityCheck(req, res, next) {
if(req.session.loggedIn) {
next()
} else {
res.redirect("/login.html")
}
}
app.listen(port, () => {
console.log("Server up at http://localhost.com:" + port)
db.checkConnection()
})
app.get("/", securityCheck, (req, res) => {
res.redirect("main.html")
})
app.get("/getGames", securityCheck, async (req, res) => {
res.send(await db.getDb("barter", "games"))
})
app.post("/submit", securityCheck, async (req, res) => {
let name = req.body.name;
let slug = req.body.slug;
let key = req.body.key;
await db.addGame("barter", "games", name, slug, key)
res.redirect("back");
})
app.post("/addTrade", securityCheck, async (req, res) => {
await db.addTrade(req.body.names, req.body.slugs, req.body.keys, req.body.ids);
res.redirect("back");
})
app.get("/searchRawg", async (req, res) => {
let searchString = req.query.searchstring;
let results = await rawg.getList(searchString)
res.send(results)
})
app.get("/gameRawg", async (req, res) => {
let slug = req.query.slug;
let result = await rawg.getGame(slug)
res.send(result)
})
app.get("/viewBundle", async (req, res) => {
let id = req.query.id;
res.send(await db.getBundle(id))
})
app.get("/bundle", async (req, res) => {
const id = req.query.id;
res.redirect("bundle.html?id=" + id)
})
app.get("/getBundles", securityCheck, async (req, res) => {
let bundles = await db.getBundles()
res.send(bundles)
})
app.post("/addBundle", securityCheck, async (req, res) => {
console.log(req.body)
await db.addBundle(req.body.title, req.body.recepient, req.body.message, req.body.names, req.body.slugs, req.body.keys, req.body.oldIds)
res.redirect("back");
})
app.post("/login", (req, res) => {
let password = req.body.password;
if (password === adminPassword) {
req.session.loggedIn = true;
res.redirect("/main.html");
}
})