Skip to content

Commit 50ac0e4

Browse files
Rolando Santamaria MasoRolando Santamaria Maso
authored andcommitted
adding multi proxy load balancer example
1 parent f83dece commit 50ac0e4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
3+
const gateway = require('../index')
4+
const { P2cBalancer } = require('load-balancers')
5+
const lambdaProxy = require('http-lambda-proxy')
6+
7+
// @TODO: update the list of target origins or proxy instances
8+
const targets = [
9+
'http://localhost:3000',
10+
lambdaProxy({
11+
target: process.env.FUNCTION_NAME,
12+
region: process.env.AWS_REGION
13+
})
14+
]
15+
const balancer = new P2cBalancer(targets.length)
16+
17+
gateway({
18+
routes: [{
19+
proxyHandler: (req, res, url, proxy, proxyOpts) => {
20+
const target = targets[balancer.pick()]
21+
if (typeof target === 'string') {
22+
proxyOpts.base = target
23+
} else {
24+
proxy = target
25+
}
26+
27+
return proxy(req, res, url, proxyOpts)
28+
},
29+
prefix: '/balanced'
30+
}]
31+
}).start(8080).then(() => console.log('API Gateway listening on 8080 port!'))
32+
33+
const service = require('restana')({})
34+
service
35+
.get('/get', (req, res) => res.send({ msg: 'Hello from service 1!' }))
36+
.start(3000).then(() => console.log('Public service listening on 3000 port!'))
37+
38+
// Usage: curl 'http://localhost:8080/balanced/get'

0 commit comments

Comments
 (0)