-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
131 lines (120 loc) · 3.9 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
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
120
121
122
123
124
125
126
127
128
129
130
131
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const bodyParser = require('body-parser');
const app = express();
const DB_PATH = 'porte.db';
app.use(bodyParser.json());
let db = new sqlite3.Database(DB_PATH, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the porte database.');
});
app.get('/', (req, res) => {
res.send('/docs');
});
app.get('/api/users/get', (req, res) => {
db.all('SELECT * FROM users', [], (err, rows) => {
if (err) {
throw err;
}
res.set('Content-Type', 'text/plain');
res.send(rows.map(formatUserMinimal).join('\n'));
});
});
app.get('/api/doorallowed/:nfc_card_id', (req, res) => {
const nfc_card_id = req.params['nfc_card_id'];
res.set('Content-Type', 'text/plain');
db.get('SELECT * FROM users WHERE nfc_card_id = ?', [nfc_card_id], (err, row) => {
if (err) {
throw err;
}
if (!row) {
return res.send('0#not found');
}
const now = new Date();
const expiry = new Date(row['expiration_date'].toString());
if (row['expiration_date'] === '-1' || row['expiration_date'] === '-2' || now < expiry) {
return res.send('1#ok');
}
res.send('0#expired');
});
});
app.get('/api/doorlogs/add', (req, res) => {
const { date_time, card_id } = req.query;
db.run('INSERT INTO logs (datetime, card_id) VALUES (?, ?)', [date_time, card_id], (err) => {
if (err) {
return res.status(400).send('Error inserting log');
}
res.send('ok');
});
});
// app.get('/users/get/:username', (req, res) => {
// const username = req.params.username;
// db.get('SELECT * FROM users WHERE username = ?', [username], (err, row) => {
// if (err) {
// throw err;
// }
// res.send(formatUser(row));
// });
// });
//
// app.get('/users/add', (req, res) => {
// const { username, nfc_card_id, status, expiration_date } = req.query;
// let expiry = expiration_date;
// if (expiration_date !== '-1' && expiration_date !== '-2') {
// try {
// expiry = new Date(expiration_date).toISOString();
// } catch (e) {
// return res.status(400).send('Invalid date format');
// }
// }
// db.run('INSERT INTO users (username, nfc_card_id, status, expiration_date) VALUES (?, ?, ?, ?)', [username, nfc_card_id, status, expiry], (err) => {
// if (err) {
// return res.status(400).send('Error adding user');
// }
// res.send('ok');
// });
// });
//
// app.get('/users/isactive/:username', (req, res) => {
// const username = req.params.username;
// db.get('SELECT * FROM users WHERE username = ?', [username], (err, row) => {
// if (err) {
// throw err;
// }
// if (!row) {
// return res.send('0#not found');
// }
// const now = new Date();
// const expiry = new Date(row.expiration_date);
// if (row.expiration_date === '-1' || row.expiration_date === '-2' || now < expiry) {
// return res.send('1#ok');
// }
// res.send('0#expired');
// });
// });
//
//
//
// app.get('/logs', (req, res) => {
// db.all('SELECT * FROM logs', [], (err, rows) => {
// if (err) {
// throw err;
// }
// res.send(rows.map(formatLog).join('\n'));
// });
// });
function formatUser(user) {
return `${user.id} # ${user.username} # ${user['nfc_card_id']} # ${user.status} # ${user['expiration_date']}`;
}
function formatUserMinimal(user) {
return `${user['nfc_card_id']};${user['expiration_date']}`;
}
function formatLog(log) {
return `${log['datetime']} # ${log['card_id']}`;
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});