]>
Commit | Line | Data |
---|---|---|
6f1b4fa4 | 1 | import * as express from 'express' |
f6500729 | 2 | import { randomInt } from '@shared/core-utils' |
6f1b4fa4 C |
3 | |
4 | export class MockInstancesIndex { | |
a1587156 | 5 | private readonly indexInstances: { host: string, createdAt: string }[] = [] |
6f1b4fa4 C |
6 | |
7 | initialize () { | |
f6500729 | 8 | return new Promise<number>(res => { |
6f1b4fa4 C |
9 | const app = express() |
10 | ||
11 | app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
12 | if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) | |
13 | ||
14 | return next() | |
15 | }) | |
16 | ||
17 | app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => { | |
18 | const since = req.query.since | |
19 | ||
20 | const filtered = this.indexInstances.filter(i => { | |
21 | if (!since) return true | |
22 | ||
23 | return i.createdAt > since | |
24 | }) | |
25 | ||
26 | return res.json({ | |
27 | total: filtered.length, | |
28 | data: filtered | |
29 | }) | |
30 | }) | |
31 | ||
f6500729 C |
32 | const port = 42101 + randomInt(1, 100) |
33 | app.listen(port, () => res(port)) | |
6f1b4fa4 C |
34 | }) |
35 | } | |
36 | ||
37 | addInstance (host: string) { | |
38 | this.indexInstances.push({ host, createdAt: new Date().toISOString() }) | |
39 | } | |
40 | } |