forked from samson-teals/url-shorten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (92 loc) · 3.08 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
const fastify = require('fastify');
const fastify_static = require('fastify-static');
const path = require('path');
///////////////////////////////////////////////////////////
// Initialize fastify server and register static files
// handler
///////////////////////////////////////////////////////////
// inject sendHtml function into response object
const fastify_reply = require('fastify/lib/reply');
fastify_reply.prototype.sendHtml = function(html) {
this.type('text/html; charset=utf-8').send(html);
};
fastify_reply.prototype.send404 = function(errorMsg = null) {
this.code(404);
this.send(errorMsg || 'not found');
};
const app = fastify();
const port = process.env.PORT || 3000;
app.register(fastify_static, {
root: path.join(__dirname, 'public'),
index: 'index.html'
});
///////////////////////////////////////////////////////////
// Create objects that we'll need later
///////////////////////////////////////////////////////////
const InitDb = require('./init_db');
const initdb = new InitDb();
const Shortener = require('./shortener');
const shortener = new Shortener();
///////////////////////////////////////////////////////////
// Main async section runs after static initialization
///////////////////////////////////////////////////////////
const main = async () => {
///////////////////////////////////////////////////////////
// await async initializers
///////////////////////////////////////////////////////////
await initdb.init();
await shortener.init();
///////////////////////////////////////////////////////////
// add handlers
///////////////////////////////////////////////////////////
app.get('/last/:n(^\\d+)', async (req, res) => {
const n = req.params.n || 10;
const data = await shortener.last(n);
res.send(data);
});
app.post('/add', async (req, res) => {
const url = req.body.url;
const url_starts_with_http = true;
if (url_starts_with_http) {
// todo: get data from shortener module
const data = await shortener.insert(url);
res.send(data);
}
else {
res.code(400);
res.send('bad pattern');
}
});
app.post('/remove', async (req, res) => {
const key = req.body.key;
// todo: get data from shortener module
const data = {
key: 'a-removed-key',
url: 'http://some-url',
created: '2020-03-08T22:35:44.076560-07:00',
updated: '2020-03-08T22:35:44.076560-07:00'
};
res.send(data);
});
app.get('/r/:key', async (req, res) => {
const key = req.params.key;
// todo: get data from shortener module
const data = {
key: 'a-key',
url: 'http://google.ca',
created: '2020-03-08T22:35:44.076560-07:00',
updated: '2020-03-08T22:35:44.076560-07:00'
};
if (data && data.url) {
res.redirect(302, data.url);
}
else {
res.send404();
}
});
///////////////////////////////////////////////////////////
// start application server
///////////////////////////////////////////////////////////
app.listen(port, '0.0.0.0', () => console.log(`Shortener app listening on port ${port}!`));
};
main();