starfish

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package starfish provides a library for building a HTTP edge router. For every request, it will check against a list of matching functions, and serve the request with the appropriate handler. This can be used to build reverse proxies, file servers, edge gateways, etc - see the examples for inspiration.

Index

Examples

Constants

View Source
const Always = BoolMatcher(true)

Always is a Matcher which always matches

Variables

View Source
var Append = defaultRouter.Append

Append routes to the default router

BadGateway always gives a 502

View Source
var Clear = defaultRouter.Clear

Clear the routes of the default router

View Source
var ListenAndServe = defaultRouter.ListenAndServe

ListenAndServe serves HTTP on the default router

View Source
var ListenAndServeTLS = defaultRouter.ListenAndServeTLS

ListenAndServeTLS serves HTTPS on the default router

View Source
var MatchFunc = defaultRouter.MatchFunc

MatchFunc calls the default router's MatchFunc

Never is a Matcher which never matches

View Source
var Pop = defaultRouter.Pop

Pop a route from the default router

View Source
var PopN = defaultRouter.PopN

PopN routes from the default router

View Source
var Push = defaultRouter.Push

Push a route to the default router

View Source
var Replace = defaultRouter.Replace

Replace the routes of the default router

View Source
var Routes = defaultRouter.Routes

Routes of the default router

Functions

func Proxy

func Proxy(upstream string) http.Handler

Proxy creates a new proxy to the given host. If the URL is invalid, it'll serve a BadGateway instead.

Example
fooHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello from upstream!")
})
fooServer := httptest.NewServer(fooHandler)
fooURL, _ := url.Parse(fooServer.URL)
mainHandler := Proxy(fooURL)
r := httptest.NewRequest(http.MethodGet, "https://example.com", nil)
w := httptest.NewRecorder()
mainHandler.ServeHTTP(w, r)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
Output:

Hello from upstream!

func ProxyURL

func ProxyURL(origin *url.URL) http.Handler

ProxyURL creates a new proxy (using the scheme) to the given host.

func ServeHTTP

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

ServeHTTP calls the default router's ServeHTTP

func Static

func Static(path string) http.Handler

Static creates a new http.FileServer for the given directory.

Types

type BoolMatcher

type BoolMatcher bool

BoolMatcher gives a Matcher which always either matches or not, depending on its underlying value

func (BoolMatcher) Match

func (m BoolMatcher) Match(r *http.Request) bool

Match implements Matcher for m

type Handler

type Handler interface {
	Handle(http.Handler) Route
	HandleFunc(func(http.ResponseWriter, *http.Request)) Route
}

A Handler should have a Handle function which returns a Route

func Match

func Match(matcher Matcher) Handler

Match calls the default router's Match

type HandlerFunc

type HandlerFunc func(http.Handler) Route

A HandlerFunc is an adapter to allow the use of ordinary functions as Handlers.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(h http.Handler) Route

Handle calls f(h).

func (HandlerFunc) HandleFunc

func (f HandlerFunc) HandleFunc(h func(http.ResponseWriter, *http.Request)) Route

HandleFunc calls f(http.HandlerFunc(h))

type Matcher

type Matcher interface {
	Match(*http.Request) bool
}

A Matcher can check a http.Request to see if it should serve it

type MatcherFunc

type MatcherFunc func(*http.Request) bool

A MatcherFunc is an adapter to allow the use of ordinary functions as Matchers.

func (MatcherFunc) Match

func (f MatcherFunc) Match(r *http.Request) bool

Match calls f(r).

type Route

type Route interface {
	Matcher
	http.Handler
}

A Route is a http.Handler and a Matcher

type Router

type Router []Route

A Router is essentially a safely accessible list of Routes that also implements http.Handler.

Example
host := func(host string) Matcher {
	return MatcherFunc(func(r *http.Request) bool { return r.Host == host })
}
stringer := func(s string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, s)
	})
}
r := new(Router)
test := func(url string) {
	w := httptest.NewRecorder()
	r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, url, nil))
	resp := w.Result()
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}
r.Match(host("foo.com")).Handle(stringer("Hello from foo!"))
test("http://foo.com")
test("http://bar.com")

r.Match(host("bar.com")).Handle(stringer("Hello from bar!"))
test("http://foo.com")
test("http://bar.com")

r.Pop()
test("http://foo.com")
test("http://bar.com")

r.Clear()
test("http://foo.com")
test("http://bar.com")

r.MatchFunc(func(r *http.Request) bool { return true }).HandleFunc(http.NotFound)
Output:

Hello from foo!
404 page not found

Hello from foo!
Hello from bar!
Hello from foo!
404 page not found

404 page not found

404 page not found

func (*Router) Append

func (router *Router) Append(routes ...Route)

Append appends Routes to the Router

func (*Router) Clear

func (router *Router) Clear()

Clear removes all the routes in the router

func (*Router) ListenAndServe

func (router *Router) ListenAndServe(addr string) error

ListenAndServe listens on the given port and serves HTTP

func (*Router) ListenAndServeTLS

func (router *Router) ListenAndServeTLS(addr, cert, key string) error

ListenAndServeTLS listens on the given port and serves HTTPS

func (*Router) Match

func (router *Router) Match(matcher Matcher) Handler

Match returns a Handler for the given matcher

func (*Router) MatchFunc

func (router *Router) MatchFunc(matchFunc func(*http.Request) bool) Handler

MatchFunc returns a Handler for the matcher

func (*Router) Pop

func (router *Router) Pop() Route

Pop removes a Route from the Router (and returns it)

func (*Router) PopN

func (router *Router) PopN(n int) []Route

PopN removes n routes from the Router

func (*Router) Push

func (router *Router) Push(route Route)

Push pushes a route to the router

func (*Router) Replace

func (router *Router) Replace(newRoutes []Route) []Route

Replace replaces all the routes in the Router, and returns the old set

func (*Router) Routes

func (router *Router) Routes() []Route

Routes safely returns a copy of the Router's routes

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP uses the first matching route to respond to the http request.

Jump to

Keyboard shortcuts

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