route

package
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

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

func Before

func Before(h http.Handler, middleware ...Middleware) http.Handler

Before is an alias for Around to provide a more semantic API

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) Extend

func (c Chain) Extend(chain Chain) Chain

func (Chain) Then

func (c Chain) Then(h http.Handler) http.Handler

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) Delete

func (g *Group) Delete(pattern string, handler http.Handler)

Delete registers a DELETE handler within the group

func (*Group) Get

func (g *Group) Get(pattern string, handler http.Handler)

Get registers a GET handler within the group

func (*Group) GetHandler

func (g *Group) GetHandler(pattern string, handler http.Handler)

GetHandler registers a GET handler within the group with a handler that returns an error

func (*Group) Group

func (g *Group) Group(group GroupFunc) *Group

Group creates a nested group at the current prefix level and applies the provided group function

func (*Group) HandleFunc

func (g *Group) HandleFunc(pattern string, handler http.Handler)

HandleFunc registers a handler without method restrictions

func (*Group) Head

func (g *Group) Head(pattern string, handler http.Handler)

Head registers a HEAD handler within the group

func (*Group) Options

func (g *Group) Options(pattern string, handler http.Handler)

Options registers an OPTIONS handler within the group

func (*Group) Patch

func (g *Group) Patch(pattern string, handler http.Handler)

Patch registers a PATCH handler within the group

func (*Group) Post

func (g *Group) Post(pattern string, handler http.Handler)

Post registers a POST handler within the group

func (*Group) PrefixGroup

func (g *Group) PrefixGroup(prefix string, group GroupFunc) *Group

PrefixGroup creates a nested group with a common prefix and applies the provided group function

func (*Group) Put

func (g *Group) Put(pattern string, handler http.Handler)

Put registers a PUT handler within the group

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 ListInfo

type ListInfo struct {
	Pattern string   `json:"pattern"`
	Methods []string `json:"methods"`
}

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware represents a function that wraps an http.Handler with additional functionality

type Mux

type Mux struct {
	*http.ServeMux
	// contains filtered or unexported fields
}

Mux extends http.ServeMux with additional routing features. It also provides a middleware chain for adding middleware to routes.

func New

func New(middleware ...Middleware) *Mux

New creates a new Mux instance

func (*Mux) Delete

func (m *Mux) Delete(pattern string, handler http.Handler)

Delete registers a DELETE handler

func (*Mux) DumpRoutes

func (m *Mux) DumpRoutes() (string, error)

DumpRoutes returns a JSON representation of all routes

func (*Mux) Get

func (m *Mux) Get(pattern string, handler http.Handler)

Get registers a GET handler

func (*Mux) Group

func (m *Mux) Group(group GroupFunc) *Group

Group creates a new route group with the given configuration function.

func (*Mux) HandleFunc

func (m *Mux) HandleFunc(pattern string, handler http.Handler)

HandleFunc registers a handler without method restrictions

func (*Mux) Head

func (m *Mux) Head(pattern string, handler http.Handler)

Head registers a HEAD handler

func (*Mux) Home

func (m *Mux) Home(handler http.Handler)

Home registers a handler for the root path

func (*Mux) ListRoutes

func (m *Mux) ListRoutes() []ListInfo

ListRoutes returns a list of all registered routes

func (*Mux) NotFound

func (m *Mux) NotFound(handler http.Handler)

NotFound registers a handler for when no routes match

func (*Mux) Options

func (m *Mux) Options(pattern string, handler http.Handler)

Options registers an OPTIONS handler

func (*Mux) Patch

func (m *Mux) Patch(pattern string, handler http.Handler)

Patch registers a PATCH handler

func (*Mux) Post

func (m *Mux) Post(pattern string, handler http.Handler)

Post registers a POST handler

func (*Mux) PrefixGroup

func (m *Mux) PrefixGroup(prefix string, group GroupFunc) *Group

PrefixGroup creates a new route group with the given prefix and applies the given group configuration function.

func (*Mux) Put

func (m *Mux) Put(pattern string, handler http.Handler)

Put registers a PUT handler

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

func (m *Mux) ServeDirectoryWithPrefix(pattern string, fsPrefix string, fs http.FileSystem) error

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

func (m *Mux) ServeFiles(fs http.FileSystem, urlPrefix string, paths ...any) error

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",
},

)

func (*Mux) Use

func (m *Mux) Use(middleware ...Middleware)

Use adds middleware to the Mux

type Route

type Route struct {
	Pattern string
	Methods map[string]struct{}
}

Route stores information about registered routes

Directories

Path Synopsis
Package middleware provides HTTP middleware components for the route package.
Package middleware provides HTTP middleware components for the route package.

Jump to

Keyboard shortcuts

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