Skip to content
/ router Public

Featured, composable and fast router with hierarchical middleware layer (moved to vinxi/vinxi)

License

Notifications You must be signed in to change notification settings

vinxi/router

Repository files navigation

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.

About

Featured, composable and fast router with hierarchical middleware layer (moved to vinxi/vinxi)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages