Skip to content

Latest commit

 

History

History
113 lines (88 loc) · 2.63 KB

README.md

File metadata and controls

113 lines (88 loc) · 2.63 KB

Router Build Status Coverage Status

Provides simple routing capabilities.

func main() {
    rr := router.New("/")

    rr.Get("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Hello world")
    })

    if err := http.ListenAndServe(":80", rr); err != nil {
        fmt.Printf("failed to start server: %v", err.Error())
    }
}

Subroute

func main() {
    rr := router.New("/")
    subrr := rr.SubRouter("/admin")

    rr.Get("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Hello world")
    })

    subrr.Get("/payroll", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "in /admin/payroll")
    })

    http.ListenAndServe(":80", rr)
}

Middleware

type tokenMiddleware struct { }

func (t tokenMiddleware) SetToken(r *http.Request) {
    token := generateToken()

    // middleware can halt the request
    if token == nil {
        r.WriteHeader(401)
        router.HaltRequest(r)
        return
    }
    c2 := context.WithValue(r.Context(), "key", token)
    router.BindContext(c2, r)
}

func (t tokenMiddleware) Token(c context.Context) string {
    return c.Value("key").(string)  // might want to be more cautious than this
}

func main() {
    rr := router.New("/")

    rr.Before(func (w http.ResponseWriter, r *http.Request) {
        tokenMiddleware.SetToken(r)
    })

    rr.Get("/", func(w http.ResponseWriter, r *http.Request) {
        token := tokenMiddleware.Token(r.Context())
        fmt.Fprintf(w, "TOKEN: %s", token)
    })

    http.ListenAndServe(":80", rr)
}

Extract URL params

rr.Get("/users/:name", func(w http.ResponseWriter, r *http.Request) {
    params := router.Params(r.Context())
    fmt.Fprintf(w, "Hey %s", params["name"])
})

Use handlers

type usersHandler struct {
    svc aService
}
func (h usersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    h.svc.doSomething()
}

rr.Handle("GET", "/users", usersHandler{svc: Service.New()})

404 handling

rr.NotFound(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Not found")
})

Wildcard params

// GET: /hello/go/programmer
rr.Get("/hello/*", func(w http.ResponseWriter, r *http.Request) {
    wildcard := router.Param(r.Context(), "*") // => "go/programmer"
    vals = strings.Split(wildcard, "/")
    fmt.Fprintf(w, "Hello %s %s", vals[0], vals[1])
})