Skip to content

Latest commit

 

History

History
96 lines (71 loc) · 2.57 KB

README.md

File metadata and controls

96 lines (71 loc) · 2.57 KB

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.