Coder Social home page Coder Social logo

api-gateway's Introduction

Turbosonic: api-gateway

Build Status

A lightweight, sub-millisecond api-gateway intended for microservices on docker and monitored via Kibana

Monitoring

What does it do?

Acts as the conduit between the outside world and your internal microservice ecosystem, using a simple yaml configuration file you can set up your routing to take external requests and forward them on to internal endpoints.

Getting started

Create a configuration file

You need to create a configuration yaml file to be used when the API gateway first starts, full details are in the wiki, but here's a starter for 10 (it's the same as config.yaml on this project's root).

name: /web/v1 
endpoints:
  - url: /example
    methods:
      - method: GET
        destination:
          name: service
          host: http://my-first-service
          url: /things

This configuration will expose one endpoint, web/v1/example (which, when following these instructions, can be accessed via http://localhost:8080/web/v1/example) which will allow access to the /things endpoint on the internal service hosted at http://my-first-service

Docker networking

If you have some other containers running APIs which you want to expose using the gateway, you'll need to create a network, that's pretty easy.

$ docker network create turbosonic

Run the docker image

All of Turbosonic's services run on a scratch docker image, which means they're very small and incredibly lightweight (7mb of magic!), the following command will run the api-gateway in a container:

$ docker run -d -p 80:80 \ 
  --name api-gateway \
  --net turbosonic \
  -v /myconfigs/config.yaml:/data/config.yaml \
  turbosonic/api-gateway

This is just standard docker stuff:

  • -d is to run to in the background (daemon mode)
  • -p {external port}:80 will expose the gateway to the port you add
  • --name {a nice name} gives the container a nice name
  • -net {name of docker network} the name of the docker network (created in the last step)
  • -v {path to yaml file}:/data/config.yaml mounts your config file into the container to be used
  • turbosonic/api-gateway is the name of the image on docker hub

TLS

If you want to run your gateway with TLS (and thus an HTTPS reverse proxy), just mount your cert and private key as below:

$ docker run -d -p 443:80 \ 
  --name api-gateway \
  --net turbosonic \
  -v /myconfigs/config.yaml:/data/config.yaml \
  -v /mycerts/cert.pem:/data/certs/cert.pem \
  -v /mycerts/key.pem:/data/certs/key.pem \
  turbosonic/api-gateway

Things to come

This is the start of a journey to create a simple, secure, scalable and production ready api-gateway, to allow developers to focus on the core functionality of their systems, below is a list of things to be added in the coming months.

Coming soon...

  • Authentication - it would be pretty useless if a gateway didn't keep out the bad guys
  • Authorization - limit access to certain endpoints based on the claims of the user accessing it
  • Request bundling - collate multiple internal requests into one external request, improving latency for devices with shoddy internet connections
  • Multiple configurations - so you can have /web/v2 and /mobile/v1 etc, running simultaneously
  • Caching - hold responses in memory for ultra-fast response times
  • Mocking - return mocked data for fast prototyping and concurrent development of front and back end
  • Rate limiting - Protect your internal endpoints by adding a forcefield when users start spamming you
  • Retries - If at first you don't succeed; try, try again
  • Logging - To monitor usage, find errors, and to show off how fast your API is.

A bit further away...

  • Websockets - allow clients to connect to your application via websockets rather than http, this is when things get silly, silly fast.
  • Remote configuration - create your APIs via a web interface, then just run the docker image, it will go pick up your configuration from the web (securely) to make life so much easier.
  • Self documentation - Swagger on demand

Want to help?

This is our first foray in to the world of Golang, we used to do this stuff in node, but Go has blown our socks off with its simplicity, speed and itsy bitsy teeny weeny container images.

That means we could probably do with a bit of help, take a fork and create a pull request, but please, if you do, add and link an issue to what you are doing, we won't accept it otherwise!

api-gateway's People

Contributors

daviddykeuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

api-gateway's Issues

Fallbacks

Optional return object if the proxied request fails for any region

Keeping the system alive if something dies

Config reload

A config might want to be changed without redeploying the code, this might require a management API to be created on a different port

Authorization

Need to add scopes and rules to endpoints via configuration

Parameter handling

Dynamic parts of URLs (like IDs) need to be handled and sent to destination services

Request bundling

collate multiple internal requests in to one external request, improving latency for devices with shoddy internet connections

Authentication

It would be pretty useless if a gateway didn't keep out the bad guys

TLS

Things need to be secure, allow for adding SSL certs

Testing

This thing needs good testing, no one wants to release a ๐Ÿ’ฉ

Log to InfluxDB

For all that Grafana glory

Create a Grafana dashboard JSON

CD/CI ๐Ÿ—

Something like this:

  • Changes to master trigger new alpha docker images
  • Adding the label beta to a commit on master triggers new beta docker images
  • Adding the label release to a commit on master triggers new latest docker images

Logging Updates

Logging is really to Elasticsearch not ELK, so update to that. Also include the date in the index of the log to allow easier administration of logs and allow for custom index names in ENV files

Circit breaker

Stop forwarding requests if the underlying service has been broken for sometime

Query string handling

Everything from ? onwards on the url needs to be sent on to the destination service

Auth0 fails when no roles are provided

When providing a JWT with no roles property

2018/09/07 03:26:10 http: panic serving 10.244.0.15:49886: interface conversion: interface {} is nil, not string
goroutine 4 [running]:
net/http.(*conn).serve.func1(0x19d0e000)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1697 +0x9f
panic(0x82f55e0, 0x19e40780)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/runtime/panic.go:491 +0x1d0
github.com/turbosonic/api-gateway/initializer.checkRoles(0x19d12600, 0x19c60900, 0x3, 0x19c548b0, 0x1, 0x1, 0x19c548d0, 0x1, 0x1, 0x19c60930, ...)
	/home/travis/gopath/src/github.com/turbosonic/api-gateway/initializer/initializer.go:120 +0x108
github.com/turbosonic/api-gateway/initializer.createEndpoint.func1(0x8496660, 0x19c0a020, 0x19d12600)
	/home/travis/gopath/src/github.com/turbosonic/api-gateway/initializer/initializer.go:58 +0x77
net/http.HandlerFunc.ServeHTTP(0x19c6e280, 0x8496660, 0x19c0a020, 0x19d12600)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1918 +0x34
goji%2eio.dispatch.ServeHTTP(0x8496660, 0x19c0a020, 0x19d12600)
	/home/travis/gopath/src/goji.io/dispatch.go:17 +0x91
goji%2eio.(*dispatch).ServeHTTP(0x84e1fec, 0x8496660, 0x19c0a020, 0x19d12600)
	<autogenerated>:1 +0x40
github.com/turbosonic/api-gateway/authentication/clients/auth0.CheckJwt.func1(0x8496660, 0x19c0a020, 0x19d12180)
	/home/travis/gopath/src/github.com/turbosonic/api-gateway/authentication/clients/auth0/auth0.go:58 +0x426
net/http.HandlerFunc.ServeHTTP(0x19cb9410, 0x8496660, 0x19c0a020, 0x19d12180)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1918 +0x34
github.com/turbosonic/api-gateway/logging.LogHandler.LogHandlerFunc.func1(0x84967c0, 0x19d18000, 0x19d12180)
	/home/travis/gopath/src/github.com/turbosonic/api-gateway/logging/logging.go:61 +0xa2
net/http.HandlerFunc.ServeHTTP(0x19c59200, 0x84967c0, 0x19d18000, 0x19d12180)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1918 +0x34
github.com/turbosonic/api-gateway/responseMarshal.AddHeaders.func1(0x84967c0, 0x19d18000, 0x19d12180)
	/home/travis/gopath/src/github.com/turbosonic/api-gateway/responseMarshal/responseMarshal.go:23 +0x20f
net/http.HandlerFunc.ServeHTTP(0x19cb9420, 0x84967c0, 0x19d18000, 0x19d12180)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1918 +0x34
github.com/turbosonic/api-gateway/responseMarshal.CorsHandler.func1(0x84967c0, 0x19d18000, 0x19d12180)
	/home/travis/gopath/src/github.com/turbosonic/api-gateway/responseMarshal/responseMarshal.go:47 +0x190
net/http.HandlerFunc.ServeHTTP(0x19cb9430, 0x84967c0, 0x19d18000, 0x19d12180)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1918 +0x34
goji%2eio.(*Mux).ServeHTTP(0x19cd1440, 0x84967c0, 0x19d18000, 0x19d12000)
	/home/travis/gopath/src/goji.io/mux.go:74 +0x108
net/http.serverHandler.ServeHTTP(0x19c92300, 0x84967c0, 0x19d18000, 0x19d12000)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:2619 +0x8e
net/http.(*conn).serve(0x19d0e000, 0x8496f00, 0x19c460a0)
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:1801 +0x5d1
created by net/http.(*Server).Serve
	/home/travis/.gimme/versions/go1.9.2.linux.amd64/src/net/http/server.go:2720 +0x1f6```

Logging

To monitor usage, find errors, and to show off how fast your API is

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.