Skip to content
/ mux Public

Simple, versatile and composable multiplexer for vinxi (merged into vinxi/vinxi)

License

Notifications You must be signed in to change notification settings

vinxi/mux

Repository files navigation

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

About

Simple, versatile and composable multiplexer for vinxi (merged into vinxi/vinxi)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages