Documentation ¶
Index ¶
- Constants
- Variables
- func Error(code int, err string) statusError
- func ListenAndServe(addr string, handler Handler) error
- func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error
- func MustMarshalJSON(v any) []byte
- func NewMD5SaltHashFunc(salt string) func(string) string
- func ParseRequestBody[T any](r *Request, parserFunc func([]byte, any) error) (*T, error)
- func QueryEscape(s string) string
- func RealClientAddr(r *Request) string
- func Redirect(w Writer, r *Request, url string, code int)
- func StringContains(s, substr string) bool
- func WriteError(w Writer, r *Request, err error)
- func WriteErrorStatusJSON(w Writer, err error)
- type AllOfRouters
- type AnyOfRouters
- type Cookie
- type Dir
- type FileSystem
- type Fork
- func (f *Fork) Add(paths ...Path) ForkBuilder
- func (f *Fork) Branch(path string) ForkBuilder
- func (f *Fork) Handle(path string, h Handler) ForkBuilder
- func (f *Fork) HandleFunc(path string, hf func(Writer, *Request)) ForkBuilder
- func (f *Fork) Path(r Router, h Handler) ForkBuilder
- func (f *Fork) PathFunc(r Router, hf func(Writer, *Request)) ForkBuilder
- func (f *Fork) PathFuncs(rf func(*Request) bool, hf func(Writer, *Request)) ForkBuilder
- func (f *Fork) ServeHTTP(w Writer, r *Request)
- func (f *Fork) With(routers []RouterMiddleware, middlewares []Middleware) ForkBuilder
- func (f *Fork) WithMiddlewares(middlewares ...Middleware) ForkBuilder
- func (f *Fork) WithRouters(routers ...RouterMiddleware) ForkBuilder
- type ForkBuilder
- type ForkFunc
- func (f ForkFunc) Add(paths ...Path) ForkBuilder
- func (f ForkFunc) Branch(path string) ForkBuilder
- func (f ForkFunc) Handle(path string, h Handler) ForkBuilder
- func (f ForkFunc) HandleFunc(path string, hf func(Writer, *Request)) ForkBuilder
- func (f ForkFunc) Path(r Router, h Handler) ForkBuilder
- func (f ForkFunc) PathFunc(r Router, hf func(Writer, *Request)) ForkBuilder
- func (f ForkFunc) PathFuncs(rf func(*Request) bool, hf func(Writer, *Request)) ForkBuilder
- func (f ForkFunc) With(routerMiddlewares []RouterMiddleware, middlewares []Middleware) ForkBuilder
- func (f ForkFunc) WithMiddlewares(middlewares ...Middleware) ForkBuilder
- func (f ForkFunc) WithRouters(routers ...RouterMiddleware) ForkBuilder
- type FuncRouter
- type Handler
- func AddPrefix(prefix string, h Handler) Handler
- func BufferedHandler(bytes []byte) Handler
- func FileServer(root FileSystem) Handler
- func IndexHandler(fs FileSystem) Handler
- func StripPrefix(prefix string, h Handler) Handler
- func Use(h Handler, m ...Middleware) Handler
- func Using(ms []Middleware, h Handler) Handler
- type HandlerFunc
- type HostRouter
- type JSON
- type Middleware
- type Path
- type PathMatchRouter
- type PathPrefixRouter
- type Regexp
- type Request
- type ResponseWriter
- type Router
- type RouterMiddleware
- type StatusError
- type TLSRouter
- type URL
- type UserAgentRouter
- type Writer
Constants ¶
const ( MethodRouterCONNECT methodRouter = "CONNECT" MethodRouterDELETE methodRouter = "DELETE" MethodRouterGET methodRouter = "GET" MethodRouterHEAD methodRouter = "HEAD" MethodRouterOPTIONS methodRouter = "OPTIONS" MethodRouterPOST methodRouter = "POST" MethodRouterPUT methodRouter = "PUT" MethodRouterTRACE methodRouter = "TRACE" )
const ( // StatusOK = 200 StatusOK = http.StatusOK // StatusPartialContent = 206 StatusPartialContent = http.StatusPartialContent // StatusMovedPermanently = 301 StatusMovedPermanently = http.StatusMovedPermanently // StatusNotModified = 304 StatusNotModified = http.StatusNotModified // StatusBadRequest = 400 StatusBadRequest = http.StatusBadRequest StatusUnauthorized = http.StatusUnauthorized // StatusPaymentRequired = 402 StatusPaymentRequired = http.StatusPaymentRequired // StatusForbidden = 403 StatusForbidden = http.StatusForbidden // StatusNotFound = 404 StatusNotFound = http.StatusNotFound // StatusMethodNotAllowed = 405 StatusMethodNotAllowed = http.StatusMethodNotAllowed // StatusConflict = 409 StatusConflict = http.StatusConflict // StatusPreconditionFailed = 412 StatusPreconditionFailed = http.StatusPreconditionFailed // StatusRequestedRangeNotSatisfiable = 416 StatusRequestedRangeNotSatisfiable = http.StatusRequestedRangeNotSatisfiable // StatusTooManyRequests = 429 StatusTooManyRequests = http.StatusTooManyRequests // StatusInternalServerError = 500 StatusInternalServerError = http.StatusInternalServerError )
Variables ¶
var ( MethodMiddlewareCONNECT = middlewareMethod("CONNECT") MethodMiddlewareDELETE = middlewareMethod("DELETE") MethodMiddlewareGET = middlewareMethod("GET") MethodMiddlewareHEAD = middlewareMethod("HEAD") MethodMiddlewareOPTIONS = middlewareMethod("OPTIONS") MethodMiddlewarePOST = middlewareMethod("POST") MethodMiddlewarePUT = middlewareMethod("PUT") MethodMiddlewareTRACE = middlewareMethod("TRACE") )
var ( RouterMiddlewareCONNECT = routerRouterMiddleware(MethodRouterCONNECT) RouterMiddlewareDELETE = routerRouterMiddleware(MethodRouterDELETE) RouterMiddlewareGET = routerRouterMiddleware(MethodRouterGET) RouterMiddlewareHEAD = routerRouterMiddleware(MethodRouterHEAD) RouterMiddlewareOPTIONS = routerRouterMiddleware(MethodRouterOPTIONS) RouterMiddlewarePOST = routerRouterMiddleware(MethodRouterPOST) RouterMiddlewarePUT = routerRouterMiddleware(MethodRouterPUT) RouterMiddlewareTRACE = routerRouterMiddleware(MethodRouterTRACE) )
var ErrRegexp = Error(StatusInternalServerError, "url path regexp mismatch")
var MarshalJSON = json.Marshal
var UnmarshalJSON = json.Unmarshal
Functions ¶
func ListenAndServe ¶
ListenAndServe calls http.ListenAndServe
func ListenAndServeTLS ¶
ListenAndServe calls http.ListenAndServeTLS
func MustMarshalJSON ¶
func NewMD5SaltHashFunc ¶
NewMD5SaltHashFunc creates a Func type which salts input to crypto/md5.Sum
func ParseRequestBody ¶
func RealClientAddr ¶
RealClientAddr returns the Client IP, using "X-Real-Ip", and then "X-Forwarded-For", before defaulting to RemoteAddr
func StringContains ¶
func WriteError ¶
func WriteErrorStatusJSON ¶
Types ¶
type AllOfRouters ¶
type AllOfRouters []Router
AllOfRouters is a Router group that returns true when all Routers in the group return true
func (AllOfRouters) RouteHTTP ¶
func (and AllOfRouters) RouteHTTP(r *Request) bool
type AnyOfRouters ¶
type AnyOfRouters []Router
AnyOfRouters is a Router group that returns true when any Routers in the group returns true
func (AnyOfRouters) RouteHTTP ¶
func (or AnyOfRouters) RouteHTTP(r *Request) bool
type Fork ¶
type Fork []Path
Fork is a Pather made of []Pather
func (*Fork) Branch ¶
func (f *Fork) Branch(path string) ForkBuilder
func (*Fork) Handle ¶
func (f *Fork) Handle(path string, h Handler) ForkBuilder
Handle is short for Path(RoutePathExact(path), h)
func (*Fork) HandleFunc ¶
func (f *Fork) HandleFunc(path string, hf func(Writer, *Request)) ForkBuilder
HandleFunc is short for Handle(path, HandlerFunc(hf))
func (*Fork) Path ¶
func (f *Fork) Path(r Router, h Handler) ForkBuilder
Path is short for Add(Path{r, h})
func (*Fork) PathFunc ¶
func (f *Fork) PathFunc(r Router, hf func(Writer, *Request)) ForkBuilder
PathFunc is short for Path(r, HandlerFunc(hf))
func (*Fork) With ¶
func (f *Fork) With(routers []RouterMiddleware, middlewares []Middleware) ForkBuilder
func (*Fork) WithMiddlewares ¶
func (f *Fork) WithMiddlewares(middlewares ...Middleware) ForkBuilder
func (*Fork) WithRouters ¶
func (f *Fork) WithRouters(routers ...RouterMiddleware) ForkBuilder
type ForkBuilder ¶
type ForkBuilder interface { Add(...Path) ForkBuilder Branch(path string) ForkBuilder Path(Router, Handler) ForkBuilder PathFunc(Router, func(Writer, *Request)) ForkBuilder PathFuncs(func(*Request) bool, func(Writer, *Request)) ForkBuilder Handle(path string, h Handler) ForkBuilder HandleFunc(path string, hf func(Writer, *Request)) ForkBuilder With([]RouterMiddleware, []Middleware) ForkBuilder WithMiddlewares(...Middleware) ForkBuilder WithRouters(...RouterMiddleware) ForkBuilder }
type ForkFunc ¶
type ForkFunc func(...Path)
func (ForkFunc) Add ¶
func (f ForkFunc) Add(paths ...Path) ForkBuilder
func (ForkFunc) Branch ¶
func (f ForkFunc) Branch(path string) ForkBuilder
func (ForkFunc) HandleFunc ¶
func (f ForkFunc) HandleFunc(path string, hf func(Writer, *Request)) ForkBuilder
func (ForkFunc) PathFunc ¶
func (f ForkFunc) PathFunc(r Router, hf func(Writer, *Request)) ForkBuilder
func (ForkFunc) With ¶
func (f ForkFunc) With(routerMiddlewares []RouterMiddleware, middlewares []Middleware) ForkBuilder
func (ForkFunc) WithMiddlewares ¶
func (f ForkFunc) WithMiddlewares(middlewares ...Middleware) ForkBuilder
func (ForkFunc) WithRouters ¶
func (f ForkFunc) WithRouters(routers ...RouterMiddleware) ForkBuilder
type FuncRouter ¶
FuncRouter is a func type Router
func (FuncRouter) RouteHTTP ¶
func (f FuncRouter) RouteHTTP(r *Request) bool
RouteHTTP implements Router by calling f
type Handler ¶
Handler = http.Handler
func BufferedHandler ¶
BufferedHandler returns a Handler that always writes the closured bytes
func IndexHandler ¶
func IndexHandler(fs FileSystem) Handler
IndexHandler returns a Handler that maps every request to /index.html for injected FileSystem, without issuing a redirect
func StripPrefix ¶
func Use ¶
func Use(h Handler, m ...Middleware) Handler
func Using ¶
func Using(ms []Middleware, h Handler) Handler
type HostRouter ¶
type HostRouter string
HostRouter is a string type for matching Request.Host
func (HostRouter) RouteHTTP ¶
func (host HostRouter) RouteHTTP(r *Request) bool
type Middleware ¶
Middleware is a consumer type that manipulates Handlers
func AddPrefixMiddleware ¶
func AddPrefixMiddleware(prefix string) Middleware
func RegexpPathContextMiddleware ¶
func RegexpPathContextMiddleware(regexp *Regexp) Middleware
func StripPrefixMiddleware ¶
func StripPrefixMiddleware(prefix string) Middleware
type Path ¶
Path is a struct with Router and Handler pointers
func NewPathFunc ¶
NewPathFunc is short for NewPath(r, HandlerFunc(hf))
func SinglePagePath ¶
func SinglePagePath(fs FileSystem, cache bool) Path
type PathMatchRouter ¶
type PathMatchRouter string
PathMatchRouter is a string type that matches a path literal
func (PathMatchRouter) RouteHTTP ¶
func (path PathMatchRouter) RouteHTTP(r *Request) bool
RouteHTTP implements Router by literally matching the request path
type PathPrefixRouter ¶
type PathPrefixRouter string
PathPrefixRouter is a string type that matches a path prefix
func (PathPrefixRouter) RouteHTTP ¶
func (prefix PathPrefixRouter) RouteHTTP(r *Request) bool
RouteHTTP implements Router by matching the path prefix
type Router ¶
Router is an routing interface
func RegexpPathRouter ¶
func SinglePageRouter ¶
func SinglePageRouter() Router
SinglePageRouter returns a Router that checks for Single Page App response
Request.Method is GET Request.URL.Path does not have a period (.) Request.Header["Accept"] contains "text/html"
func UseRouter ¶
func UseRouter(r Router, rm ...RouterMiddleware) Router
func UsingRouter ¶
func UsingRouter(mr []RouterMiddleware, r Router) Router
UsingRouter applies RouterMiddlewares to a Router
type RouterMiddleware ¶
func PathPrefixRouterMiddleware ¶
func PathPrefixRouterMiddleware(str string) RouterMiddleware
func PathRouterMiddleware ¶
func PathRouterMiddleware(path string) RouterMiddleware
func RegexpPathRouterMiddleware ¶
func RegexpPathRouterMiddleware(regexp *Regexp) RouterMiddleware
type StatusError ¶
StatusError is an error with a status code
type UserAgentRouter ¶
type UserAgentRouter string
UserAgentRouter is a string type for matching Request.Header["User-Agent"]
func (UserAgentRouter) RouteHTTP ¶
func (ua UserAgentRouter) RouteHTTP(r *Request) bool
RouteHTTP matches the first chars of Request.Header["User-Agent"]