Documentation ¶
Index ¶
- func After(h http.Handler, middleware ...Middleware) http.Handler
- func Around(h http.Handler, middleware ...Middleware) http.Handler
- func Before(h http.Handler, middleware ...Middleware) http.Handler
- type Chain
- type FileMapping
- type Group
- func (g *Group) Delete(pattern string, handler http.Handler)
- func (g *Group) Get(pattern string, handler http.Handler)
- func (g *Group) GetHandler(pattern string, handler http.Handler)
- func (g *Group) Group(group GroupFunc) *Group
- func (g *Group) HandleFunc(pattern string, handler http.Handler)
- func (g *Group) Head(pattern string, handler http.Handler)
- func (g *Group) Options(pattern string, handler http.Handler)
- func (g *Group) Patch(pattern string, handler http.Handler)
- func (g *Group) Post(pattern string, handler http.Handler)
- func (g *Group) PrefixGroup(prefix string, group GroupFunc) *Group
- func (g *Group) Put(pattern string, handler http.Handler)
- func (g *Group) Use(middleware ...Middleware)
- type GroupFunc
- type ListInfo
- type Middleware
- type Mux
- func (m *Mux) Delete(pattern string, handler http.Handler)
- func (m *Mux) DumpRoutes() (string, error)
- func (m *Mux) Get(pattern string, handler http.Handler)
- func (m *Mux) Group(group GroupFunc) *Group
- func (m *Mux) HandleFunc(pattern string, handler http.Handler)
- func (m *Mux) Head(pattern string, handler http.Handler)
- func (m *Mux) Home(handler http.Handler)
- func (m *Mux) ListRoutes() []ListInfo
- func (m *Mux) NotFound(handler http.Handler)
- func (m *Mux) Options(pattern string, handler http.Handler)
- func (m *Mux) Patch(pattern string, handler http.Handler)
- func (m *Mux) Post(pattern string, handler http.Handler)
- func (m *Mux) PrefixGroup(prefix string, group GroupFunc) *Group
- func (m *Mux) Put(pattern string, handler http.Handler)
- func (m *Mux) ServeDirectory(pattern string, fs http.FileSystem) error
- func (m *Mux) ServeDirectoryWithPrefix(pattern string, fsPrefix string, fs http.FileSystem) error
- func (m *Mux) ServeFileFrom(urlPath string, fs http.FileSystem, filePath string)
- func (m *Mux) ServeFiles(fs http.FileSystem, urlPrefix string, paths ...any) error
- func (m *Mux) Use(middleware ...Middleware)
- type Route
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
func After(h http.Handler, middleware ...Middleware) http.Handler
After wraps a handler with the provided middleware in reverse order. It returns the resulting http.Handler. This is useful for applying middleware that should run after the main handler
Example: h := wrap.After(myHandler, middleware1, middleware2) // h is now wrapped with middleware2 and middleware1, reversed
func Around ¶
func Around(h http.Handler, middleware ...Middleware) http.Handler
Around wraps a handler with the provided middleware in the order they are passed It return the resulting http.Handler. So, it's mostly useful for on-the-fly middleware application.
Example: h := wrap.Around(myHandler, middleware1, middleware2) // h is now wrapped with middleware1 and middleware2
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain represents an immutable chain of http.Handler middleware
This is essentially the same as the `github.com/justinas/alice` package, which allows for chaining middleware in a clean and reusable way. All credit goes to `github.com/justinas`.
I reimplemented it with a new name to match my needs, and added some additional functionality to it. All credit goes to `github.com/justinas`.
func NewChain ¶
func NewChain(middleware ...Middleware) Chain
NewChain creates a new middleware chain, memoizing the middlewares
func (Chain) Append ¶
func (c Chain) Append(middleware ...Middleware) Chain
Append adds additional middleware to the chain and returns a new chain 1. It returns a new chain with the combined middlewares 2. The original chain remains unchanged 3. This allows for composing middleware chains easily
Example: chain := wrap.New(middleware1).Append(middleware2, middleware3)
func (Chain) Then ¶
Then chains the middleware to the given http.Handler and returns the resulting http.Handler Chains can be safely reused, and the original chain remains unchanged.
Example:
chain := wrap.New(middleware1, middleware2) pipe1 := chain.Then(anotherHandler) pipe2 := chain.Then(yetAnotherHandler)
func (Chain) ThenFunc ¶
func (c Chain) ThenFunc(fn http.HandlerFunc) http.Handler
ThenFunc wraps the given handler function with all middleware in the chain and returns the resulting http.Handler. If the provided handler function is nil, it returns a handler that does nothing.
Example: chain := wrap.New(middleware1, middleware2) pipe1 := chain.ThenFunc(myHandlerFunc) pipe2 : = chain.ThenFunc(anotherHandlerFunc)
type FileMapping ¶
type FileMapping struct { URLPath string // The URL path where the file will be served FilePath string // The path to the file within the filesystem }
FileMapping represents a mapping between a URL path and a filesystem path
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group represents a collection of routes with a common prefix and middleware
func (*Group) GetHandler ¶
GetHandler registers a GET handler within the group with a handler that returns an error
func (*Group) Group ¶
Group creates a nested group at the current prefix level and applies the provided group function
func (*Group) HandleFunc ¶
HandleFunc registers a handler without method restrictions
func (*Group) PrefixGroup ¶
PrefixGroup creates a nested group with a common prefix and applies the provided group function
func (*Group) Use ¶
func (g *Group) Use(middleware ...Middleware)
Use registers middleware with the group
type GroupFunc ¶
type GroupFunc func(g *Group)
GroupFunc is a function that configures a route group
type Middleware ¶
Middleware represents a function that wraps an http.Handler with additional functionality
type Mux ¶
Mux extends http.ServeMux with additional routing features. It also provides a middleware chain for adding middleware to routes.
func (*Mux) DumpRoutes ¶
DumpRoutes returns a JSON representation of all routes
func (*Mux) HandleFunc ¶
HandleFunc registers a handler without method restrictions
func (*Mux) ListRoutes ¶
ListRoutes returns a list of all registered routes
func (*Mux) PrefixGroup ¶
PrefixGroup creates a new route group with the given prefix and applies the given group configuration function.
func (*Mux) ServeDirectory ¶
func (m *Mux) ServeDirectory(pattern string, fs http.FileSystem) error
ServeDirectory serves all files under a filesystem directory, matching the URL paths directly. The provided pattern must use Go 1.22's enhanced patterns, e.g. "/static/{file...}"
Requirements:
- pattern must contain the wildcard pattern {file...} (e.g. "/static/{file...}")
- fs cannot be nil
Returns an error if the pattern is invalid or missing the {file...} suffix.
func (*Mux) ServeDirectoryWithPrefix ¶
ServeDirectoryWithPrefix serves files that exist under fsPrefix in the filesystem at URLs matching the provided pattern. It requires Go 1.22's enhanced patterns to indicate file paths.
Requirements:
- pattern must contain the wildcard pattern {file...} (e.g. "/foo/{file...}")
- fsPrefix must start with "/" and not end with "/"
- fs cannot be nil
Example: ServeDirectoryWithPrefix("/foo/{file...}", "/uploads", fs) will serve files that exist at "/uploads/image.jpg" in the filesystem when requested at "/foo/image.jpg"
Returns an error if any of the requirements are not met.
func (*Mux) ServeFileFrom ¶
func (m *Mux) ServeFileFrom(urlPath string, fs http.FileSystem, filePath string)
ServeFileFrom serves a single file from a filesystem at a specific URL path. This is useful when you want to serve a specific file at a custom URL path.
Example:
mux.ServeFileFrom("/favicon.ico", http.Dir("static"), "favicon.ico") This will serve the "favicon.ico" file from the "static" directory at the "/favicon.ico" URL path.
func (*Mux) ServeFiles ¶
ServeFiles registers multiple individual files from a filesystem at their respective paths. Files can be specified using their full path within the filesystem. If urlPrefix is provided (e.g., "/assets"), files will be served under that URL path. If urlPrefix is empty (""), files will be served at the root level. The paths argument can be a mix of strings and FileMapping structs.
Examples:
1. Serve files at root level router.ServeFiles(http.FS(root.Files), "", // empty prefix serves at root
"/foo/bar.png", // Serves at /bar.png "/icons/main.png", // Serves at /main.png
)
2. Serve files under a URL prefix router.ServeFiles(http.FS(root.Files), "/assets",
"/foo/bar.png", // Serves at /assets/bar.png "/icons/main.png", // Serves at /assets/main.png
)
3. Mix and match with custom URL paths router.ServeFiles(http.FS(root.Files), "/special",
"/foo/bar.png", // Serves at /special/bar.png FileMapping{ // Custom URL path still respects prefix URLPath: "icons/custom.png", FilePath: "/icons/main.png", }, // Serves at /special/icons/custom.png
)
4. Use mappings at root level router.ServeFiles(http.FS(root.Files), "",
FileMapping{ URLPath: "/site-icon.png", FilePath: "/icons/main.png", },
)
Directories ¶
Path | Synopsis |
---|---|
Package middleware provides HTTP middleware components for the route package.
|
Package middleware provides HTTP middleware components for the route package. |