Documentation ¶
Index ¶
- Constants
- Variables
- func GetDataFromRequest(c *Context, key string, dataType int) (string, bool)
- func TLSConfigAutocert() (c *tls.Config, err error)
- func TLSConfigParse(certFile, keyFile interface{}) (TLSConfig *tls.Config, err error)
- func TLSConfigTransform(key, pem string) (c *tls.Config, err error)
- type Binder
- type Context
- func (c *Context) Aborted() bool
- func (c *Context) Attachment(file, name string) error
- func (c *Context) Bind(i interface{}) error
- func (c *Context) Bytes(contentType string, b []byte) (err error)
- func (c *Context) End() error
- func (c *Context) Error(err error)
- func (c *Context) File(file string) (err error)
- func (c *Context) Flush()
- func (c *Context) Get(key string, dts ...int) string
- func (c *Context) HTML(html string) (err error)
- func (c *Context) Header() http.Header
- func (c *Context) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (c *Context) Inline(file, name string) error
- func (c *Context) IsWebSocket() bool
- func (c *Context) JSON(i interface{}) error
- func (c *Context) JSONP(callback string, i interface{}) error
- func (c *Context) Protocol() string
- func (c *Context) Redirect(url string) error
- func (c *Context) RemoteAddr() string
- func (c *Context) Render(name string, data interface{}) (err error)
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) Stream(contentType string, r io.Reader) (err error)
- func (c *Context) String(s string) (err error)
- func (c *Context) Write(b []byte) (n int, err error)
- func (c *Context) WriteHeader(code int)
- func (c *Context) XML(i interface{}, indent string) (err error)
- type DefaultBinder
- type Group
- type GroupCaller
- type GroupMode
- type GroupNode
- type HTTPError
- type HTTPErrorHandler
- type HandlerFunc
- type MiddlewareFunc
- type Node
- type Options
- type Proxy
- type RESTful
- type Renderer
- type Router
- type Server
- func (s *Server) AcquireContext(w http.ResponseWriter, r *http.Request) *Context
- func (s *Server) Any(path string, h HandlerFunc)
- func (s *Server) Close() error
- func (s *Server) DefaultHTTPErrorHandler(c *Context, err error)
- func (s *Server) GET(path string, h HandlerFunc)
- func (s *Server) Group(prefix string, i interface{}, method ...string) *Group
- func (s *Server) POST(path string, h HandlerFunc)
- func (s *Server) Proxy(prefix, address string, method ...string) *Proxy
- func (s *Server) RESTful(prefix string, handle iRESTful, method ...string) *RESTful
- func (s *Server) Register(path string, handler HandlerFunc, method ...string)
- func (s *Server) ReleaseContext(c *Context)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) Shutdown(ctx ctx.Context) error
- func (s *Server) Start() (err error)
- func (s *Server) Static(prefix, root string, method ...string) *Static
- func (s *Server) Use(middleware ...MiddlewareFunc)
- type Session
- type Static
Constants ¶
const ( RouteGroupPath = "_RouteGroupPath" RouteGroupName = "_RouteGroupName" )
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-Protocol" HeaderXHTTPMethodOverride = "X-HTTP-value-Override" HeaderXRealIP = "X-Real-Addr" HeaderXRequestID = "X-Request-ID" HeaderXRequestedWith = "X-Requested-With" HeaderServer = "Server" HeaderOrigin = "origin" // Access control HeaderAccessControlRequestMethod = "Access-Control-Request-value" 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" // Security HeaderStrictTransportSecurity = "Strict-Transport-Security" HeaderXContentTypeOptions = "X-Content-Type-options" HeaderXXSSProtection = "X-XSS-Protection" HeaderXFrameOptions = "X-Frame-options" HeaderContentSecurityPolicy = "Content-Security-Policy" HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only" HeaderXCSRFToken = "X-CSRF-Token" HeaderReferrerPolicy = "Referrer-Policy" )
Headers
const ( MIMETextHTML = "text/html" MIMETextPlain = "text/plain" MIMETextXML = "text/xml" MIMEApplicationJS = "application/javascript" MIMEApplicationXML = "application/xml" MIMEApplicationJSON = "application/json" MIMEApplicationProtobuf = "application/protobuf" MIMEApplicationMsgpack = "application/msgpack" MIMEOctetStream = "application/octet-stream" MIMEMultipartForm = "multipart/form-store" MIMEApplicationForm = "application/x-www-form-urlencoded" )
MIME types
const ( MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8 MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8 MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8 MIMEApplicationJSCharsetUTF8 = MIMEApplicationJS + "; " + charsetUTF8 MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8 MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8 )
MIME UTF8 types
const ( RequestDataTypeParam int = iota //params RequestDataTypeBody //POST FORM RequestDataTypeQuery //GET RequestDataTypeCookie //COOKIES RequestDataTypeHeader //HEADER )
const ( RoutePathName_Param string = ":" RoutePathName_Vague string = "*" )
Variables ¶
var ( ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType) ErrNotFound = NewHTTPError(http.StatusNotFound) ErrForbidden = NewHTTPError(http.StatusForbidden) ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed) ErrStatusRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge) ErrTooManyRequests = NewHTTPError(http.StatusTooManyRequests) ErrBadRequest = NewHTTPError(http.StatusBadRequest) ErrBadGateway = NewHTTPError(http.StatusBadGateway) ErrInternalServerError = NewHTTPError(http.StatusInternalServerError) ErrRequestTimeout = NewHTTPError(http.StatusRequestTimeout) ErrValidatorNotRegistered = errors.New("validator not registered") ErrRendererNotRegistered = errors.New("renderer not registered") ErrInvalidRedirectCode = errors.New("invalid redirect status code") ErrCookieNotFound = errors.New("cookie not found") ErrInvalidCertOrKeyType = errors.New("invalid cert or key type, must be string or []byte") )
Errors
var ( AnyHttpMethod = []string{ http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodConnect, http.MethodOptions, http.MethodTrace, } // Error handlers MethodNotFoundHandler = func(c *Context) error { return ErrNotFound } )
var RESTfulMethods = []string{ http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete, }
Functions ¶
func GetDataFromRequest ¶
func TLSConfigAutocert ¶
createTLS starts an HTTPS server using certificates automatically installed from https://letsencrypt.org.
func TLSConfigParse ¶
通过文件或者证书内容获取TLSConfig
Types ¶
type Context ¶
type Context struct { Path string Server *Server Session *Session Request *http.Request Response http.ResponseWriter // contains filtered or unexported fields }
Context API上下文.
func NewContext ¶
NewContext returns a Context instance.
func (*Context) Attachment ¶
func (*Context) Flush ¶
func (c *Context) Flush()
Flush implements the http.Flusher interface to allow an HTTP Handler to flush buffered store to the client. See http.Flusher(https://golang.org/pkg/net/http/#Flusher)
func (*Context) Hijack ¶
Hijack implements the http.Hijacker interface to allow an HTTP Handler to take over the connection. See http.Hijacker(https://golang.org/pkg/net/http/#Hijacker)
func (*Context) WriteHeader ¶
Status sends an HTTP Response header with status code. If Status is not called explicitly, the first call to Write will trigger an implicit Status(http.StatusOK). Thus explicit calls to Status are mainly used to send error codes.
type DefaultBinder ¶
type DefaultBinder struct{}
DefaultBinder is the default implementation of the Binder interface.
func (*DefaultBinder) Bind ¶
func (b *DefaultBinder) Bind(c *Context, i interface{}) (err error)
Bind implements the `Binder#Bind` function.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group 使用反射集中注册方法 可以使用 /prefix/$Group/$value 的格式访问
type GroupCaller ¶
GroupCaller 建议为每个注册的struct对象封装一个caller方法可以避免使用 reflect.Value.Call()方法
type GroupNode ¶
type GroupNode struct {
// contains filtered or unexported fields
}
GroupNode 节点,每个节点对应一个容器以及容器下面的所有接口
type HTTPError ¶
type HTTPError struct { Code int `json:"-"` Message interface{} `json:"message"` }
HTTPError represents an error that occurred while handling a Request.
func NewHTTPError ¶
NewHTTPError creates a new HTTPError instance.
func NewHTTPError500 ¶
func NewHTTPError500(message interface{}) *HTTPError
type HTTPErrorHandler ¶
HTTPErrorHandler is a centralized HTTP error Handler.
type HandlerFunc ¶
HandlerFunc defines a function to serve HTTP requests.
type MiddlewareFunc ¶
type MiddlewareFunc func(*Context, func())
MiddlewareFunc defines a function to process middleware.
type Node ¶
type Node struct { Route []string //当前路由绝对路径 Handler HandlerFunc //handler入口 // contains filtered or unexported fields }
type Options ¶
type Options struct { SessionKey string SessionType []int //存放SESSION KEY的方式 SessionSecret string SessionStorage session.Storage //Session数据存储器 }
func NewOptions ¶
func NewOptions() *Options
type Proxy ¶
type RESTful ¶
type RESTful struct {
// contains filtered or unexported fields
}
func NewRESTful ¶
func NewRESTful() *RESTful
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is the registry of all registered Routes for an `Server` instance for Request matching and URL path parameter parsing.
type Server ¶
type Server struct { Router *Router Debug bool //DEBUG模式会打印所有路由匹配状态,向客户端输出详细错误信息 Binder Binder Server *http.Server Options *Options Renderer Renderer HTTPErrorHandler HTTPErrorHandler // contains filtered or unexported fields }
Server is the top-level framework instance.
func (*Server) AcquireContext ¶
AcquireContext returns an empty `Ctx` instance from the pool. You must return the Context by calling `ReleaseContext()`.
func (*Server) Any ¶
func (s *Server) Any(path string, h HandlerFunc)
Any registers a new Register for all HTTP methods and path with matching Handler in the Router with optional Register-level middleware.
func (*Server) DefaultHTTPErrorHandler ¶
DefaultHTTPErrorHandler is the default HTTP error Handler. It sends a JSON Response with status code.
func (*Server) GET ¶
func (s *Server) GET(path string, h HandlerFunc)
GET registers a new GET Register for a path with matching Handler in the Router with optional Register-level middleware.
func (*Server) POST ¶
func (s *Server) POST(path string, h HandlerFunc)
POST registers a new POST Register for a path with matching Handler in the Router with optional Register-level middleware.
func (*Server) Register ¶
func (s *Server) Register(path string, handler HandlerFunc, method ...string)
AddTarget registers a new Register for an HTTP value and path with matching Handler in the Router with optional Register-level middleware.
func (*Server) ReleaseContext ¶
ReleaseContext returns the `Ctx` instance back to the pool. You must call it after `AcquireContext()`.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
func (*Server) Static ¶
Static registers a new Register with path prefix to serve static files from the provided root directory. 如果root 不是绝对路径 将以程序的WorkDir为基础
func (*Server) Use ¶
func (s *Server) Use(middleware ...MiddlewareFunc)
Use adds middleware to the chain which is run after Router.