router

package module
v0.0.0-...-0b2fb1f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 4 Imported by: 0

README

Go-Router

go-router provides a basic wrapper around the standrd-library HTTP-server-mux to allow:

  1. Registering of middleware
  2. Sub-routers rooted at specific paths, with scoped middleware

The standard-library server-mux is entirely re-used at runtime - this library only eases the construction of the mux.

Example

root := router.New()

// if we want the '404' response to go through
// registered middleware we need to handle that
// in the router.
root.Handle("/", http.NotFoundHandler())

// create a sub-router under the '/api' prefix
api := root.New("/api")

// this handler will respond to a GET request at '/api/test'.
api.HandleFunc("GET /test", func(w http.ResponseWriter, r *http.Request) {
    log.Println("in /api/test handler")
})

// add middleware dedicated to the '/api' prefix
api.Use(func(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Println("in api middleware")
        h.ServeHTTP(w, r)
    })
})

// add a handler using the 'root' router
root.HandleFunc("GET /test/{id}", func(w http.ResponseWriter, r *http.Request) {
    log.Printf("in /test handler for id: %q", r.PathValue("id"))
})

// add middleware to the 'root' router. This will also apply to requests
// to the '/api' routes.
root.Use(func(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Println("in root middleware")
        h.ServeHTTP(w, r)
    })
})

// serve up traffic
err := http.ListenAndServe(":8080", root.Handler())
if err != nil {
    fmt.Fprintln(os.Stderr, "error: ", err)
    os.Exit(1)
}

Documentation

Overview

Package router provides a basic HTTP-router which wraps http.ServerMux.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router is similar to http.ServeMux, except is allows registering middleware and creating "sub" routers.

Handler-patterns support the same features and syntax as http.ServeMux.

func New

func New() *Router

New allocates a new Router.

func (*Router) Handle

func (r *Router) Handle(pattern string, handler http.Handler)

Handle registers a handler for a pattern. Patterns support the same syntax as http.ServerMux.

func (*Router) HandleFunc

func (r *Router) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleFunc registers a handler for a pattern. Patterns support the same syntax as http.ServerMux.

func (*Router) Handler

func (r *Router) Handler() http.Handler

Handler produces an http.Handler to server requests using the handlers and middleware registered to the router.

func (*Router) New

func (r *Router) New(prefix string) *Router

New creates a sub-router. All handlers registered to the sub-router will use the parent router's middleware. All handlers registered to the sub-router will be scoped to the passed-in path-prefix (which may be empty).

All parent-middleware will be applied before all sub-router middleware.

func (*Router) Use

func (r *Router) Use(middelware func(http.Handler) http.Handler)

Use applies a middle-ware function to all handlers (including handlers in sub-routers).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL