-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
154 lines (125 loc) · 4.26 KB
/
app.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const express = require('express');
const csv = require('csv-parser');
const fs = require('fs');
const https = require('https');
const uuid = require('uuid');
const jwt = require('jsonwebtoken');
const config = require('./config');
const User = require('./user');
const app = express();
const jsonParser = express.json();
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: config.tables.users.usersPath,
header: config.tables.users.headers,
append: true
});
app.get('/api/users', (req, res) => {
let users = [];
fs.createReadStream(config.tables.users.usersPath)
.pipe(csv())
.on('data', (row) => {
users.push(row);
})
.on('end', () => {
res.send(users);
});
});
app.post('/api/create', jsonParser, (req, res) => {
if(!req.body)
return res.sendStatus(400);
const name = req.body.name;
const pass = req.body.password;
if (name && pass) {
let users = [];
fs.createReadStream(config.tables.users.usersPath)
.pipe(csv())
.on('data', (row) => {
users.push(row);
})
.on('end', async () => {
if (users.some(e => e.Name === name)) {
res.status(400).send("User with such name exists");
}
else {
const user = new User(uuid.v4(), name, pass, Date.now());
await csvWriter.writeRecords([user]);
res.send(user.id);
}
})
}
else
res.status(400).send("Wrong data");
});
app.post('/api/login', jsonParser, (req, res) => {
if(!req.body)
return res.sendStatus(400);
const name = req.body.name;
const pass = req.body.password;
if (name && pass) {
let users = [];
fs.createReadStream(config.tables.users.usersPath)
.pipe(csv())
.on('data', (row) => {
users.push(row);
})
.on('end', () => {
const user = users.find(e => e.Name === name && e.Password === pass);
if (user) {
const response = {
id: user.Id,
name: user.Name,
token: jwt.sign({ id: user.id }, config.tokenKey)
};
res.status(200).send(response);
}
else
res.status(404).send("User with such name doesn't exist");
})
}
else
res.status(400).send("Wrong data");
});
app.get('/api/btcRate', (req, res) => {
const authHeader = req.headers.authorization;
if (authHeader) {
jwt.verify(
authHeader.split(' ')[1],
config.tokenKey,
(error, decoded) => {
if (error)
res.status(400).send('Invalid token');
if (decoded) {
const options = {
method: 'GET',
hostname: 'rest.coinapi.io',
path: '/v1/exchangerate/BTC/UAH',
headers: {'X-CoinAPI-Key': '36841ECC-2E4A-41ED-92BE-68372717978D'}
};
const coinReq = https.request(options, (coinRes) => {
let chunks = [];
coinRes.on('data', (d) => {
chunks.push(d);
});
coinRes.on('end', () => {
if (coinRes.statusCode != 200) {
res.status(500).send('There might be an issue with coinapi.io');
} else {
const info = JSON.parse(chunks.toString());
res.send(`BTC rate: ${info['rate']}`);
}
});
});
coinReq.on('error', (e) => {
console.error(e);
});
coinReq.end();
}
}
)
}
else
res.status(401).send();
})
app.listen(config.port, config.host, () =>
console.log(`Server listens to http://${config.host}:${config.port}`));