Documentation ¶
Index ¶
- Constants
- Variables
- func PutFormReader(fr *FormReader)
- func PutRequest(r *Request)
- func StartServer(conf ServerConfig, log zerolog.Logger, h http.Handler) (io.Closer, error)
- func StatusBytes(code int) []byte
- func StatusText(code int) string
- type Compressor
- 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 Middleware
- type Middlewarer
- 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) Logger() zerolog.Logger
- 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
- type Response
- func (r *Response) BadRequestJSON(src interface{}) error
- func (r *Response) BadRequestString(s string) error
- func (r *Response) Bytes(status int, b []byte) (err error)
- func (r *Response) CreatedInt(v int64) error
- func (r *Response) CreatedJSON(src interface{}) error
- func (r *Response) CreatedString(s string) error
- 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) Int(status int, v int64) error
- func (r *Response) JSON(status int, src interface{}) error
- func (r *Response) OKBytes(b []byte) error
- func (r *Response) OKInt(v int64) error
- func (r *Response) OKJSON(src interface{}) error
- func (r *Response) OKString(s string) error
- func (r *Response) Redirect(toURL string) error
- func (r *Response) RedirectPermanent(toURL string) error
- func (r *Response) SetCookie(cookie *http.Cookie)
- func (r *Response) Status(statusCode int) error
- func (r *Response) StatusBadRequest() error
- func (r *Response) StatusCode() int
- 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 RouteValue
- type Router
- func (r *Router) AddMiddleware(middlewares ...interface{})
- func (r *Router) Connect(path string, view interface{})
- func (r *Router) Delete(path string, view interface{})
- func (r *Router) File(routePath, filename string)
- func (r *Router) Fork(path string) *Router
- func (r *Router) Get(path string, view interface{})
- func (r *Router) Group(path string, subRoutes func())
- func (r *Router) Head(path string, view interface{})
- func (r *Router) Media(routePath, rootDir string)
- func (r *Router) NotFound(view interface{})
- func (r *Router) Options(path string, view interface{})
- func (r *Router) Patch(path string, view interface{})
- func (r *Router) Post(path string, view interface{})
- func (r *Router) Put(path string, view interface{})
- func (r *Router) Redirect(path, method, toURL string, permanent bool)
- func (r *Router) Route(method, path string, view interface{})
- 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, view interface{})
- type ServerConfig
- type View
- type Viewer
Constants ¶
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 )
var DefaultNotFound = func(r *Request) error {
return r.Response.StatusNotFound()
}
DefaultNotFound view for web.Router
var DefaultViewConverter = func(view interface{}) View { switch view := view.(type) { case func(*Request) error: return view case View: return view case Viewer: return view.View() case http.HandlerFunc: return func(r *Request) error { view(r.Response, r.Request) return nil } case http.Handler: return func(r *Request) error { view.ServeHTTP(r.Response, r.Request) return nil } case func(http.ResponseWriter, *http.Request): return func(r *Request) error { view(r.Response, r.Request) return nil } default: panic("wrong route view type") } }
DefaultViewConverter for web.Router
Functions ¶
func StartServer ¶
StartServer http with config and logging
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 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 Middlewarer ¶
Middlewarer is Middleware implementators
type Request ¶
type Request struct { *http.Request context.Context Response *Response Log zerolog.Logger 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 ¶
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
type Response ¶
type Response struct { http.ResponseWriter // contains filtered or unexported fields }
Response writer
func (*Response) BadRequestJSON ¶
BadRequestJSON is alias JSON with status BadRequest
func (*Response) BadRequestString ¶
BadRequestString is alias String with status BadRequest
func (*Response) CreatedInt ¶
CreatedInt is alias Int with status Created
func (*Response) CreatedJSON ¶
CreatedJSON is alias JSON with status Created
func (*Response) CreatedString ¶
CreatedString is alias String with status Created
func (*Response) HTMLString ¶
HTMLString response
func (*Response) HTMLTemplate ¶
HTMLTemplate response
func (*Response) RedirectPermanent ¶
RedirectPermanent status code
func (*Response) StatusBadRequest ¶
StatusBadRequest response
func (*Response) StatusForbidden ¶
StatusForbidden response
func (*Response) StatusNoContent ¶
StatusNoContent response
func (*Response) StatusUnauthorized ¶
StatusUnauthorized response
func (*Response) TextTemplate ¶
TextTemplate response
func (*Response) WriteHeader ¶
WriteHeader status code
type RouteValue ¶
RouteValue of web.Router handler
type Router ¶
type Router struct { ViewConverter func(interface{}) View OnAddRoute func(method, path string) // contains filtered or unexported fields }
Router http with web.View handlers and middlewares
func (*Router) AddMiddleware ¶
func (r *Router) AddMiddleware(middlewares ...interface{})
AddMiddleware to this router root
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
type ServerConfig ¶
type ServerConfig struct { // Port for tcp socket Port uint16 `desc:"listen port for tcp socket"` Host string `desc:"listen address" default:"0.0.0.0"` TLS bool `desc:"TLS enable. Using selfsigned certificate by empty cert/key"` TLSCertPath string TLSKeyPath string TLSSelfMaxAge time.Duration `default:"365d"` ReadTimeout time.Duration `default:"20s"` WriteTimeout time.Duration `default:"30s"` IdleTimeout time.Duration `default:"5s"` }
ServerConfig http with options
func (*ServerConfig) ValidAndRepair ¶
func (c *ServerConfig) ValidAndRepair() error