-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (87 loc) · 2.32 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
104
105
106
const express = require('express');
const dotenv = require('dotenv');
const mysql = require('mysql2');
dotenv.config();
// Database
function connect_db() {
const connection = mysql.createConnection({
host: process.env.DBIP,
user: process.env.DBUSER,
password: process.env.DBPASS,
database: 'strafbakken'
});
connection.connect( error => {
if (error) {
console.error(error);
return res.statusStatus(500);
}
});
return connection;
}
// Routes
const app = express();
app.use('/', express.static('public'));
app.get('/bakken', (req, res) => {
if (req.headers.token == process.env.INLOG_PASS) {
// Make db connection
const conn = connect_db();
// Query
const query = 'SELECT * FROM strafbakken';
conn.query(query, (error, results) => {
if (error) {
console.error(error);
res.sendStatus(500);
conn.destroy();
} else {
res.send(JSON.stringify(results));
conn.end();
}
});
} else {
res.sendStatus(403);
}
});
app.post('/bakken', (req, res) => {
if (req.headers.token == process.env.INLOG_PASS) {
// Make db connection
const conn = connect_db();
// Query
const query = 'UPDATE strafbakken SET bakken = bakken + 1 WHERE name = ?';
conn.query(query, [req.headers.name], (error) => {
if (error) {
console.error(error);
res.sendStatus(500);
conn.destroy();
} else {
res.sendStatus(200);
conn.end();
}
});
} else {
res.sendStatus(403);
}
})
app.delete('/bakken', (req, res) => {
if (req.headers.token == process.env.INLOG_PASS) {
// Make db connection
const conn = connect_db();
// Query
const query = 'UPDATE strafbakken SET bakken = bakken - 1 WHERE name = ?';
conn.query(query, [req.headers.name], (error) => {
if (error) {
console.error(error);
res.sendStatus(500);
conn.destroy();
} else {
res.sendStatus(200);
conn.end();
}
});
} else {
res.sendStatus(403);
}
})
// Listen
const port = process.env.PORT || 8080;
app.listen(port);
console.log(`Listening on port ${port}...`);