pathrouter

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: GPL-3.0 Imports: 6 Imported by: 1

README

path-router-go

path router is a http router for go using a trie data structure for routing

r := pathrouter.NewRouter()

r.Get("/", func(w http.ResponseWriter, req *http.Request, ps *pathrouter.Params) {
    fmt.Fprintf(w, "Hello, World!")
})

log.Fatal(http.ListenAndServe(":8000", r))
  • middleware

middleware can be added using the use method

middleware is applied when the route is inserted and won't change if the use function is called again

func logger(next HandlerFunc) HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request, ps *pathrouter.Params) {
        log.Println(r.Method, r.URL.String())
        next(w, r)
    }
}

r := pathrouter.NewRouter()
r.Use(logger)
  • groups

groups in path router allow url path prefixing and middleware scoped to urls

r := pathrouter.NewRouter()

api := r.Scope("/api")
api.Use(logger)

usersAPI := api.Scope("/users")
usersAPI.Use(auth)

usersAPI.Get("/", getUsers)
usersAPI.Post("/", postUsers)
usersAPI.Delete("/", postUsers)
  • url params

url parameters can be specified by prefixing the url element with a :

the maximum number of url parameters is limited to 32

/hello/:user

/hello/hehaowen00

the matched values will be stored in the parameter struct in the request context

func (w http.ResponseWriter, r *http.Request, ps *pathrouter.Params) {
    value := ps.Get(r.URL.Path, ":user") // hehaowen00
}

url parameters will only match one url segment unlike wildcards

  • wildcards

wildcards are specified using * and must be at the end of the url

urls with wildcards will terminate matching and consume the rest of the url

/static/js/*

/static/js/app/index.min.js

wildcards are stored in the parameter struct in the request context

func (w http.ResponseWriter, r *http.Request, ps *pathrouter.Params) {
    value := ps.Get(r.URL.Path, "*") // app/index.min.js
}
  • error handling

error handlers can be registered using HandleErr

currently router only uses http.StatusNotFound handler

r.HandleErr(http.StatusNotFound, func (w http.ResponseWriter, r *http.Request, ps *pathrouter.Params) {
    fmt.Fprintf(w, "Page Not Found\n")
})
  • routing conflicts

when a parameter and wildcard node are in the same position within the URL, only the parameter node will be matched and the wildcard route ignored

/special/:id
/special/*

adding another route with the same special path segment will overwrite the previous

/special/:a
/special/:b
  • compatibility

path router implements the Handle method to add http.Handler routes

r := pathrouter.NewRouter()

r.Handle("GET", "/handle", http.HandleFunc(func (w http.ResponseWriter, r *http.Request) {
    ps := r.Context().Value(pathrouter.ParamsKey).(*pathrouter.Params)
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GzipMiddleware

func GzipMiddleware(next http.HandlerFunc) http.HandlerFunc

Types

type CorsHandler

type CorsHandler struct {
	AllowedOrigins   []string
	AllowCredentials bool
}

func (*CorsHandler) Middleware

func (cors *CorsHandler) Middleware(next http.HandlerFunc) http.HandlerFunc

type GZipResponseWriter

type GZipResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (GZipResponseWriter) Write

func (rw GZipResponseWriter) Write(data []byte) (int, error)

type IRouter

type IRouter interface {
	IRoutes

	HandleErr(errorCode int, handler http.HandlerFunc)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

func NewRouter

func NewRouter() IRouter

type IRoutes

type IRoutes interface {
	Scope(prefix string) IRoutes
	Use(MiddlewareFunc)

	Handle(method, path string, handler http.Handler)

	Get(path string, handler http.HandlerFunc)
	Post(path string, handler http.HandlerFunc)
	Put(path string, handler http.HandlerFunc)
	Patch(path string, handler http.HandlerFunc)
	Delete(path string, handler http.HandlerFunc)
	Connect(paath string, handler http.HandlerFunc)
	Options(paath string, handler http.HandlerFunc)
}

type MiddlewareFunc

type MiddlewareFunc func(next http.HandlerFunc) http.HandlerFunc

type Params

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

func (*Params) Get

func (ps *Params) Get(name string) string

type Scope

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

func (*Scope) Connect

func (s *Scope) Connect(path string, handler http.HandlerFunc)

func (*Scope) Delete

func (s *Scope) Delete(path string, handler http.HandlerFunc)

func (*Scope) Get

func (s *Scope) Get(path string, handler http.HandlerFunc)

func (*Scope) Handle

func (s *Scope) Handle(method, path string, handler http.Handler)

func (*Scope) Options

func (s *Scope) Options(path string, handler http.HandlerFunc)

func (*Scope) Patch

func (s *Scope) Patch(path string, handler http.HandlerFunc)

func (*Scope) Post

func (s *Scope) Post(path string, handler http.HandlerFunc)

func (*Scope) Put

func (s *Scope) Put(path string, handler http.HandlerFunc)

func (*Scope) Scope

func (s *Scope) Scope(prefix string) IRoutes

func (*Scope) Use

func (s *Scope) Use(middleware MiddlewareFunc)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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