Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

open-webtech/go-api-gateway

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go API Gateway

This project is a simple, but flexible and extensible API gateway written in Go. It features a variety of features that make it easy to manage API routes.

Features

  • Custom Handlers: Use middlewares and other handlers of your choice to handle incoming requests.
  • Response Formatting: Format your responses in the way that best suits your API's needs. Currently, JSON and plain text are supported.
  • Authentication: Authenticate your users with basic or bearer token authentication.
  • Logging: Keep track of your API's usage with access logs.

This project is still under development, and more features are planned for the future. Contributions are welcome!

Installation

To install go-api-gateway, use the following command:

go get -u github.com/open-webtech/go-api-gateway

Usage

To use go-api-gateway, you first need to create a services.Context for storing your services (e.g. responder, loggers, etc.).

For convenience, it's recommended to create a HandlerFactory then. This allows you to generate the handlers you wish to use, such as reverse proxies, preconfigured with your desired services and settings.

Now, you can create a Gateway and add your entrypoints to it. Finally, you can start the gateway by calling ListenAndServe on it.

package main

import (
	"crypto/tls"
	"log"
	"net/http"

	"github.com/open-webtech/go-api-gateway/auth"
	"github.com/open-webtech/go-api-gateway/response/format"
	"github.com/open-webtech/go-api-gateway/gateway"
)

func main() {
	formatter := format.NewJsonFormatter()

	sc := services.NewContextWithDefaults()
	sc.Responder.SetFormatter(formatter)

	f := gateway.NewHandlerFactory(sc)
	f.RequestHeader = http.Header{
	 	"X-Foo": []string{"Bar"},
	}
	f.Transport = &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	rp, err := f.MakeReverseProxy("http://my-backend:8000")
	if err != nil {
		log.Fatal(err)
	}

	rp.PassPath("*", "/")
	rp.PassPaths("HEAD|GET|POST", "/api/version", "/api/posts")
	rp.RewritePath("HEAD|GET|POST", "/posts", "/api/posts")

	g := gateway.NewFromServiceContext(sc)
	g.HandleHost("*.api.example.com", rp)
	
	log.Fatal(g.ListenAndServe(":8080"))
}

Glossary

Entrypoint: An entrypoint is a specific host or path that the Gateway can handle. It is associated with a specific http.Handler that will process the incoming HTTP requests for that entrypoint.

Service: A service is a reusable component that provides certain functionality to the Gateway and its parts. Services can include things like responders and loggers. They are stored in a services.Context and can be accessed from there as needed.

Responder: The responder is a service that sends HTTP responses to the client. It can be configured with a specific response formatter, such as JSON or plain text.

About

📡 Simple but powerful API gateway written in Go

Topics

Resources

Code of conduct

Stars

Watchers

Forks

Releases

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.