mux

package
v0.0.0-...-3f1871c Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2017 License: MIT, MIT Imports: 3 Imported by: 0

README

mux Build Status GoDoc API Coverage Status Go Report Card

Simple, versatile, general purpose HTTP multiplexer for vinxi supporting multiple matching/filtering rules and easy composition capabilities.

This is more a convenient solution than very efficient one. If you're looking for great performance, simply pick another solution.

Installation

go get -u gopkg.in/vinxi/mux.v0

API

See godoc reference.

Examples

Simple multiplexer
package main

import (
  "fmt"
  "gopkg.in/vinxi/mux.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  m := mux.New()
  m.If(mux.MatchMethod("GET", "POST"), mux.MatchPath("^/foo"))

  m.Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
    w.Header().Set("Server", "vinxi")
    h.ServeHTTP(w, r)
  })

  m.Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
    w.Write([]byte("foo"))
  })

  vs.Use(m)
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", 3100)
  err := vs.Listen()
  if err != nil {
    fmt.Printf("Error: %s\n", err)
  }
}
Composition
package main

import (
  "fmt"
  "gopkg.in/vinxi/mux.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  // Create a custom multiplexer for /ip path
  ip := mux.If(mux.Path("^/ip"))
  ip.Use(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte(r.RemoteAddr))
  })

  // Create a custom multiplexer for /headers path
  headers := mux.If(mux.Path("^/headers"))
  headers.Use(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte(fmt.Errorf("Headers: %#v", r.Header).Error()))
  })

  // Creates the root multiplexer who host both multiplexers
  m := mux.New()
  m.If(mux.MatchMethod("GET"))
  m.Use(ip)
  m.Use(headers)

  // Register the multiplexer in the vinxi
  vs.Use(m)
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", 3100)
  err := vs.Listen()
  if err != nil {
    fmt.Printf("Error: %s\n", err)
  }
}
Custom matcher function
package main

import (
  "fmt"
  "gopkg.in/vinxi/mux.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  m := mux.New()

  // Register a custom matcher function
  m.If(func(req *http.Request) bool {
    return req.Method == "GET" && req.RequestURI == "/foo"
  })

  m.Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
    w.Header().Set("Server", "vinxi")
    h.ServeHTTP(w, r)
  })

  m.Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
    w.Write([]byte("foo"))
  })

  vs.Use(m)
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", 3100)
  err := vs.Listen()
  if err != nil {
    fmt.Printf("Error: %s\n", err)
  }
}

License

MIT

Documentation

Overview

Package mux implements an HTTP domain-specific traffic multiplexer with built-in matchers and features for easy plugin composition and activable logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher func(*http.Request) bool

Matcher represents the function interface implemented by matchers

func MatchHeader

func MatchHeader(key, pattern string) Matcher

MatchHeader matches a given header key and value againts the request.

func MatchHost

func MatchHost(pattern string) Matcher

MatchHost matches the given host string in the incoming request.

func MatchMethod

func MatchMethod(methods ...string) Matcher

MatchMethod matches the HTTP method name againts the request.

func MatchPath

func MatchPath(pattern string) Matcher

MatchPath matches the given path patterns againts the incoming request.

func MatchQuery

func MatchQuery(key, pattern string) Matcher

MatchQuery matches a given query param againts the request.

type Mux

type Mux struct {
	// Matchers stores a list of matcher functions.
	Matchers []Matcher

	// Layer stores the multiplexer middleware layer.
	Layer *layer.Layer
}

Mux is a HTTP request/response/error multiplexer who implements both middleware and plugin interfaces. It has been designed for easy plugin composition based on HTTP matchers/filters.

func Every

func Every(muxes ...*Mux) *Mux

Every is an alias to If().

func Header(key, pattern string) *Mux

Header returns a new multiplexer who matches an HTTP request header field based on the given key and regexp pattern.

func Host

func Host(pattern string) *Mux

Host returns a new multiplexer who matches an HTTP request URL host based on the given regexp pattern.

func If

func If(muxes ...*Mux) *Mux

If creates a new multiplexer that will be executed if all the mux matchers passes.

func Match

func Match(matchers ...Matcher) *Mux

Match creates a new multiplexer based on a given matcher function.

func Method

func Method(methods ...string) *Mux

Method returns a new multiplexer who matches an HTTP request based on the given method/s.

func New

func New() *Mux

New creates a new multiplexer with default settings.

func Or

func Or(muxes ...*Mux) *Mux

Or creates a new multiplexer that will be executed if at least one mux matcher passes.

func Path

func Path(pattern string) *Mux

Path returns a new multiplexer who matches an HTTP request path based on the given regexp pattern.

func Query

func Query(key, pattern string) *Mux

Query returns a new multiplexer who matches an HTTP request query param based on the given key and regexp pattern.

func Some

func Some(muxes ...*Mux) *Mux

Some is an alias to Or().

func (*Mux) AddMatcher

func (m *Mux) AddMatcher(matchers ...Matcher) *Mux

AddMatcher adds a new matcher function in the current mumultiplexer matchers stack.

func (*Mux) HandleHTTP

func (m *Mux) HandleHTTP(w http.ResponseWriter, r *http.Request, h http.Handler)

HandleHTTP returns the function handler to match an incoming HTTP transacion and trigger the equivalent middleware phase.

func (*Mux) If

func (m *Mux) If(matchers ...Matcher) *Mux

If is a semantic alias to AddMatcher.

func (*Mux) Match

func (m *Mux) Match(req *http.Request) bool

Match matches the give Context againts a list of matchers and returns `true` if all the matchers passed.

func (*Mux) Some

func (m *Mux) Some(matchers ...Matcher) *Mux

Some matches the incoming request if at least one of the matchers passes.

func (*Mux) Use

func (m *Mux) Use(handler interface{}) *Mux

Use registers a new plugin in the middleware stack.

func (*Mux) UseFinalHandler

func (m *Mux) UseFinalHandler(handler http.Handler) *Mux

UseFinalHandler registers a new plugin in the middleware stack.

func (*Mux) UsePhase

func (m *Mux) UsePhase(phase string, handler interface{}) *Mux

UsePhase registers a new plugin in the middleware stack.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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