reverseproxy

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

Reverse Proxy

License made-with-Go Open Source Love svg1

This package contains a simple HTTP/S Reverse Proxy meant to be imported in your application and configured programmatically and through environment variables.

How it works

You must list your services though enviroment variable by using the following convention:

name: <SERVICENAME>_PROXY_URL value: (HOST)/common/path/

to get all the calls coming to the reverse proxy host at the path /common/path/* to be proxied to their respective hosts.

For example if you have your reverse proxy server listening to http://localhost:8080 and your backend service listening to http://localhost:8081/service-1/api/v1/*

You can get the calls coming to your reverse proxy host at the paths http://localhost:8080/service-1/api/v1/* to be all proxied to the backend service at the paths http://localhost:8081/service-1/api/v1/*.

This mechanism can be created by setting the following environment variable:

export SERVICE_1_PROXY_URL='(http://localhost:8081)/service-1/api/v1/'

And by programming your reverse proxy this way:

package main

import (
	"log"
	"github.com/gyozatech/temaki/reverseproxy"
	"github.com/gyozatech/temaki/middlewares"
	// "github.com/gyozatech/noodlog" // for logger a)
)

func main() {

	var logger *log.Logger = log.New(os.Stdout, "", log.LstdFlags)
	middlewares.SetLogger(logger)
	// alternative with gyozatech/noodlog:
	// middlewares.SetLogger(noodlog.NewLogger().EnableTraceCaller())
	
	log.Fatal(reverseproxy.NewReverseProxy().
    	           UseMiddleware(middlewares.RequestLoggerMiddleware).
			       UseMiddleware(middlewares.CORSMiddleware).
			       UseMiddleware(middlewares.RecoverPanicMiddleware).
			       Start(8080))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProxyRequestHandler

func ProxyRequestHandler(proxy *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request)

ProxyRequestHandler handles the http request using proxy

Types

type ErrorHandler

type ErrorHandler func() func(http.ResponseWriter, *http.Request, error)

type Middleware

type Middleware func(handler http.Handler) http.Handler

type RequestModifier

type RequestModifier func(req *http.Request)

type ResponseModifier

type ResponseModifier func(*http.Response) error

type ReverseProxy

type ReverseProxy struct {
	Middlewares  *[]Middleware
	ReqModifier  *RequestModifier
	RespModifier *ResponseModifier
	ErrHandler   *ErrorHandler
}

func NewReverseProxy

func NewReverseProxy() *ReverseProxy

func (*ReverseProxy) NewProxy

func (rp *ReverseProxy) NewProxy(targetHost string) (*httputil.ReverseProxy, error)

func (*ReverseProxy) Start

func (rp *ReverseProxy) Start(port int) error

func (*ReverseProxy) UseErrorHandler

func (rp *ReverseProxy) UseErrorHandler(errorHandler ErrorHandler) *ReverseProxy

func (*ReverseProxy) UseMiddleware

func (rp *ReverseProxy) UseMiddleware(middleware ...Middleware) *ReverseProxy

func (*ReverseProxy) UseRequestModifier

func (rp *ReverseProxy) UseRequestModifier(requestModifier RequestModifier) *ReverseProxy

func (*ReverseProxy) UseResponseModifier

func (rp *ReverseProxy) UseResponseModifier(responseModifier ResponseModifier) *ReverseProxy

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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