BackendStack21 / fast-gateway

Fast-gateway is an easy to use Node.js API gateway framework built to handle large scale API traffic with great performance and scalability. Written with JavaScript, it is a framework for the masses!
MIT License
311 stars 35 forks source link

Internal route for health check #29

Closed maxmartinezc closed 3 years ago

maxmartinezc commented 4 years ago

Hello, is it possible to implement an internal router for health check (/ health, / readiness, / liveness) ?

Thank's

maxmartinezc commented 4 years ago

My workaround is:

const server = gateway({ middlewares: [ Cors(), bodyParser.json() ], routes: [{ prefix: '/health', methods: ['GET'], hooks: { async onRequest(req, res) { res.send({ status: 'UP' }); return true; } } }] });

jkyberneees commented 4 years ago

Hi @maxmartinezc, excuses for reaching out a bit late. You can simply setup your health check using restana routes. Please see example below:

const service = gateway({
  routes: [{
    prefix: '/service',
    target: 'http://127.0.0.1:3000'
  }]
})

service.get('/health', (req, res) => {
  res.send({
    status: 'OK'
  })
})

service.start(PORT).then(server => {
  console.log(`API Gateway listening on ${PORT} port!`)
})

Your solution also works, but I would take this one instead for performance reasons.

Thanks again for reaching out.

angherq commented 3 years ago

I am doing something similar but I have a global middleware enabled in the gateway obj, it looks like the middleware is getting executed. Is there a way to prevent the middleware to run when we go to http://127.0.0.1:3000/whateverNotMatchingThePrefix. Hope to be clear!

jkyberneees commented 3 years ago

Hi @angherq, thanks for reaching out. Can you please attach some code snippet that can describe your configuration?

Thanks