|
1 | | -# echo-api |
2 | | -Basic Echo API to help debugging API gateway solutions and introduce artificial latency |
| 1 | +# Introduction |
| 2 | +Basic *echo* API to help debugging API gateway or proxy solutions. |
| 3 | + |
| 4 | +# Usage |
| 5 | +The service exposes the following endpoints: |
| 6 | +- `GET /echo/:latency/ms`: The endpoints respond with the HTTP request headers using the `:latency` parameter value to introduce artificial latency. |
| 7 | + |
| 8 | +## Example: |
| 9 | +```bash |
| 10 | +docker run --rm -p 3000:3000 kyberneees/echo-api:latest |
| 11 | +``` |
| 12 | +```bash |
| 13 | +curl -v http://localhost:3000/echo/10/ms |
| 14 | + |
| 15 | +* Trying 127.0.0.1:3000... |
| 16 | +* Connected to localhost (127.0.0.1) port 3000 (#0) |
| 17 | +> GET /echo/10/ms HTTP/1.1 |
| 18 | +> Host: localhost:3000 |
| 19 | +> User-Agent: curl/7.79.1 |
| 20 | +> Accept: */* |
| 21 | +> |
| 22 | +* Mark bundle as not supporting multiuse |
| 23 | +< HTTP/1.1 200 OK |
| 24 | +< x-added-latency-ms: 10 |
| 25 | +< content-type: application/json; charset=utf-8 |
| 26 | +< Date: Wed, 25 May 2022 12:50:24 GMT |
| 27 | +< Connection: keep-alive |
| 28 | +< Keep-Alive: timeout=5 |
| 29 | +< Content-Length: 67 |
| 30 | +< |
| 31 | +* Connection #0 to host localhost left intact |
| 32 | +{"host":"localhost:3000","user-agent":"curl/7.79.1","accept":"*/*"} |
| 33 | +``` |
| 34 | +
|
| 35 | +Container logs: |
| 36 | +```bash |
| 37 | +GET /echo/10/ms 200 - - 10.244 ms |
| 38 | +... |
| 39 | +``` |
| 40 | +
|
| 41 | +# Implementation |
| 42 | +```js |
| 43 | +const restana = require('restana') |
| 44 | +const morgan = require('morgan') |
| 45 | + |
| 46 | +const service = restana() |
| 47 | +service.use(morgan('tiny')) |
| 48 | + |
| 49 | +service.get('/echo/:latency/ms', (req, res) => { |
| 50 | + const { latency } = req.params |
| 51 | + |
| 52 | + res.setHeader('x-added-latency-ms', latency) |
| 53 | + setTimeout(() => res.send(req.headers), latency) |
| 54 | +}) |
| 55 | + |
| 56 | +service.start(3000) |
| 57 | +``` |
0 commit comments