-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
120 lines (103 loc) · 2.95 KB
/
server.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
const { json } = require('express');
const express = require('express');
const mongo = require('mongodb').MongoClient;
const http = require('http');
const app = express();
app.use(express.json());
const url = `mongodb://${process.env.MONGODB_USERNAME}:${encodeURIComponent(process.env.MONGODB_PASSWORD)}@${process.env.MONGODB_HOST}:27017/${process.env.MONGODB_DATABASE}`;
async function getCatalog() {
return new Promise(done => {
const req = http.request({
hostname: 'catalog',
path: '/catalog',
port: '8080',
method: 'GET'
}, res => {
let body = '';
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
const response = JSON.parse(body);
done(response);
});
});
req.on('error', error => {
console.error(`Failed to get movies catalog: ${error}`);
done([]);
});
req.end();
});
}
async function getRentals(db) {
return new Promise(done => {
db.collection('rentals').find().toArray(async (error, rentals) => {
if (error) {
console.error(`Failed to query rentals: ${error}`)
done([]);
}
done(rentals);
});
});
}
async function getExpandedRentals(db) {
const rentals = await getRentals(db);
const catalog = await getCatalog();
const extended = rentals.map(rental => {
const movie = catalog.find(m => m.id === rental.catalog_id);
return movie ? {
...rental,
price: 1 * movie.price,
vote_average: movie.vote_average,
original_title: movie.original_title,
backdrop_path: movie.backdrop_path,
overview: movie.overview
} : null;
}).filter(Boolean);
return extended;
}
function startWithRetry() {
mongo.connect(url, {
useUnifiedTopology: true,
useNewUrlParser: true,
connectTimeoutMS: 1000,
socketTimeoutMS: 1000,
}, (err, client) => {
if (err) {
console.error(`Error connecting, retrying in 1 sec: ${err}`);
setTimeout(startWithRetry, 1000);
return;
}
const db = client.db(process.env.MONGODB_DATABASE);
app.listen(8080, () => {
app.get('/rentals/healthz', (_, res) => {
res.sendStatus(200)
return;
});
app.get('/rentals', async (_, res) => {
console.log('GET /rentals');
const rentals = await getExpandedRentals(db);
res.json(rentals);
});
app.post('/rent', async (req, res) => {
console.log(`POST /rent`);
const rent = {
_id: req.body?.catalog_id,
id: req.body?.catalog_id,
price: req.body?.price,
catalog_id: req.body?.catalog_id
};
try {
await db.collection('rentals').updateOne(
{ _id: rent._id },
{ $set: rent },
{ upsert: true }
);
} catch(err) {
console.log(`Failed to rent: ${err}`);
}
res.json([]);
});
console.log('Server running on port 8080.');
});
});
};
startWithRetry();