Documentation
¶
Index ¶
- Constants
- Variables
- func RandomBase64Bytes(n int) (string, error)
- func RandomString(n int, charset string) (string, error)
- func StdRandomString(n int) (string, error)
- type AbsPathRouter
- type Chain
- type ChainFunc
- type Chainer
- type CharSet
- type ContentOption
- type Context
- func (c *Context) AbsPath() string
- func (c *Context) BadRequest(errs ...error)
- func (c *Context) Close()
- func (c *Context) ContentType() string
- func (c *Context) Cookie(name string) (*Cookie, error)
- func (c *Context) Cookies() []*Cookie
- func (c *Context) CookiesNamed(name string) []*Cookie
- func (c *Context) ErrorJSON(err error, detail ...string) error
- func (c *Context) Header(name string) string
- func (c *Context) Headers(name string) ([]string, bool)
- func (c *Context) InternalServerError(errs ...error)
- func (c *Context) Method() string
- func (c *Context) NotFound()
- func (c *Context) Path() string
- func (c *Context) PathAsInt() (int64, bool, error)
- func (c *Context) PathPrefix() string
- func (c *Context) Printf(format string, v ...any) (int, error)
- func (c *Context) Query() url.Values
- func (c *Context) Redirect(u string, status int)
- func (c *Context) RedirectFound(u string)
- func (c *Context) Referer() string
- func (c *Context) Render(tmpl, view string, val any)
- func (c *Context) Scan(v any) error
- func (c *Context) ScanFormURL(v any) error
- func (c *Context) ScanJSON(v any) error
- func (c *Context) ScanURL(v any) error
- func (c *Context) SendErrorJSON(e ErrorJSON) error
- func (c *Context) SendJSON(v any) error
- func (c *Context) SendStatusCode(code int)
- func (c *Context) ServeFile(name string)
- func (c *Context) SetContentType(typ string, opts ...ContentOption)
- func (c *Context) SetCookies(cookie *Cookie, rest ...*Cookie)
- func (c *Context) SetHeader(k, v string)
- func (c *Context) SetStatus(status int)
- func (c *Context) Unauthorized()
- type Cookie
- type DebugHandler
- type Decoder
- type ErrorJSON
- type File
- type FileSystem
- type FsOptions
- type Func
- type Handler
- type HttpHandler
- type HttpHandlerFunc
- type MethodRouter
- type PathRouter
- type Request
- type ResponseWriter
- type RootRouter
- type Server
- type Wrap
Constants ¶
const ( // Using the UTF-8 by default. ContentTypeUnknown = "application/octet-stream" ContentTypeBinary ContentTypePlain = "text/plain" ContentTypeCSS = "text/css" ContentTypeHTML = "text/html" ContentTypeJSON = "application/json" ContentTypeProblemJSON = "application/problem+json" ContentTypeEncodedURL = "application/x-www-form-urlencoded" )
const StdRandomStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Variables ¶
Functions ¶
func RandomBase64Bytes ¶ added in v0.15.0
Generates random sequence of bytes with length n encoded to the Base64.
func RandomString ¶ added in v0.15.0
Generates random string with length n and consisting of characters present in charset.
func StdRandomString ¶ added in v0.15.0
RandomString shortcut to use the StdRandomString as the second argument.
Types ¶
type AbsPathRouter ¶ added in v0.8.0
type AbsPathRouter struct {
// contains filtered or unexported fields
}
The type implements absolute paths routing for requests.
func AbsPath ¶ added in v0.8.0
func AbsPath() *AbsPathRouter
Returns new empty AbsPathRouter with def as the default fallback.
func (*AbsPathRouter) Case ¶ added in v0.8.0
func (router *AbsPathRouter) Case(pth string, handler Handler) *AbsPathRouter
Define new handler for the specified absolute path. Will panic on empty path or if path does not start with slash..
func (*AbsPathRouter) Default ¶ added in v0.8.0
func (router *AbsPathRouter) Default(handler Handler) *AbsPathRouter
func (*AbsPathRouter) Handle ¶ added in v0.8.0
func (router *AbsPathRouter) Handle(c *Context)
type ContentOption ¶
type ContentOption interface {
KeyValue() [2]string
}
type Context ¶
type Context struct {
// Implementing the context shit.
context.Context
// The http.Request, expand.
R *Request
// Relative URL.
RelUrl *url.URL
W ResponseWriter
// Custom data to expand the Context type..
Env any
App any
// contains filtered or unexported fields
}
The type contains all information regarding HTTP request and interaction with it.
func (*Context) BadRequest ¶ added in v0.7.0
Send 400 bad request back to the client with the specified error.
func (*Context) CookiesNamed ¶ added in v0.15.0
func (*Context) Headers ¶ added in v0.15.1
Get request headers by name, returns all including duplicates.
func (*Context) InternalServerError ¶
Send internal server error to the client.
func (*Context) RedirectFound ¶ added in v0.15.1
func (*Context) Render ¶ added in v0.12.0
Render the specified template with the specified view using the value.
func (*Context) Scan ¶
Scan the incoming value from body depending on the content type of the request into a structure or map[string] any. If you want to be sure about what kind of encoding is going to be applied use more specific methods (eg ScanJSON) or implement it yourself.
func (*Context) ScanFormURL ¶ added in v0.13.1
Scan the X-WWW-FORM-URLENCODED value into the v.
func (*Context) ScanJSON ¶ added in v0.7.0
Reads from the request body as if it was JSON and stores in reciever (v).
func (*Context) ScanURL ¶ added in v0.14.0
Scan the X-WWW-FORM-URLENCODED value into the v taking the query from the Request URL. Respects types and works like json.Unmarshal.
Know that the method is quite slow, since inside it creates maps so if you need optimization use the *Context.Query() to your liking.
func (*Context) SendErrorJSON ¶ added in v0.10.0
Send the structured error json with some default values if not specified.
func (*Context) SendJSON ¶ added in v0.7.0
Sends a JSON marshalized value back to the client setting the corresponding content type.
func (*Context) SendStatusCode ¶ added in v0.7.0
Sends the specified status code back to the client.
func (*Context) SetContentType ¶
func (c *Context) SetContentType(typ string, opts ...ContentOption)
Set the reply content type.
func (*Context) SetCookies ¶ added in v0.15.0
Send the specified Cookies. Must be called before any *Context.SetHeader calls, because once the headers are sent you cannot add more cookies.
func (*Context) Unauthorized ¶ added in v0.15.1
func (c *Context) Unauthorized()
type DebugHandler ¶ added in v0.10.3
type DebugHandler struct{}
The type implements simple debuging handler.
func (*DebugHandler) Handle ¶ added in v0.10.3
func (handler *DebugHandler) Handle(c *Context)
Implementing Handler.
type ErrorJSON ¶ added in v0.10.0
type ErrorJSON struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
Instance string `json:"instance"`
}
{
"type": "https://example.com",
"title": "You do not have enough credit.",
"status": 403,
"detail": "Your current balance is 30, but that costs 50.",
"instance": "/account/12345/msgs/abc"
}
type File ¶ added in v0.11.0
dotFileHidingFile is the http.File use in dotFileHidingFileSystem. It is used to wrap the Readdir method of http.File so that we can remove files and directories that start with a period from its output.
type FileSystem ¶ added in v0.11.0
type FileSystem struct {
http.FileSystem
// The options will be passed to every file.
Options FsOptions
// contains filtered or unexported fields
}
HTTP file systems that allows easier customization than the one from the standard library
func StaticDir ¶
func StaticDir(pth string) *FileSystem
func (*FileSystem) Handle ¶ added in v0.11.0
func (fsys *FileSystem) Handle(c *Context)
func (FileSystem) Open ¶ added in v0.11.0
func (fsys FileSystem) Open(name string) (http.File, error)
Open is a wrapper around the Open method of the embedded FileSystem that serves a 403 permission error when name has a file or directory with whose name starts with a period in its path.
func (*FileSystem) ShowDot ¶ added in v0.11.0
func (fsys *FileSystem) ShowDot(show bool) *FileSystem
type FsOptions ¶ added in v0.11.0
type FsOptions struct {
ShowDot bool
}
The type provides options on static files access and listing.
type Func ¶
type Func func(*Context)
The type is used for custom-one-place implementation of handling.
type Handler ¶
type Handler interface {
Handle(c *Context)
}
func StaticFile ¶
type HttpHandler ¶
type HttpHandlerFunc ¶
type HttpHandlerFunc = http.HandlerFunc
type MethodRouter ¶
type MethodRouter struct {
// contains filtered or unexported fields
}
The type implements method routing.
func (*MethodRouter) Case ¶
func (mr *MethodRouter) Case( method string, handler Handler, ) *MethodRouter
Define new handler for the specified method.
func (*MethodRouter) Default ¶ added in v0.8.0
func (mr *MethodRouter) Default( handler Handler, ) *MethodRouter
type PathRouter ¶
type PathRouter struct {
// contains filtered or unexported fields
}
The type implements path routing for requests.
func Path ¶
func Path() *PathRouter
Returns new empty PathRouter with the def specified as the default fallback.
func (*PathRouter) Case ¶
func (router *PathRouter) Case(pth string, handler Handler) *PathRouter
Define new handler for the specified path. The defined path must not contain slashes and will panic otherwise.
func (*PathRouter) Default ¶ added in v0.8.0
func (router *PathRouter) Default(handler Handler) *PathRouter
type ResponseWriter ¶
type ResponseWriter = http.ResponseWriter
type RootRouter ¶
type RootRouter struct {
// For global data.
App any
// contains filtered or unexported fields
}
The type implements the entry point for all the request for the server.
func Root ¶
func Root(app any, handler Handler) *RootRouter
Return the new RootRouter with the specified handler. If you want to get most of the high-level you will NEED this part of the library. Use it anywhere where you create the new service, eg in every server because it does much of initialization and sanitazation in the ServeHTTP.
func (*RootRouter) ServeHTTP ¶
func (router *RootRouter) ServeHTTP(w ResponseWriter, r *Request)
Implementing the http.Handler .
func (*RootRouter) Templates ¶ added in v0.12.0
func (router *RootRouter) Templates(tmpls tplx.Templates) *RootRouter