-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
55 lines (45 loc) · 1.41 KB
/
server.ts
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
import dotenv from 'dotenv-safe';
import restify from 'restify';
import { initDbConnection } from './utils/database';
import { initializeTasks } from './utils/monitoring';
import {
getEndpointsHandler,
getEndpointResultsHandler,
getResultHandler,
postEndpointHandler,
deleteEndpointHandler,
patchEndpointHandler,
} from './utils/handlers';
async function main() {
dotenv.config();
const server = restify.createServer({
name: 'urlmon api',
});
server.use(restify.plugins.bodyParser());
console.log('Connecting to database ...');
/* eslint-disable no-await-in-loop */
let done = false;
while (!done) {
try {
await initDbConnection();
done = true;
} catch (e) {
console.log(e.message);
console.log('db connection failed, retrying in 5 seconds ...');
await new Promise((resolve) => { setTimeout(resolve, 5000); });
}
}
/* eslint-enable no-await-in-loop */
console.log('Successfully connected to database');
server.get('/endpoints', getEndpointsHandler);
server.get('/endpoint/:id/results', getEndpointResultsHandler);
server.get('/result/:id', getResultHandler);
server.post('/endpoint', postEndpointHandler);
server.del('/endpoint/:id', deleteEndpointHandler);
server.patch('/endpoint/:id', patchEndpointHandler);
server.listen(5000, () => {
console.log(`${server.name} listening at ${server.url}`);
});
initializeTasks();
}
main();