Featured, composable and fast router with hierarchical middleware layer (moved to vinxi/vinxi)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
_examples
.editorconfig
.gitignore
.travis.yml
History.md
LICENSE
README.md
match.go
match_test.go
route.go
route_test.go
router.go
router_test.go

README.md

router Build Status GoDoc Coverage Status Go Report Card API

Note: deprecated repository. Source has been merged into vinxi/vinxi.

Featured and fast router used by vinxi. Provides a hierarchical middleware layer.

Originally based in bmizerany/pat.

Installation

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

API

See godoc reference for detailed API documentation.

Examples

Router

package main

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

func main() {
  fmt.Printf("Server listening on port: %d\n", 3100)
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  r := router.New()
  r.Get("/get").Forward("http://httpbin.org")
  r.Get("/headers").Forward("http://httpbin.org")
  r.Get("/image/:format").Forward("http://httpbin.org")
  r.Get("/say").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello, foo"))
  }))

  vs.Use(r)
  vs.Forward("http://example.com")

  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

Virtual host like muxer router

package main

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

func main() {
  fmt.Printf("Server listening on port: %d\n", 3100)
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  r := router.New()
  r.Get("/get").Forward("http://httpbin.org")
  r.Get("/headers").Forward("http://httpbin.org")
  r.Get("/image/:format").Forward("http://httpbin.org")
  r.Get("/say").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello, foo"))
  }))

  // Create a host header multiplexer
  muxer := mux.Host("localhost:3100")
  muxer.Use(r)

  vs.Use(muxer)
  vs.Forward("http://example.com")

  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

License

MIT.