Skip to content

Commit 89ac61e

Browse files
committed
adding services.json endpoint
1 parent 7271b76 commit 89ac61e

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

demos/basic.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ gateway({
99

1010
routes: [{
1111
prefix: '/public',
12-
target: 'http://public.myapp:300'
12+
target: 'http://public.myapp:300',
13+
docs: {
14+
name: 'Public Service',
15+
endpoint: 'swagger.json',
16+
type: 'swagger'
17+
}
1318
}, {
1419
prefix: '/admin',
1520
target: 'http://admin.myapp:3000',

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ const gateway = (opts) => {
1818
server.use(middleware)
1919
})
2020

21+
// registering services.json
22+
const services = opts.routes.map(route => ({
23+
prefix: route.prefix,
24+
docs: route.docs
25+
}))
26+
server.get('/services.json', (req, res) => {
27+
res.send(services)
28+
})
29+
30+
// processing routes
2131
opts.routes.forEach(route => {
2232
if (undefined === route.prefixRewrite) {
2333
route.prefixRewrite = ''

test/config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ module.exports = async () => {
2323
}
2424
}, {
2525
prefix: '/users',
26-
target: 'http://localhost:3000'
26+
target: 'http://localhost:3000',
27+
docs: {
28+
name: 'Users Service',
29+
endpoint: 'swagger.json',
30+
type: 'swagger'
31+
}
2732
}, {
2833
prefix: '/users/proxy-aborted',
2934
target: 'http://localhost:5000',

test/smoke.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ describe('API Gateway', () => {
4747
await remote.start(3000)
4848
})
4949

50+
it('services.json contains registered services', async () => {
51+
await request(gateway)
52+
.get('/services.json')
53+
.expect(200)
54+
.then((response) => {
55+
expect(response.body.find(service => service.prefix === '/users')).to.deep.equal({
56+
prefix: '/users',
57+
docs: {
58+
name: 'Users Service',
59+
endpoint: 'swagger.json',
60+
type: 'swagger'
61+
}
62+
})
63+
})
64+
})
65+
5066
it('remote is proxied /users/response-time/204 - 204', async () => {
5167
await request(gateway)
5268
.post('/users/response-time/204')

0 commit comments

Comments
 (0)