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

SSL connection? #66

Closed Capurim closed 3 years ago

Capurim commented 3 years ago

Hello, Is it possible to handle SSL/TLS connection and set our certificate and keys?

With https library we can add credential as option like this:

const credentials = {
  key: /* my key */,
  cert: /* my certificate */
}

https.createServer(credentials, app).listen(443, () => {
  console.log('Server started on port 443')
})

I don't see something like that in your doc. Thank you. Capu

jkyberneees commented 3 years ago

Hi @Capurim, fast-gateway uses restana as default HTTP server. Therefore you can do:

'use strict'

const gateway = require('fast-gateway')
const PORT = process.env.PORT || 4443
const https = require('https')
const pem = require('pem')

pem.createCertificate({
  days: 1,
  selfSigned: true
}, (err, keys) => {
  if (err) console.error(err)

  gateway({
    restana: {
      server: https.createServer({
        key: keys.serviceKey,
        cert: keys.certificate
      })
    },
    middlewares: [
    ],

    routes: [{
      prefix: '/api',
      target: 'http://localhost:3000'
    }]
  }).start(PORT).then(server => {
    console.log(`API Gateway listening on ${PORT} port!`)
  })

  const api = require('restana')({})
  api.get('/ssl-protected', (req, res) => {
    res.send('SSL Terminated!')
  })

  api.start(3000).then(() => console.log('API service listening on 3000 port!'))
})

Then: https://localhost:4443/api/ssl-protected

PS: This demo is now added to the repository.