From 6f1b4fa417786c2015f16b435e872aa65378efd7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 4 Sep 2019 11:18:33 +0200 Subject: Add auto follow instances index support --- .../instances-index/mock-instances-index.ts | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 shared/extra-utils/instances-index/mock-instances-index.ts (limited to 'shared/extra-utils/instances-index') diff --git a/shared/extra-utils/instances-index/mock-instances-index.ts b/shared/extra-utils/instances-index/mock-instances-index.ts new file mode 100644 index 000000000..cfa4523c1 --- /dev/null +++ b/shared/extra-utils/instances-index/mock-instances-index.ts @@ -0,0 +1,38 @@ +import * as express from 'express' + +export class MockInstancesIndex { + private indexInstances: { host: string, createdAt: string }[] = [] + + initialize () { + return new Promise(res => { + const app = express() + + app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) + + return next() + }) + + app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => { + const since = req.query.since + + const filtered = this.indexInstances.filter(i => { + if (!since) return true + + return i.createdAt > since + }) + + return res.json({ + total: filtered.length, + data: filtered + }) + }) + + app.listen(42100, () => res()) + }) + } + + addInstance (host: string) { + this.indexInstances.push({ host, createdAt: new Date().toISOString() }) + } +} -- cgit v1.2.3