2
1

mock-instances-index.ts 995 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import * as express from 'express'
  2. export class MockInstancesIndex {
  3. private readonly indexInstances: { host: string, createdAt: string }[] = []
  4. initialize () {
  5. return new Promise(res => {
  6. const app = express()
  7. app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
  8. if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url)
  9. return next()
  10. })
  11. app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => {
  12. const since = req.query.since
  13. const filtered = this.indexInstances.filter(i => {
  14. if (!since) return true
  15. return i.createdAt > since
  16. })
  17. return res.json({
  18. total: filtered.length,
  19. data: filtered
  20. })
  21. })
  22. app.listen(42101, () => res())
  23. })
  24. }
  25. addInstance (host: string) {
  26. this.indexInstances.push({ host, createdAt: new Date().toISOString() })
  27. }
  28. }