-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (48 loc) · 1.72 KB
/
Copy pathserver.js
File metadata and controls
60 lines (48 loc) · 1.72 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
require('dotenv').config();
const express = require('express');
const gcal = require('./Utility/gcal.js');
const days = require('./ReqHandlers/GET-Handlers/days.js');
const timeslots = require('./ReqHandlers/GET-Handlers/timeslots.js');
const book = require('./ReqHandlers/POST-Handlers/book.js');
const PORT = Number(process.env.PORT) || 8080;
function sendResult(res, data) {
const status = data && data.success === false ? 400 : 200;
res.status(status).json(data);
}
function wrap(handler) {
return (req, res, next) => Promise.resolve(handler(req, res)).catch(next);
}
async function main() {
const auth = await gcal.initAuthorize();
const app = express();
app.use(express.json());
app.get('/days', wrap(async (req, res) => {
const data = await days.getBookableDays(auth, req.query.year, req.query.month);
sendResult(res, data);
}));
app.get('/timeslots', wrap(async (req, res) => {
const data = await timeslots.getAvailTimeslots(
auth, req.query.year, req.query.month, req.query.day
);
sendResult(res, data);
}));
app.post('/book', wrap(async (req, res) => {
const data = await book.bookAppointment(
auth,
req.query.year, req.query.month, req.query.day,
req.query.hour, req.query.minute
);
sendResult(res, data);
}));
app.use((err, _req, res, _next) => {
console.error(err);
res.status(500).json({success: false, message: err.message || 'Internal server error'});
});
app.listen(PORT, () => {
console.log(`Server is now running on port ${PORT}... Ctrl+C to end`);
});
}
main().catch((err) => {
console.error(err);
process.exit(1);
});