reverseproxy

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2025 License: Apache-2.0 Imports: 8 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 through enviroment variable by using the following convention:

name: PROXY_RULE_<SERVICENAME> value: /<pathprefix>/>https://<host:port>

The incoming requests having the specified path prefix will be completely redirected to the given host, removing the path prefix and appending the rest of the URL as long as any headers, cookies, query params.

Example:

package main

import (
	"log"
	"net/http"
	"os"

	"github.com/gyozatech/temaki/reverseproxy"
	"github.com/gyozatech/temaki/middlewares"
)

func main() {

	os.Setenv("PROXY_RULE_S1", "/weather/>https://api.weather.com")
	os.Setenv("PROXY_RULE_S2", "/geo/>https://api.geo.com")

	routes := reverseproxy.CollectPathPrefixRoutesFromEnvVar()
	// or alternatively, you can initialize directly the PathPrefixRoutesMap: 
	/* 
           routes := reverseproxy.PathPrefixRoutesMap{
		"weather": "https://api.weather.com",
		"geo": "https://api.geo.com",
	}
        */

	log.Fatalf("Server error: %s", reverseproxy.New(routes).
		WithMiddlewares(middlewares.RequestLoggerMiddleware).
		WithModifyRequest(modifyReqFn).
		WithModifyResponse(modifyRespFn).
		Start(8080))
}

func modifyReqFn(req *http.Request){
   req.Header.Set("X-Custom-Header", "ModifiedByProxy")
}

func modifyRespFn(resp *http.Response) error {
	resp.Header.Add("X-Proxy-Modified", "true")
	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

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

Middleware represent a HTTP middleware for all incoming requests

type ModifyRequest added in v0.1.10

type ModifyRequest func(req *http.Request)

ModifyRequest is the function to adapt the request before proxying it

type ModifyResponse added in v0.1.10

type ModifyResponse func(resp *http.Response) error

ModifyResponse is the function to adapt the response before returning it to the user

type PathPrefix added in v0.1.6

type PathPrefix string

PathPrefix is the prefix of the path to find a match in the request path

type PathPrefixRoutesMap added in v0.1.6

type PathPrefixRoutesMap map[PathPrefix]TargetHost

PathPrefixRoutesMap is the map of PathPrefixes and TargetHosts to map the available routes for the reverse proxy

func CollectPathPrefixRoutesFromEnvVar added in v0.1.6

func CollectPathPrefixRoutesFromEnvVar() PathPrefixRoutesMap

CollectPathPrefixRoutesFromEnvVar collects the env vars like: PROXY_RULE_WEATHER_API=/weather/>https://api.weather.com PROXY_RULE_WEATHER_API=/geo/>https://api.geo.com This means that a call to:

https://<proxy-host>/weather/v1/today?zone=EU

will be sent to:

https://api.weather.com/v1/today?zone=EU

type ReverseProxy

type ReverseProxy struct {
	// contains filtered or unexported fields
}

ReverseProxy is the instance of the reverse proxy server

func New added in v0.1.6

func New(routes PathPrefixRoutesMap) *ReverseProxy

New is used to create a new instance of a reverse proxy

func (*ReverseProxy) Start

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

Start starts the reverse proxy on the specified port

func (*ReverseProxy) WithMiddlewares added in v0.1.6

func (rp *ReverseProxy) WithMiddlewares(middlewares ...Middleware) *ReverseProxy

WithMiddlewares allows specifying the http middleware to be applied to all routes

func (*ReverseProxy) WithModifyRequest added in v0.1.10

func (rp *ReverseProxy) WithModifyRequest(mreq ModifyRequest) *ReverseProxy

WithModifyRequest adds a request modifier to the reverse proxy

func (*ReverseProxy) WithModifyResponse added in v0.1.10

func (rp *ReverseProxy) WithModifyResponse(mresp ModifyResponse) *ReverseProxy

WithModifyResponse adds a response modifier to the reverse proxy

type TargetHost added in v0.1.6

type TargetHost string

TargetHost is the destination host to which to proxy the whole request after the PathPrefix removal

Directories

Path Synopsis
reverse proxy service: it's using env vars to register the routes to the backend services s1 and s2 based on the prefix.
reverse proxy service: it's using env vars to register the routes to the backend services s1 and s2 based on the prefix.

Jump to

Keyboard shortcuts

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