-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode_HW2.txt
More file actions
101 lines (92 loc) · 2.63 KB
/
Copy pathCode_HW2.txt
File metadata and controls
101 lines (92 loc) · 2.63 KB
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
const express = require('express');
const mariadb = require('mariadb');
const path = require('path');
const axios = require('axios');
const bodyParser = require('body-parser');
const { error } = require('console');
const app = express();
const ip = '34.56.60.41'; // ip and replicaIp are switched on other VM
const replicaIp = '35.239.139.129';
const port = 80;
const pool = mariadb.createPool({
host: '127.0.0.1',
port: 3306,
user: 'lgetter2',
password: 'secret',
database: 'hw2',
connectionLimit: 5
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
app.get('/greeting', async (req, res) => {
res.status(200).send('Hello World!');
});
app.post('/register', async (req, res) => {
const username = req.body.username;
let conn;
try {
conn = await pool.getConnection();
await conn.query('INSERT INTO users(username) VALUES (?)', [username]);
axios.post(`http://${replicaIp}/register-replica`, { username : username });
res.status(200).send();
} catch (err) {
res.status(500).send(`Error adding user: ${err}`);
} finally {
if (conn) conn.release();
}
});
app.post('/register-replica', async (req, res) => {
const username = req.body.username;
let conn;
try {
conn = await pool.getConnection();
await conn.query('INSERT INTO users(username) VALUES (?)', [username]);
res.status(200).send();
} catch (err) {
res.status(500).send(`Error adding user: ${err}`);
} finally {
if (conn) conn.release();
}
});
app.get('/list', async (req, res) => {
let conn;
try {
conn = await pool.getConnection();
const users = await conn.query('SELECT username FROM users');
const usernames = users.map(user => user.username);
res.status(200).json({ users : usernames });
} catch (err) {
res.status(500).send(`Error retrieving users: ${err}`);
} finally {
if (conn) conn.release();
}
});
app.post('/clear', async (req, res) => {
let conn;
try {
conn = await pool.getConnection();
await conn.query('DELETE FROM users');
axios.post(`http://${replicaIp}/clear-replica`);
res.status(200).send();
} catch (err) {
res.status(500).send(`Error deleting users: ${err}`);
} finally {
if (conn) conn.release();
}
});
app.post('/clear-replica', async (req, res) => {
const username = req.body.username;
let conn;
try {
conn = await pool.getConnection();
await conn.query('DELETE FROM users');
res.status(200).send();
} catch (err) {
res.status(500).send(`Error adding user: ${err}`);
} finally {
if (conn) conn.release();
}
});
app.listen(port, () => {
console.log(`Server is running on http://${ip}:${port}`);
});