path_proxy

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

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

Go to latest
Published: Jan 2, 2023 License: MIT Imports: 5 Imported by: 0

README

Path Proxy

Super simple way to define a proxy based on matching a url path.

Example

In this example we:

  1. start a proxy server on port :3333
  2. send all traffic that matches ^/api (starts with /api) to http://localhost:7788
  3. send everything else (/) to http://localhost:3000
package main

import (
    "fmt"
    "net/http"

    "github.com/emehrkay/path_proxy"
)

func main() {
    var err error
	handler := &PathProxyHandler{}
	err = handler.ProxyDefintions(ProxyDefSet{
		{
			"/",
			"http://localhost:3000",
		},
		{
			"^/api",
			"http://localhost:7788",
		},
	})

	if err != nil {
		panic(err)
	}

	fmt.Println("server running on port 3333")
	http.ListenAndServe(":3333", handler)
}

Route definitions are automatically ordered from longest to shortest. This is also a simple regular expression handler handler.Handle("/some/regex$", anActualHandler)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PathProxyHandler

type PathProxyHandler struct {
	Routes []*Route
	// contains filtered or unexported fields
}

func (*PathProxyHandler) HandleFunc

func (h *PathProxyHandler) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request))

func (*PathProxyHandler) Handler

func (h *PathProxyHandler) Handler(pattern *regexp.Regexp, handler http.Handler)

func (*PathProxyHandler) Proxy

func (h *PathProxyHandler) Proxy(pattern, targetUrl string) error

func (*PathProxyHandler) ProxyDefintions

func (h *PathProxyHandler) ProxyDefintions(proxies ProxyDefSet) error

func (*PathProxyHandler) ServeHTTP

func (h *PathProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ProxyDef

type ProxyDef struct {
	Pattern string
	Target  string
}

type ProxyDefSet

type ProxyDefSet []ProxyDef

type Route

type Route struct {
	Pattern *regexp.Regexp
	Handler http.Handler
}

Jump to

Keyboard shortcuts

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