Documentation ¶
Index ¶
- Constants
- Variables
- func PutFormReader(fr *FormReader)
- func PutRequest(r *Request)
- func StatusBytes(code int) []byte
- func StatusText(code int) string
- type Compressor
- type Cookie
- type FormReader
- func (r *FormReader) NextField() bool
- func (r *FormReader) Read(b []byte) (n int, _ error)
- func (r *FormReader) ReadBytes() ([]byte, error)
- func (r *FormReader) ReadString() string
- func (r *FormReader) Remaining() int64
- func (r *FormReader) Reset(req *http.Request)
- func (r *FormReader) Skip() (n int64)
- type Handler
- type Header
- type Middleware
- type MiddlewareFunc
- type NewRoutesCallback
- type Request
- func (r *Request) FormFile(key string) *multipart.FileHeader
- func (r *Request) FormFileData(key string) ([]byte, *multipart.FileHeader)
- func (r *Request) FormFiles(key string) []*multipart.FileHeader
- func (r *Request) FormReader() *FormReader
- func (r *Request) ParseJSON(dst interface{}) error
- func (r *Request) RealIP() string
- func (r *Request) RouteInt(name string) int64
- func (r *Request) RouteStr(name string) string
- func (r *Request) URI() string
- func (r *Request) UserAgent() *user_agent.UserAgent
- type Response
- func (r *Response) Bytes(status int, b []byte) (err error)
- func (r *Response) Flush()
- func (r *Response) HTMLBytes(code int, html []byte) error
- func (r *Response) HTMLString(code int, html string) error
- func (r *Response) HTMLTemplate(code int, tmpl *htemplate.Template, data interface{}) error
- func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (r *Response) Int(status int, v int) error
- func (r *Response) Int64(status int, v int64) error
- func (r *Response) JSON(status int, src interface{}) error
- func (r *Response) Redirect(toURL string) error
- func (r *Response) RedirectPermanent(toURL string) error
- func (r *Response) ResponseCode() int
- func (r *Response) ResponseSize() int
- func (r *Response) SetCookie(cookie *Cookie)
- func (r *Response) Status(code int) error
- func (r *Response) StatusBadRequest() error
- func (r *Response) StatusConflict() error
- func (r *Response) StatusCreated() error
- func (r *Response) StatusForbidden() error
- func (r *Response) StatusNoContent() error
- func (r *Response) StatusNotFound() error
- func (r *Response) StatusOK() error
- func (r *Response) StatusTooLarge() error
- func (r *Response) StatusUnauthorized() error
- func (r *Response) String(status int, s string) error
- func (r *Response) TextTemplate(code int, tmpl *ttemplate.Template, data interface{}) error
- func (r *Response) Write(data []byte) (int, error)
- func (r *Response) WriteHeader(statusCode int)
- type ResponseWriter
- type RouteValue
- type Router
- func (r *Router) AddMiddleware(middlewares ...Middleware)
- func (r *Router) CONNECT(path string, h Handler, m ...Middleware)
- func (r *Router) DELETE(path string, h Handler, m ...Middleware)
- func (r *Router) File(routePath, filename string)
- func (r *Router) Fork(path string) *Router
- func (r *Router) GET(path string, h Handler, m ...Middleware)
- func (r *Router) Group(path string, subRoutes func())
- func (r *Router) HEAD(path string, h Handler, m ...Middleware)
- func (r *Router) Media(routePath, rootDir string)
- func (r *Router) NotFound(h Handler)
- func (r *Router) OPTIONS(path string, h Handler, m ...Middleware)
- func (r *Router) PATCH(path string, h Handler, m ...Middleware)
- func (r *Router) POST(path string, h Handler, m ...Middleware)
- func (r *Router) PUT(path string, h Handler, m ...Middleware)
- func (r *Router) ROUTE(method, path string, h Handler, m ...Middleware)
- func (r *Router) Redirect(path, method, toURL string, permanent bool)
- func (r *Router) ServeHTTP(originWriter http.ResponseWriter, originRequest *http.Request)
- func (r *Router) Static(routePath, rootDir string, compress *Compressor) error
- func (r *Router) StaticFS(routePath, rootDir string, fsDir *embed.FS, compress *Compressor) error
- func (r *Router) StaticFile(routePath, filename string, compress *Compressor) error
- func (r *Router) StaticFileFS(routePath, filename string, fs *embed.FS, compress *Compressor) error
- func (r *Router) TRACE(path string, h Handler, m ...Middleware)
- func (r *Router) WithLogNewRoutes(f NewRoutesCallback) *Router
- type SameSite
- type Server
- func (s *Server) Close() error
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) WithErrorLog(logger *log.Logger) *Server
- func (s *Server) WithHandler(h http.Handler) *Server
- func (s *Server) WithHost(listenHost string) *Server
- func (s *Server) WithPort(listenPort uint16) *Server
- func (s *Server) WithTimeouts(readTimeout, writeTimeout, idleTimeout time.Duration) *Server
Constants ¶
const ( SameSiteDefaultMode = http.SameSiteDefaultMode SameSiteLaxMode = http.SameSiteLaxMode SameSiteStrictMode = http.SameSiteStrictMode SameSiteNoneMode = http.SameSiteNoneMode )
const ( HeaderAccept = "Accept" HeaderAcceptEncoding = "Accept-Encoding" HeaderAllow = "Allow" HeaderAuthorization = "Authorization" HeaderContentDisposition = "Content-Disposition" HeaderContentEncoding = "Content-Encoding" HeaderContentLength = "Content-Length" HeaderContentType = "Content-Type" HeaderCookie = "Cookie" HeaderSetCookie = "Set-Cookie" HeaderIfModifiedSince = "If-Modified-Since" HeaderLastModified = "Last-Modified" HeaderLocation = "Location" HeaderUpgrade = "Upgrade" HeaderVary = "Vary" HeaderWWWAuthenticate = "WWW-Authenticate" HeaderXForwardedFor = "X-Forwarded-For" HeaderXForwardedProto = "X-Forwarded-Proto" HeaderXForwardedProtocol = "X-Forwarded-Protocol" HeaderXForwardedSsl = "X-Forwarded-Ssl" HeaderXUrlScheme = "X-Url-Scheme" HeaderXHTTPMethodOverride = "X-HTTP-Method-Override" HeaderXRealIP = "X-Real-IP" HeaderXRequestID = "X-Request-ID" HeaderXRequestedWith = "X-Requested-With" HeaderServer = "Server" HeaderOrigin = "Origin" HeaderAccessControlRequestMethod = "Access-Control-Request-Method" HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" HeaderAccessControlMaxAge = "Access-Control-Max-Age" HeaderStrictTransportSecurity = "Strict-Transport-Security" HeaderXContentTypeOptions = "X-Content-Type-Options" HeaderXXSSProtection = "X-XSS-Protection" HeaderXFrameOptions = "X-Frame-Options" HeaderContentSecurityPolicy = "Content-Security-Policy" HeaderXCSRFToken = "X-CSRF-Token" )
Header types
const ( MimeApplicationJSON = "application/json; " + charsetUTF8 MimeApplicationJavaScript = "application/javascript; " + charsetUTF8 MimeApplicationXML = "application/xml; " + charsetUTF8 MimeTextXML = "text/xml; " + charsetUTF8 MimeApplicationForm = "application/x-www-form-urlencoded" MimeApplicationProtobuf = "application/protobuf" MimeApplicationMsgpack = "application/msgpack" MimeTextHTML = "text/html; " + charsetUTF8 MimeTextPlain = "text/plain; " + charsetUTF8 MimeMultipartForm = "multipart/form-data" MimeOctetStream = "application/octet-stream" )
Mime types
const ( MethodGet = "GET" MethodHead = "HEAD" MethodPost = "POST" MethodPut = "PUT" MethodPatch = "PATCH" // RFC 5789 MethodDelete = "DELETE" MethodConnect = "CONNECT" MethodOptions = "OPTIONS" MethodTrace = "TRACE" )
common http methods
const ( StatusContinue = 100 // RFC 7231, 6.2.1 StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2 StatusProcessing = 102 // RFC 2518, 10.1 StatusEarlyHints = 103 // RFC 8297 StatusOK = 200 // RFC 7231, 6.3.1 StatusCreated = 201 // RFC 7231, 6.3.2 StatusAccepted = 202 // RFC 7231, 6.3.3 StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4 StatusNoContent = 204 // RFC 7231, 6.3.5 StatusResetContent = 205 // RFC 7231, 6.3.6 StatusPartialContent = 206 // RFC 7233, 4.1 StatusMultiStatus = 207 // RFC 4918, 11.1 StatusAlreadyReported = 208 // RFC 5842, 7.1 StatusIMUsed = 226 // RFC 3229, 10.4.1 StatusMultipleChoices = 300 // RFC 7231, 6.4.1 StatusMovedPermanently = 301 // RFC 7231, 6.4.2 StatusFound = 302 // RFC 7231, 6.4.3 StatusSeeOther = 303 // RFC 7231, 6.4.4 StatusNotModified = 304 // RFC 7232, 4.1 StatusUseProxy = 305 // RFC 7231, 6.4.5 StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7 StatusPermanentRedirect = 308 // RFC 7538, 3 StatusBadRequest = 400 // RFC 7231, 6.5.1 StatusPaymentRequired = 402 // RFC 7231, 6.5.2 StatusForbidden = 403 // RFC 7231, 6.5.3 StatusNotFound = 404 // RFC 7231, 6.5.4 StatusMethodNotAllowed = 405 // RFC 7231, 6.5.5 StatusNotAcceptable = 406 // RFC 7231, 6.5.6 StatusProxyAuthRequired = 407 // RFC 7235, 3.2 StatusRequestTimeout = 408 // RFC 7231, 6.5.7 StatusConflict = 409 // RFC 7231, 6.5.8 StatusGone = 410 // RFC 7231, 6.5.9 StatusLengthRequired = 411 // RFC 7231, 6.5.10 StatusPreconditionFailed = 412 // RFC 7232, 4.2 StatusRequestEntityTooLarge = 413 // RFC 7231, 6.5.11 StatusRequestURITooLong = 414 // RFC 7231, 6.5.12 StatusUnsupportedMediaType = 415 // RFC 7231, 6.5.13 StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4 StatusExpectationFailed = 417 // RFC 7231, 6.5.14 StatusTeapot = 418 // RFC 7168, 2.3.3 StatusMisdirectedRequest = 421 // RFC 7540, 9.1.2 StatusUnprocessableEntity = 422 // RFC 4918, 11.2 StatusLocked = 423 // RFC 4918, 11.3 StatusFailedDependency = 424 // RFC 4918, 11.4 StatusTooEarly = 425 // RFC 8470, 5.2. StatusUpgradeRequired = 426 // RFC 7231, 6.5.15 StatusPreconditionRequired = 428 // RFC 6585, 3 StatusTooManyRequests = 429 // RFC 6585, 4 StatusRequestHeaderFieldsTooLarge = 431 // RFC 6585, 5 StatusInternalServerError = 500 // RFC 7231, 6.6.1 StatusNotImplemented = 501 // RFC 7231, 6.6.2 StatusBadGateway = 502 // RFC 7231, 6.6.3 StatusGatewayTimeout = 504 // RFC 7231, 6.6.5 StatusHTTPVersionNotSupported = 505 // RFC 7231, 6.6.6 StatusVariantAlsoNegotiates = 506 // RFC 2295, 8.1 StatusInsufficientStorage = 507 // RFC 4918, 11.5 StatusLoopDetected = 508 // RFC 5842, 7.2 StatusNotExtended = 510 // RFC 2774, 7 StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6 )
Variables ¶
var ( // ErrMessageTooLarge is returned by ReadForm if the message form // data is too large to be processed. ErrMessageTooLarge = multipart.ErrMessageTooLarge // ErrNoCookie is returned by Request's Cookie method when a cookie is not found. ErrNoCookie = http.ErrNoCookie // ErrMissingFile is returned by FormFile when the provided file field name // is either not present in the request or not a file field. ErrMissingFile = http.ErrMissingFile )
var DefaultNotFound = func(r *Request) error {
return r.Response.StatusNotFound()
}
Functions ¶
func StatusBytes ¶
StatusBytes returns a text bytes for the HTTP status code. It returns the empty string if the code is unknown.
func StatusText ¶
StatusText returns a text for the HTTP status code. It returns the empty string if the code is unknown.
Types ¶
type Compressor ¶
type Compressor struct { MinSize int // default: 0 bytes MaxSize int // default: unlimit GzipDisable bool GzipMinFactor float64 // default: 0.75 from origin size GzipLevel int // range [0,9], default: 9 BrotliDisable bool BrotliMinFactor float64 // default: 0.75 from origin size BrotliLevel int // range [0,11], default: 6 // contains filtered or unexported fields }
Compressor config for static route
func (*Compressor) ApplyBrotli ¶
func (c *Compressor) ApplyBrotli(w io.Writer, data []byte) error
ApplyBrotli compress
type Cookie ¶
A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an HTTP response or the Cookie header of an HTTP request.
See https://tools.ietf.org/html/rfc6265 for details.
type FormReader ¶
type FormReader struct { IsFile bool Name string Filename string ContentType string // contains filtered or unexported fields }
FormReader for multipart fields
func GetFormReader ¶
func GetFormReader(req *http.Request) *FormReader
GetFormReader from pool and fill
func NewFormReader ¶
func NewFormReader(r *http.Request) *FormReader
NewFormReader for multipart fields
func (*FormReader) NextField ¶
func (r *FormReader) NextField() bool
NextField parsed field header if available
func (*FormReader) ReadBytes ¶
func (r *FormReader) ReadBytes() ([]byte, error)
ReadBytes of field value
type Handler ¶
func WrapHandler ¶
func WrapHandlerFunc ¶
func WrapHandlerFunc(f http.HandlerFunc) Handler
type Header ¶
A Header represents the key-value pairs in an HTTP header.
The keys should be in canonical form, as returned by CanonicalHeaderKey.
type Middleware ¶
type MiddlewareFunc ¶
func (MiddlewareFunc) Handler ¶
func (f MiddlewareFunc) Handler(next Handler) Handler
type NewRoutesCallback ¶
type NewRoutesCallback func(method, path string)
type Request ¶
type Request struct { *http.Request context.Context Response *Response Payload interface{} RouteVals []RouteValue RouteTail string RoutePath string // contains filtered or unexported fields }
Request net/http with context.Context, current route and ResponseWriter
func GetRequest ¶
func GetRequest(w http.ResponseWriter, r *http.Request) *Request
GetRequest from pool and fill
func (*Request) FormFile ¶
func (r *Request) FormFile(key string) *multipart.FileHeader
FormFile without opening or nil if not found
func (*Request) FormFileData ¶
func (r *Request) FormFileData(key string) ([]byte, *multipart.FileHeader)
FormFileData and header
func (*Request) FormFiles ¶
func (r *Request) FormFiles(key string) []*multipart.FileHeader
FormFiles without opening or nil if not found
func (*Request) UserAgent ¶
func (r *Request) UserAgent() *user_agent.UserAgent
UserAgent parse via github.com/mssola/user_agent
type Response ¶
type Response struct { http.ResponseWriter // contains filtered or unexported fields }
func (*Response) Flush ¶ added in v1.0.7
func (r *Response) Flush()
Flush implements the http.Flusher interface to allow an HTTP handler to flush buffered data to the client
func (*Response) HTMLTemplate ¶
func (*Response) Hijack ¶ added in v1.0.7
Hijack implements the http.Hijacker interface to allow an HTTP handler to take over the connection
func (*Response) RedirectPermanent ¶
func (*Response) ResponseCode ¶
func (*Response) ResponseSize ¶
func (*Response) StatusBadRequest ¶ added in v1.0.2
func (*Response) StatusConflict ¶ added in v1.0.2
func (*Response) StatusCreated ¶ added in v1.0.2
func (*Response) StatusForbidden ¶ added in v1.0.2
func (*Response) StatusNoContent ¶ added in v1.0.2
func (*Response) StatusNotFound ¶ added in v1.0.2
func (*Response) StatusTooLarge ¶ added in v1.0.2
func (*Response) StatusUnauthorized ¶ added in v1.0.2
func (*Response) TextTemplate ¶
func (*Response) WriteHeader ¶
type ResponseWriter ¶
type ResponseWriter = http.ResponseWriter
A ResponseWriter interface is used by an HTTP handler to construct an HTTP response.
A ResponseWriter may not be used after the Handler.ServeHTTP method has returned.
type RouteValue ¶
RouteValue of web.Router handler
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router with handlers and middlewares
func (*Router) AddMiddleware ¶
func (r *Router) AddMiddleware(middlewares ...Middleware)
AddMiddleware for the next handlers
func (*Router) CONNECT ¶
func (r *Router) CONNECT(path string, h Handler, m ...Middleware)
CONNECT is alias ROUTE with method Connect
func (*Router) DELETE ¶
func (r *Router) DELETE(path string, h Handler, m ...Middleware)
DELETE is alias ROUTE with method Delete
func (*Router) GET ¶
func (r *Router) GET(path string, h Handler, m ...Middleware)
GET is alias ROUTE with method Get
func (*Router) HEAD ¶
func (r *Router) HEAD(path string, h Handler, m ...Middleware)
HEAD is alias ROUTE with method Head
func (*Router) OPTIONS ¶
func (r *Router) OPTIONS(path string, h Handler, m ...Middleware)
OPTIONS is alias ROUTE with method Options
func (*Router) PATCH ¶
func (r *Router) PATCH(path string, h Handler, m ...Middleware)
PATCH is alias ROUTE with method Patch
func (*Router) POST ¶
func (r *Router) POST(path string, h Handler, m ...Middleware)
POST is alias ROUTE with method Post
func (*Router) PUT ¶
func (r *Router) PUT(path string, h Handler, m ...Middleware)
PUT is alias ROUTE with method Put
func (*Router) ROUTE ¶
func (r *Router) ROUTE(method, path string, h Handler, m ...Middleware)
ROUTE is add handler to router with http method
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(originWriter http.ResponseWriter, originRequest *http.Request)
func (*Router) Static ¶
func (r *Router) Static(routePath, rootDir string, compress *Compressor) error
Static cached fileserver, compress if set
func (*Router) StaticFile ¶
func (r *Router) StaticFile(routePath, filename string, compress *Compressor) error
StaticFile serve, compress if set
func (*Router) StaticFileFS ¶
StaticFileFS serve, compress if set
func (*Router) TRACE ¶
func (r *Router) TRACE(path string, h Handler, m ...Middleware)
TRACE is alias ROUTE with method Trace
func (*Router) WithLogNewRoutes ¶
func (r *Router) WithLogNewRoutes(f NewRoutesCallback) *Router
type SameSite ¶
SameSite allows a server to define a cookie attribute making it impossible for the browser to send this cookie along with cross-site requests. The main goal is to mitigate the risk of cross-origin information leakage, and provide some protection against cross-site request forgery attacks.
See https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00 for details.