This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
119 lines (102 loc) · 3.41 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
107
108
109
110
111
112
113
114
115
116
117
118
119
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const uuidv1 = require('uuid/v1');
const bcrypt = require('bcrypt');
const port = process.env.PORT || 3000;
const dataset = require("./modules/dataset.js");
//const prediction = require("./modules/prediction.js");
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static('static'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.status(200).json({ message: 'Enable CORS in Node.js - Clue Mediator' });
});
app.listen(port, () => {
console.log('Server started on: ' + port);
});
/** simplify paths **/
app.get("/model", (req, res)=>{
res.sendFile(__dirname+"/static/model.html");
});
app.get("/login", (req, res)=>{
res.sendFile(__dirname+"/static/login.html");
});
app.get("/register", (req, res)=>{
res.sendFile(__dirname+"/static/register.html");
});
/** building api **/
app.get("/api/buildings", async (req, res)=>{
res.send(await dataset.prototype.getBuildings());
});
app.get("/api/building/:id", async (req, res)=>{
res.send(await dataset.prototype.getBuilding(req.params.id));
});
/** register stuff */
app.post('/api/register', async (req, res) => {
const usersList = await dataset.prototype.getAllUsers();
console.log(usersList);
let user = usersList.find(user => user.username == req.body.username);
if (user != null)
return res.status(400).send('Username already exists');
const pass = req.body.password;
const license = req.body.license;
const hashedPassword = await bcrypt.hash(pass, 10);
user = {
id: uuidv1(),
username: req.body.username,
password: hashedPassword,
};
let validKey = await dataset.prototype.getLicense(license);
console.log(validKey);
if (validKey)
await dataset.prototype.addUsers(user);
else
res.status(401).send();
res.status(200).send();
});
/** login stuff */
app.post('/api/login', async (req, res) => {
const usersList = await dataset.prototype.getAllUsers();
const user = usersList.find(user => user.username == req.body.username);
if (user == null)
return res.status(400).send('User not found');
if (await bcrypt.compare(req.body.password, user.password))
res.status(200).send(user.id)
else
res.status(401).send('Wrong password');
});
app.get('/api/balls/:id', async (req, res) => {
res.status(200).send(await dataset.prototype.getBalls(req.params.id));
});
app.post('/api/events', async (req, res) => {
if (!req.body.user_id) {
res.status(404).send();
}
res.status(200).send(await dataset.prototype.insertEvent(req.body.event));
});
app.post('/api/balls', async (req, res) => {
if (!req.body.user_id) {
res.status(404).send();
}
res.status(200).send(await dataset.prototype.insertBall(req.body.ball));
});
app.delete('/api/balls/:id/:bid/:uid', async (req, res) => {
if (!req.params.uid) {
res.status(404).send();
}
res.status(200).send(await dataset.prototype.deleteBall(req.params.id, req.params.bid));
});
/** chart */
app.get('/api/chart/:id', async(req, res) => {
res.send(await dataset.prototype.getEventByHour(req.params.id));
});
/** prediction */
app.get('/api/prediction/:id', async(req, res) => {
res.send(await dataset.prototype.makePrediction(req.params.id));
});