gateway

package module
v0.0.0-...-d35192a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 9 Imported by: 0

README

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/secondtruth/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/secondtruth/go-api-gateway/auth"
	"github.com/secondtruth/go-api-gateway/response/format"
	"github.com/secondtruth/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.

Documentation

Overview

Package gateway provides an API gateway that routes incoming requests to different handlers based on the host.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gateway

type Gateway struct {
	Responder response.HttpResponder
	AccessLog logging.AccessLogger
	// contains filtered or unexported fields
}

Gateway represents an API gateway that routes incoming requests to different handlers based on the host.

func New

func New() *Gateway

New creates a new instance of the API gateway.

func NewFromServiceContext

func NewFromServiceContext(sc services.Context) *Gateway

NewFromServiceContext creates a new instance of the API gateway using the provided service context.

func (*Gateway) HandleHost

func (g *Gateway) HandleHost(host string, handler http.Handler)

HandleHost associates the given host with the provided handler.

func (*Gateway) HasExactHost

func (g *Gateway) HasExactHost(host string) bool

HasExactHost checks if the gateway has an exact match for the given host.

func (*Gateway) HasHost

func (g *Gateway) HasHost(host string) bool

HasHost checks if the gateway has a handler associated with the given host.

func (*Gateway) ListenAndServe

func (g *Gateway) ListenAndServe(addr string) error

ListenAndServe starts the HTTP server and listens for incoming requests on the specified address.

func (*Gateway) ListenAndServeTLS

func (g *Gateway) ListenAndServeTLS(addr, certFile, keyFile string) error

ListenAndServeTLS starts the HTTPS server and listens for incoming requests on the specified address.

type HandlerFactory

type HandlerFactory struct {
	Transport      http.RoundTripper
	RequestHeader  http.Header
	ModifyResponse func(*http.Response) error
	Services       services.Context
}

HandlerFactory creates HTTP handlers preconfigured using the service context and other configuration options.

func NewHandlerFactory

func NewHandlerFactory(sc services.Context) *HandlerFactory

NewHandlerFactory creates a new instance of HandlerFactory with the given service context.

func (*HandlerFactory) MakeReverseProxy

func (f *HandlerFactory) MakeReverseProxy(remote string) (*reverseproxy.ReverseProxyMux, error)

MakeReverseProxy creates a preconfigured reverseproxy.ReverseProxyMux instance for the given remote host.

func (*HandlerFactory) MakeRouter

func (f *HandlerFactory) MakeRouter() *httprouter.Router

MakeRouter creates a preconfigured httprouter.Router instance.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL