Documentation
¶
Overview ¶
Package ship has implemented a flexible, powerful, high performance and minimalist Go Web HTTP router framework, which is inspired by echo and httprouter.
Index ¶
- Constants
- Variables
- func AddContentTypeMapping(contentType string, contentTypeSlice []string)
- func DisalbeRedirect(req *http.Request, via []*http.Request) error
- func InStrings(s string, ss []string) bool
- func IsInteger(s string) bool
- func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*http.Request, error)
- func PutResponseIntoPool(r *Response)
- func SetContentType(header http.Header, ct string)
- func SetContext(ctx context.Context, c *Context) (newctx context.Context)
- func SetStructFieldToDefault(v interface{}) (err error)
- func SplitHostPort(hostport string) (host, port string)
- func StartServer(addr string, handler http.Handler)
- func StartServerTLS(addr string, handler http.Handler, certFile, keyFile string)
- func ToHTTPHandler(s *Ship, h Handler) http.Handler
- type Binder
- type BinderFunc
- type BufferAllocator
- type Context
- func (c *Context) Accept() []string
- func (c *Context) AddRespHeader(name, value string)
- func (c *Context) Attachment(file string, name string) error
- func (c *Context) Bind(v interface{}) (err error)
- func (c *Context) BindQuery(v interface{}) (err error)
- func (c *Context) Blob(code int, contentType string, b []byte) (err error)
- func (c *Context) BlobText(code int, contentType string, format string, args ...interface{}) (err error)
- func (c *Context) BlobXML(code int, b []byte) (err error)
- func (c *Context) Body() io.ReadCloser
- func (c *Context) Charset() string
- func (c *Context) ClientIP() string
- func (c *Context) ContentLength() int64
- func (c *Context) ContentType() (ct string)
- func (c *Context) Cookie(name string) *http.Cookie
- func (c *Context) Cookies() []*http.Cookie
- func (c *Context) DelRespHeader(name string)
- func (c *Context) DelSession(id string) (err error)
- func (c *Context) Error(code int, err error) HTTPServerError
- func (c *Context) Execute() error
- func (c *Context) ExecuteRoute() error
- func (c *Context) File(file string) (err error)
- func (c *Context) FindRoute() (ok bool)
- func (c *Context) Form(name string, defaultValue ...string) string
- func (c *Context) FormFile(name string) (multipart.File, *multipart.FileHeader, error)
- func (c *Context) Forms() (url.Values, error)
- func (c *Context) GetReqHeader(name string, defaultValue ...string) string
- func (c *Context) GetSession(id string) (v interface{}, err error)
- func (c *Context) HTML(code int, htmlfmt string, htmlargs ...interface{}) error
- func (c *Context) Header() http.Header
- func (c *Context) Host() string
- func (c *Context) Hostname() string
- func (c *Context) Inline(file string, name string) error
- func (c *Context) IsAjax() bool
- func (c *Context) IsResponded() bool
- func (c *Context) IsTLS() bool
- func (c *Context) IsWebSocket() bool
- func (c *Context) JSON(code int, v interface{}) (err error)
- func (c *Context) Method() string
- func (c *Context) MultipartForm() (*multipart.Form, error)
- func (c *Context) MultipartReader() (*multipart.Reader, error)
- func (c *Context) NoContent(code int) error
- func (c *Context) Param(name string) string
- func (c *Context) ParamNames() []string
- func (c *Context) ParamValues() []string
- func (c *Context) Params() map[string]string
- func (c *Context) Path() string
- func (c *Context) Queries() url.Values
- func (c *Context) Query(name string, defaultValue ...string) string
- func (c *Context) QueryRawString() string
- func (c *Context) Redirect(code int, toURL string) error
- func (c *Context) Referer() string
- func (c *Context) RemoteAddr() string
- func (c *Context) Render(name string, code int, data interface{}) error
- func (c *Context) RenderOk(name string, data interface{}) error
- func (c *Context) ReqHeader() http.Header
- func (c *Context) Request() *http.Request
- func (c *Context) RequestURI() string
- func (c *Context) Reset()
- func (c *Context) RespHeader() http.Header
- func (c *Context) Respond(args ...interface{}) error
- func (c *Context) Response() *Response
- func (c *Context) ResponseWriter() http.ResponseWriter
- func (c *Context) Scheme() (scheme string)
- func (c *Context) SetConnectionClose()
- func (c *Context) SetContentType(ct string)
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) SetReqResp(r *http.Request, w http.ResponseWriter)
- func (c *Context) SetRequest(req *http.Request)
- func (c *Context) SetRespHeader(name, value string)
- func (c *Context) SetResponse(resp http.ResponseWriter)
- func (c *Context) SetSession(id string, value interface{}) (err error)
- func (c *Context) StatusCode() int
- func (c *Context) Stream(code int, contentType string, r io.Reader) (err error)
- func (c *Context) Text(code int, format string, args ...interface{}) error
- func (c *Context) URL(name string, params ...interface{}) string
- func (c *Context) UserAgent() string
- func (c *Context) Write(b []byte) (int, error)
- func (c *Context) WriteHeader(statusCode int)
- func (c *Context) XML(code int, v interface{}) (err error)
- type Defaulter
- type DefaulterFunc
- type HTTPClientError
- type HTTPServerError
- type Handler
- type HostHandler
- type HostManager
- type HostManagerHandler
- type Logger
- type Middleware
- type MuxBinder
- type MuxRenderer
- type OnceRunner
- type Renderer
- type RendererFunc
- type Response
- func (r *Response) Flush()
- func (r *Response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error)
- func (r *Response) Push(target string, opts *http.PushOptions) error
- func (r *Response) Reset(w http.ResponseWriter)
- func (r *Response) SetWriter(w http.ResponseWriter)
- func (r *Response) Write(b []byte) (n int, err error)
- func (r *Response) WriteHeader(code int)
- func (r *Response) WriteString(s string) (n int, err error)
- type Route
- type RouteBuilder
- func (r *RouteBuilder) Any(handler Handler) *RouteBuilder
- func (r *RouteBuilder) CONNECT(handler Handler) *RouteBuilder
- func (r *RouteBuilder) Clone() *RouteBuilder
- func (r *RouteBuilder) DELETE(handler Handler) *RouteBuilder
- func (r *RouteBuilder) Data(data interface{}) *RouteBuilder
- func (r *RouteBuilder) GET(handler Handler) *RouteBuilder
- func (r *RouteBuilder) Group() *RouteGroupBuilder
- func (r *RouteBuilder) HEAD(handler Handler) *RouteBuilder
- func (r *RouteBuilder) Map(method2handlers map[string]Handler) *RouteBuilder
- func (r *RouteBuilder) Method(handler Handler, methods ...string) *RouteBuilder
- func (r *RouteBuilder) Name(name string) *RouteBuilder
- func (r *RouteBuilder) OPTIONS(handler Handler) *RouteBuilder
- func (r *RouteBuilder) PATCH(handler Handler) *RouteBuilder
- func (r *RouteBuilder) POST(handler Handler) *RouteBuilder
- func (r *RouteBuilder) PUT(handler Handler) *RouteBuilder
- func (r *RouteBuilder) Redirect(code int, toURL string) *RouteBuilder
- func (r *RouteBuilder) Remove(method string) *RouteBuilder
- func (r *RouteBuilder) RemoveAny() *RouteBuilder
- func (r *RouteBuilder) RemoveCONNECT() *RouteBuilder
- func (r *RouteBuilder) RemoveDELETE() *RouteBuilder
- func (r *RouteBuilder) RemoveGET() *RouteBuilder
- func (r *RouteBuilder) RemoveHEAD() *RouteBuilder
- func (r *RouteBuilder) RemoveOPTIONS() *RouteBuilder
- func (r *RouteBuilder) RemovePATCH() *RouteBuilder
- func (r *RouteBuilder) RemovePOST() *RouteBuilder
- func (r *RouteBuilder) RemovePUT() *RouteBuilder
- func (r *RouteBuilder) RemoveTRACE() *RouteBuilder
- func (r *RouteBuilder) ResetMiddlewares(ms ...Middleware) *RouteBuilder
- func (r *RouteBuilder) Routes(handler Handler, methods ...string) []Route
- func (r *RouteBuilder) Ship() *Ship
- func (r *RouteBuilder) Static(dirpath string) *RouteBuilder
- func (r *RouteBuilder) StaticFS(fs http.FileSystem) *RouteBuilder
- func (r *RouteBuilder) TRACE(handler Handler) *RouteBuilder
- func (r *RouteBuilder) Use(middlewares ...Middleware) *RouteBuilder
- type RouteError
- type RouteGroupBuilder
- func (g *RouteGroupBuilder) AddRoutes(routes ...Route) *RouteGroupBuilder
- func (g *RouteGroupBuilder) Clone() *RouteGroupBuilder
- func (g *RouteGroupBuilder) Data(data interface{}) *RouteGroupBuilder
- func (g *RouteGroupBuilder) DelRoutes(routes ...Route) *RouteGroupBuilder
- func (g *RouteGroupBuilder) Group(prefix string, middlewares ...Middleware) *RouteGroupBuilder
- func (g *RouteGroupBuilder) ResetMiddlewares(ms ...Middleware) *RouteGroupBuilder
- func (g *RouteGroupBuilder) Route(path string) *RouteBuilder
- func (g *RouteGroupBuilder) Ship() *Ship
- func (g *RouteGroupBuilder) Use(middlewares ...Middleware) *RouteGroupBuilder
- type Router
- type Runner
- func (r *Runner) Link(other *Runner)
- func (r *Runner) RegisterOnShutdown(functions ...func())
- func (r *Runner) SetLogger(logger Logger) *Runner
- func (r *Runner) SetName(name string) *Runner
- func (r *Runner) Shutdown(ctx context.Context) (err error)
- func (r *Runner) Start(addr string, tlsFiles ...string)
- func (r *Runner) Stop()
- type Session
- type Ship
- func (s *Ship) AcquireBuffer() *bytes.Buffer
- func (s *Ship) AcquireContext(r *http.Request, w http.ResponseWriter) *Context
- func (s *Ship) AddRoute(r Route) (err error)
- func (s *Ship) AddRoutes(routes ...Route)
- func (s *Ship) Clone(name string, router Router) *Ship
- func (s *Ship) DelRoute(r Route) (err error)
- func (s *Ship) DelRoutes(routes ...Route)
- func (s *Ship) GetLogger() Logger
- func (s *Ship) GetName() string
- func (s *Ship) Group(prefix string) *RouteGroupBuilder
- func (s *Ship) HandleRequest(c *Context) error
- func (s *Ship) NewContext() *Context
- func (s *Ship) Pre(middlewares ...Middleware)
- func (s *Ship) ReleaseBuffer(buf *bytes.Buffer)
- func (s *Ship) ReleaseContext(c *Context)
- func (s *Ship) ResetMiddlewares(middlewares ...Middleware)
- func (s *Ship) ResetPreMiddlewares(middlewares ...Middleware)
- func (s *Ship) Route(path string) *RouteBuilder
- func (s *Ship) Routes() (routes []Route)
- func (s *Ship) ServeHTTP(resp http.ResponseWriter, req *http.Request)
- func (s *Ship) SetBufferSize(size int)
- func (s *Ship) Use(middlewares ...Middleware)
- type Validator
- type ValidatorFunc
Examples ¶
Constants ¶
const ( CharsetUTF8 = "charset=UTF-8" PROPFIND = "PROPFIND" )
Predefine some variables
const ( MIMETextXML = "text/xml" MIMETextHTML = "text/html" MIMETextPlain = "text/plain" MIMEApplicationXML = "application/xml" MIMEApplicationJSON = "application/json" MIMEApplicationJavaScript = "application/javascript" MIMEApplicationForm = "application/x-www-form-urlencoded" MIMEApplicationProtobuf = "application/protobuf" MIMEApplicationMsgpack = "application/msgpack" MIMEOctetStream = "application/octet-stream" MIMEMultipartForm = "multipart/form-data" MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + CharsetUTF8 MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + CharsetUTF8 MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + CharsetUTF8 MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + CharsetUTF8 MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + CharsetUTF8 MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + CharsetUTF8 )
MIME types
const ( HeaderAccept = "Accept" // RFC 7231, 5.3.2 HeaderAcceptCharset = "Accept-Charset" // RFC 7231, 5.3.3 HeaderAcceptEncoding = "Accept-Encoding" // RFC 7231, 5.3.4 HeaderAcceptLanguage = "Accept-Language" // RFC 7231, 5.3.5 HeaderAcceptPatch = "Accept-Patch" // RFC 5789, 3.1 HeaderAcceptRanges = "Accept-Ranges" // RFC 7233, 2.3 HeaderAge = "Age" // RFC 7234, 5.1 HeaderAllow = "Allow" // RFC 7231, 7.4.1 HeaderAuthorization = "Authorization" // RFC 7235, 4.2 HeaderCacheControl = "Cache-Control" // RFC 7234, 5.2 HeaderConnection = "Connection" // RFC 7230, 6.1 HeaderContentDisposition = "Content-Disposition" // RFC 6266 HeaderContentEncoding = "Content-Encoding" // RFC 7231, 3.1.2.2 HeaderContentLanguage = "Content-Language" // RFC 7231, 3.1.3.2 HeaderContentLength = "Content-Length" // RFC 7230, 3.3.2 HeaderContentLocation = "Content-Location" // RFC 7231, 3.1.4.2 HeaderContentRange = "Content-Range" // RFC 7233, 4.2 HeaderContentType = "Content-Type" // RFC 7231, 3.1.1.5 HeaderCookie = "Cookie" // RFC 2109, 4.3.4 HeaderDate = "Date" // RFC 7231, 7.1.1.2 HeaderETag = "ETag" // RFC 7232, 2.3 HeaderExpect = "Expect" // RFC 7231, 5.1.1 HeaderExpires = "Expires" // RFC 7234, 5.3 HeaderFrom = "From" // RFC 7231, 5.5.1 HeaderHost = "Host" // RFC 7230, 5.4 HeaderIfMatch = "If-Match" // RFC 7232, 3.1 HeaderIfModifiedSince = "If-Modified-Since" // RFC 7232, 3.3 HeaderIfNoneMatch = "If-None-Match" // RFC 7232, 3.2 HeaderIfRange = "If-Range" // RFC 7233, 3.2 HeaderIfUnmodifiedSince = "If-Unmodified-Since" // RFC 7232, 3.4 HeaderLastModified = "Last-Modified" // RFC 7232, 2.2 HeaderLink = "Link" // RFC 5988 HeaderLocation = "Location" // RFC 7231, 7.1.2 HeaderMaxForwards = "Max-Forwards" // RFC 7231, 5.1.2 HeaderOrigin = "Origin" // RFC 6454 HeaderPragma = "Pragma" // RFC 7234, 5.4 HeaderProxyAuthenticate = "Proxy-Authenticate" // RFC 7235, 4.3 HeaderProxyAuthorization = "Proxy-Authorization" // RFC 7235, 4.4 HeaderRange = "Range" // RFC 7233, 3.1 HeaderReferer = "Referer" // RFC 7231, 5.5.2 HeaderRetryAfter = "Retry-After" // RFC 7231, 7.1.3 HeaderServer = "Server" // RFC 7231, 7.4.2 HeaderSetCookie = "Set-Cookie" // RFC 2109, 4.2.2 HeaderSetCookie2 = "Set-Cookie2" // RFC 2965 HeaderTE = "TE" // RFC 7230, 4.3 HeaderTrailer = "Trailer" // RFC 7230, 4.4 HeaderTransferEncoding = "Transfer-Encoding" // RFC 7230, 3.3.1 HeaderUpgrade = "Upgrade" // RFC 7230, 6.7 HeaderUserAgent = "User-Agent" // RFC 7231, 5.5.3 HeaderVary = "Vary" // RFC 7231, 7.1.4 HeaderVia = "Via" // RFC 7230, 5.7.1 HeaderWarning = "Warning" // RFC 7234, 5.5 HeaderWWWAuthenticate = "WWW-Authenticate" // RFC 7235, 4.1 HeaderForwarded = "Forwarded" // RFC 7239 HeaderXForwardedBy = "X-Forwarded-By" // RFC 7239, 5.1 HeaderXForwardedFor = "X-Forwarded-For" // RFC 7239, 5.2 HeaderXForwardedHost = "X-Forwarded-Host" // RFC 7239, 5.3 HeaderXForwardedPort = "X-Forwarded-Port" 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" HeaderXServerID = "X-Server-Id" HeaderXRequestID = "X-Request-Id" HeaderXRequestedWith = "X-Requested-With" // Access control HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" // https://www.w3.org/TR/cors/#http-access-control-allow-credentials HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" // https://www.w3.org/TR/cors/#http-access-control-allow-headers HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" // https://www.w3.org/TR/cors/#http-access-control-allow-methods HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" // https://www.w3.org/TR/cors/#http-access-control-allow-origin HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" // https://www.w3.org/TR/cors/#http-access-control-expose-headers HeaderAccessControlMaxAge = "Access-Control-Max-Age" // https://www.w3.org/TR/cors/#http-access-control-max-age HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" // https://www.w3.org/TR/cors/#http-access-control-request-headers HeaderAccessControlRequestMethod = "Access-Control-Request-Method" // https://www.w3.org/TR/cors/#http-access-control-request-method // Security 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" )
Headers
Variables ¶
var ( ErrMissingContentType = errors.New("missing the header 'Content-Type'") ErrInvalidRedirectCode = errors.New("invalid redirect status code") ErrSessionNotExist = errors.New("session does not exist") ErrInvalidSession = errors.New("invalid session") )
Some non-HTTP Errors
var ( ErrBadRequest = NewHTTPServerError(http.StatusBadRequest) ErrForbidden = NewHTTPServerError(http.StatusForbidden) ErrNotFound = NewHTTPServerError(http.StatusNotFound) ErrMethodNotAllowed = NewHTTPServerError(http.StatusMethodNotAllowed) ErrStatusNotAcceptable = NewHTTPServerError(http.StatusNotAcceptable) ErrRequestTimeout = NewHTTPServerError(http.StatusRequestTimeout) ErrStatusConflict = NewHTTPServerError(http.StatusConflict) ErrStatusGone = NewHTTPServerError(http.StatusGone) ErrStatusRequestEntityTooLarge = NewHTTPServerError(http.StatusRequestEntityTooLarge) ErrUnsupportedMediaType = NewHTTPServerError(http.StatusUnsupportedMediaType) ErrTooManyRequests = NewHTTPServerError(http.StatusTooManyRequests) ErrInternalServerError = NewHTTPServerError(http.StatusInternalServerError) ErrStatusNotImplemented = NewHTTPServerError(http.StatusNotImplemented) ErrBadGateway = NewHTTPServerError(http.StatusBadGateway) ErrStatusGatewayTimeout = NewHTTPServerError(http.StatusGatewayTimeout) ErrStatusHTTPVersionNotSupported = NewHTTPServerError(http.StatusHTTPVersionNotSupported) )
Some HTTP error.
var DefaultShip = Default()
DefaultShip is the default global ship.
var DefaultSignals = []os.Signal{ os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGABRT, syscall.SIGINT, }
DefaultSignals is a set of default signals.
var ErrSkip = errors.New("skip")
ErrSkip is not an error, which is used to suggest that the middeware should skip and return it back to the outer middleware to handle.
var IsDomainName func(domainName string) bool = isDomainName
IsDomainName is used to check whether the domain name is valid or not. And you can reset it to a customized one.
The default implementation has these limits as follow:
- The maximum length of the full qualified domain name is equal to 253.
- The maximum length of the sub-domain name is equal to 63.
- The valid characters only contain "a-zA-z0-9_-.".
var MaxMemoryLimit int64 = 32 << 20 // 32MB
MaxMemoryLimit is the maximum memory.
Functions ¶
func AddContentTypeMapping ¶
AddContentTypeMapping add a content type mapping to convert contentType to contentTypeSlice, which is used by SetContentType to set the header "Content-Type" to contentTypeSlice by contentType to avoid allocating the memory.
If contentTypeSlice is empty, it is []string{contentType} by default.
func DisalbeRedirect ¶
DisalbeRedirect is used to disalbe the default redirect behavior of http.Client, that's, http.Client won't handle the redirect response and just return it to the caller.
func NewRequestWithContext ¶
func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*http.Request, error)
NewRequestWithContext is the compatibility of http.NewRequestWithContext.
func PutResponseIntoPool ¶
func PutResponseIntoPool(r *Response)
PutResponseIntoPool puts a Response into the pool.
func SetContentType ¶
SetContentType sets the header "Content-Type" to ct.
func SetContext ¶ added in v5.1.2
SetContext sets the http request context into the context.
func SetStructFieldToDefault ¶
func SetStructFieldToDefault(v interface{}) (err error)
SetStructFieldToDefault sets the default value of the fields of the pointer to struct v to the value of the tag "default" of the fields when the field value is ZERO.
For the type of the field, it only supports some base types as follow:
string float32 float64 int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 struct struct slice interface{ SetDefault(_default interface{}) error } time.Time // Format: A. Integer(UTC); B. String(RFC3339) time.Duration // Format: A. Integer(ms); B. String(time.ParseDuration) pointer to the types above
Notice: If the tag value starts with ".", it represents a field name and the default value of current field is set to the value of that field. But their types must be consistent, or panic.
Example ¶
type Struct struct { InnerInt int `default:"123"` _ int `default:"-"` } type S struct { Ignore bool `default:"true"` Int int `default:"123"` Int8 int8 `default:"123"` Int16 int16 `default:"123"` Int32 int32 `default:"123"` Int64 int64 `default:"123"` Uint uint `default:"123"` Uint8 uint8 `default:"123"` Uint16 uint16 `default:"123"` Uint32 uint32 `default:"123"` Uint64 uint64 `default:"123"` Uintptr uintptr `default:"123"` Float32 float32 `default:"1.2"` Float64 float64 `default:"1.2"` FloatN float64 `default:".Float64"` // Set the default value to other field String string `default:"abc"` Struct Struct Structs []Struct _ int `default:"-"` DurationInt time.Duration `default:"1000"` DurationStr time.Duration `default:"2s"` TimeInt time.Time `default:"1618059388"` TimeStr time.Time `default:"2021-04-10T12:56:28Z"` NoneP *int IntPtr *int `default:"456"` TimePtr *time.Time `default:"2021-04-10T12:56:28Z"` DurationPtr *time.Duration `default:"3s"` } s := S{Structs: make([]Struct, 2)} err := SetStructFieldToDefault(&s) fmt.Println(err) fmt.Println(s.Ignore) fmt.Println(s.Int) fmt.Println(s.Int8) fmt.Println(s.Int16) fmt.Println(s.Int32) fmt.Println(s.Int64) fmt.Println(s.Uint) fmt.Println(s.Uint8) fmt.Println(s.Uint16) fmt.Println(s.Uint32) fmt.Println(s.Uint64) fmt.Println(s.Uintptr) fmt.Println(s.Float32) fmt.Println(s.Float64) fmt.Println(s.FloatN) fmt.Println(s.String) fmt.Println(s.Struct.InnerInt) fmt.Println(s.Structs[0].InnerInt) fmt.Println(s.Structs[1].InnerInt) fmt.Println(s.DurationInt) fmt.Println(s.DurationStr) fmt.Println(s.TimeInt.UTC().Format(time.RFC3339)) fmt.Println(s.TimeStr.UTC().Format(time.RFC3339)) fmt.Println(s.NoneP == nil) fmt.Println(*s.IntPtr) fmt.Println(s.TimePtr.UTC().Format(time.RFC3339)) fmt.Println(*s.DurationPtr)
Output: <nil> false 123 123 123 123 123 123 123 123 123 123 123 1.2 1.2 1.2 abc 123 123 123 1s 2s 2021-04-10T12:56:28Z 2021-04-10T12:56:28Z true 456 2021-04-10T12:56:28Z 3s
func SplitHostPort ¶
SplitHostPort separates host and port. If the port is not valid, it returns the entire input as host, and it doesn't check the validity of the host. Unlike net.SplitHostPort, but per RFC 3986, it requires ports to be numeric.
Example ¶
var host, port string host, port = SplitHostPort("www.example.com") fmt.Printf("Host: %s, Port: %s#\n", host, port) host, port = SplitHostPort("www.example.com:80") fmt.Printf("Host: %s, Port: %s#\n", host, port) host, port = SplitHostPort(":80") fmt.Printf("Host: %s, Port: %s#\n", host, port) host, port = SplitHostPort("1.2.3.4:80") fmt.Printf("Host: %s, Port: %s#\n", host, port) host, port = SplitHostPort("[fe80::1122:3344:5566:7788]") fmt.Printf("Host: %s, Port: %s#\n", host, port) host, port = SplitHostPort("[fe80::1122:3344:5566:7788]:80") fmt.Printf("Host: %s, Port: %s#\n", host, port)
Output: Host: www.example.com, Port: # Host: www.example.com, Port: 80# Host: , Port: 80# Host: 1.2.3.4, Port: 80# Host: fe80::1122:3344:5566:7788, Port: # Host: fe80::1122:3344:5566:7788, Port: 80#
func StartServer ¶
StartServer is convenient function to new a runner to start the http server.
func StartServerTLS ¶
StartServerTLS is the same as StartServer, and tries to start the http server with the cert and key file. If certFile or keyFile is empty, however, it is equal to StartServer.
Types ¶
type Binder ¶
type Binder interface { // Bind parses the data from http.Request to dst. // // Notice: dst must be a non-nil pointer. Bind(dst interface{}, req *http.Request) error }
Binder is the interface to bind the value dst to req.
func FormBinder ¶
FormBinder returns a binder to bind the data to the request body as Form.
Notice: The bound value must be a pointer to a struct with the tag named tag, which is "form" by default.
func JSONBinder ¶
func JSONBinder() Binder
JSONBinder returns a binder to bind the data to the request body as JSON.
type BinderFunc ¶
BinderFunc is a function type implementing the interface Binder.
type BufferAllocator ¶
BufferAllocator is used to acquire and release a buffer.
type Context ¶
type Context struct { // Route is the route information associated with the route. Route Route // Any is the any context data associated with the route. // // Notice: when the new request is coming, they will be reset to nil. Any interface{} // Data is used to store many key-value pairs about the context. // // Notice: when the new request is coming, they will be cleaned out. Data map[string]interface{} // Public Configuration, which are not reset when calling Reset(). BufferAllocator Logger Router Router Session Session NotFound Handler Binder Binder Renderer Renderer Defaulter Defaulter Validator Validator Responder func(*Context, ...interface{}) error QueryBinder func(interface{}, url.Values) error // contains filtered or unexported fields }
Context represetns a request and response context.
func GetContext ¶ added in v5.1.2
GetContext returns the http reqeust context from the context.
func NewContext ¶
NewContext returns a new Context.
func (*Context) Accept ¶
Accept returns the accepted Content-Type list from the request header "Accept", which are sorted by the q-factor weight from high to low.
If there is no the request header "Accept", return nil.
Notice:
- If the value is "*/*", it will be amended as "".
- If the value is "<MIME_type>/*", it will be amended as "<MIME_type>/". So it can be used to match the prefix.
func (*Context) AddRespHeader ¶
AddRespHeader appends the value into the response header named name.
func (*Context) Attachment ¶
Attachment is the same as File, but sets the header "Content-Disposition" with the type "attachment" to prompt the client to save the file with the name.
If the file does not exist, it returns ErrNotFound.
func (*Context) Bind ¶
Bind extracts the data information from the request and assigns it to v, then validates whether it is valid or not.
func (*Context) BindQuery ¶
BindQuery extracts the data from the request url query and assigns it to v, then validates whether it is valid or not.
func (*Context) BlobText ¶
func (c *Context) BlobText(code int, contentType string, format string, args ...interface{}) (err error)
BlobText sends a string blob response with the status code and the content type.
func (*Context) Body ¶
func (c *Context) Body() io.ReadCloser
Body returns the reader of the request body.
func (*Context) Charset ¶
Charset returns the charset of the request content.
Return "" if there is no charset.
func (*Context) ClientIP ¶
ClientIP returns the real client's network address based on `X-Forwarded-For` or `X-Real-Ip` request header. Or returns the remote address.
func (*Context) ContentLength ¶
ContentLength return the length of the request body.
func (*Context) ContentType ¶
ContentType returns the Content-Type of the request without the charset.
func (*Context) Cookie ¶
Cookie returns the named cookie provided in the request.
Return nil if no the cookie named name.
func (*Context) DelRespHeader ¶
DelRespHeader deletes the response header named name.
func (*Context) DelSession ¶
DelSession deletes the session from the backend store.
func (*Context) Error ¶
func (c *Context) Error(code int, err error) HTTPServerError
Error returns a http error with the status code.
func (*Context) Execute ¶
Execute finds the route by the request method and path, then executes the handler of the found route, which is equal to the union of FindRoute and ExecuteRoute.
func (*Context) ExecuteRoute ¶
ExecuteRoute executes the handler of the found route.
Notice: You should call FindRoute before calling this method.
func (*Context) File ¶
File sends a file response, and the body is the content of the file.
If not set the Content-Type, it will deduce it from the extension of the file name. If the file does not exist, it returns ErrNotFound.
func (*Context) FindRoute ¶
FindRoute finds the route by the request method and path and put it into the field Route of Context.
For the handler registered into the underlying Router, it supports three kinds of types as follow:
- Route
- Handler
- http.Handler
- http.HandlerFunc
func (*Context) Form ¶
Form returns the form value by the field name.
Return defaultValue instead if the form field name does not exist.
func (*Context) GetReqHeader ¶
GetReqHeader returns the first value of the request header named name.
Return defaultValue instead if the header does not exist.
func (*Context) GetSession ¶
GetSession returns the session content by id from the backend store.
If the session id does not exist, it returns ErrSessionNotExist.
func (*Context) Header ¶
Header implements the interface http.ResponseWriter, which is the alias of RespHeader.
func (*Context) Inline ¶
Inline sends a file response as the inline to open the file in the browser.
If the file does not exist, it returns ErrNotFound.
func (*Context) IsResponded ¶
IsResponded reports whether the response is sent or not.
func (*Context) IsWebSocket ¶
IsWebSocket reports whether HTTP connection is WebSocket or not.
func (*Context) MultipartForm ¶
MultipartForm returns the multipart form.
func (*Context) MultipartReader ¶
MultipartReader returns the multipart reader from the request.
func (*Context) ParamNames ¶
ParamNames returns the names of all the URL parameters.
func (*Context) ParamValues ¶
ParamValues returns the values of all the URL parameters.
func (*Context) Query ¶
Query returns the query value by the query name.
Return defaultValue instead if the query name does not exist.
func (*Context) QueryRawString ¶
QueryRawString returns the URL query string.
func (*Context) RemoteAddr ¶
RemoteAddr returns the remote address of the http connection.
func (*Context) Render ¶
Render renders a template named name with data and sends it as the response with status code.
func (*Context) RequestURI ¶
RequestURI returns the URI of the request.
func (*Context) Reset ¶
func (c *Context) Reset()
Reset resets the context to the initalizing state.
func (*Context) RespHeader ¶
RespHeader returns the header of the response.
func (*Context) ResponseWriter ¶
func (c *Context) ResponseWriter() http.ResponseWriter
ResponseWriter returns the underlying http.ResponseWriter.
func (*Context) SetConnectionClose ¶
func (c *Context) SetConnectionClose()
SetConnectionClose sets the response header "Connection: close" to tell the server to close the connection.
func (*Context) SetContentType ¶
SetContentType sets the response header "Content-Type" to ct,
If ct is "", do nothing.
func (*Context) SetReqResp ¶
func (c *Context) SetReqResp(r *http.Request, w http.ResponseWriter)
SetReqResp is the same as Reset, but only reset the request and response, not all things.
func (*Context) SetRequest ¶
SetRequest resets the request to req.
func (*Context) SetRespHeader ¶
SetRespHeader sets the response header name to value.
func (*Context) SetResponse ¶
func (c *Context) SetResponse(resp http.ResponseWriter)
SetResponse resets the response to resp, which will ignore nil.
func (*Context) SetSession ¶
SetSession sets the session to the backend store.
func (*Context) StatusCode ¶
StatusCode returns the status code of the response.
func (*Context) Stream ¶
Stream sends a streaming response with the status code and the content type.
func (*Context) URL ¶
URL generates a url path by the route path name and provided parameters.
Return "" if there is not the route named name.
func (*Context) Write ¶
Write implements the interface http.ResponseWriter.
It will write the response header with the status code 200 firstly if the header is not sent.
func (*Context) WriteHeader ¶
WriteHeader implements the interface http.ResponseWriter.
type Defaulter ¶
type Defaulter interface {
SetDefault(data interface{}) error
}
Defaulter is used to set the default value if the data or the field of data is ZERO.
func NothingDefaulter ¶
func NothingDefaulter() Defaulter
NothingDefaulter returns a Defaulter that does nothing.
type DefaulterFunc ¶
type DefaulterFunc func(interface{}) error
DefaulterFunc is the function type implementing the interface Defaulter.
func (DefaulterFunc) SetDefault ¶
func (d DefaulterFunc) SetDefault(data interface{}) error
SetDefault implements the interface Defaulter.
type HTTPClientError ¶
type HTTPClientError struct { Code int `json:"code" xml:"code"` Method string `json:"method" xml:"method"` URL string `json:"url" xml:"url"` Data string `json:"data" xml:"data"` Err error `json:"err" xml:"err"` }
HTTPClientError represents an response error from the http client.
func NewHTTPClientError ¶
func NewHTTPClientError(method, url string, code int, err error, data ...string) HTTPClientError
NewHTTPClientError returns a new HTTPClientError.
func (HTTPClientError) Error ¶
func (e HTTPClientError) Error() string
func (HTTPClientError) String ¶
func (e HTTPClientError) String() string
func (HTTPClientError) Unwrap ¶
func (e HTTPClientError) Unwrap() error
type HTTPServerError ¶
HTTPServerError represents a server error with HTTP Status Code.
func NewHTTPServerError ¶
func NewHTTPServerError(code int, msg ...string) HTTPServerError
NewHTTPServerError returns a new HTTPServerError.
func (HTTPServerError) Error ¶
func (e HTTPServerError) Error() string
func (HTTPServerError) New ¶
func (e HTTPServerError) New(err error) HTTPServerError
New returns a new HTTPServerError with the new error.
func (HTTPServerError) NewCT ¶
func (e HTTPServerError) NewCT(ct string) HTTPServerError
NewCT returns a new HTTPServerError with the new ContentType ct.
func (HTTPServerError) Newf ¶
func (e HTTPServerError) Newf(msg string, args ...interface{}) HTTPServerError
Newf is equal to New(fmt.Errorf(msg, args...)).
func (HTTPServerError) Unwrap ¶
func (e HTTPServerError) Unwrap() error
Unwrap unwraps the inner error.
type Handler ¶
Handler is a handler of the HTTP request.
func FromHTTPHandler ¶
FromHTTPHandler converts http.Handler to Handler.
func FromHTTPHandlerFunc ¶
func FromHTTPHandlerFunc(h http.HandlerFunc) Handler
FromHTTPHandlerFunc converts http.HandlerFunc to Handler.
func MethodNotAllowedHandler ¶
MethodNotAllowedHandler returns a MethodNotAllowed handler.
type HostHandler ¶
type HostHandler interface { http.Handler HostManager }
HostHandler is a http handler to dispatch the request to the host handler by the request host, which may be used to implement the virtual hosts.
type HostManager ¶
type HostManager interface { // Len returns the number of the hosts. Len() int // Range is used to traverse all the hosts. Range(func(host string, handler http.Handler)) // AddHost adds the host and the handler, then returns the added handler. // // If the host has been added, return the added handler. AddHost(host string, handler http.Handler) (http.Handler, error) // DelHost deletes the host accurately and returns the handler. // // If the host does not exist, return nil. DelHost(host string) http.Handler // GetHost matches the host accurately and returns the handler. // // If the host does not exist, return nil. GetHost(host string) http.Handler // MatchHost matches the host by the implemented rules, such as the regular // expression, and returns the corresponding handler. // // If there is no host to match it, return ("", nil). MatchHost(host string) (matchedHost string, matchedHandler http.Handler) }
HostManager is used to manage the domain hosts.
func NewHostManager ¶
func NewHostManager(regexpHostManager HostManager) HostManager
NewHostManager returns a new HostManager implementation, which uses IsDomainName to check whether a host name is the valid domain and supports three kinds of hosts:
- Exact: a valid domain, such as "www.example.com".
- Prefix: a valid domain with the suffix ".*", such as "www.example.*".
- Suffix: a valid domain with the prefix "*.", such as "*.example.com".
- Regexp: a valid regular expression defined by regexpHostManager.
Notice: if the host name is not any of the exact, prefix and suffix formats, it will be regarded as the regexp host name.
If regexpHostManager is nil, it is NewRegexpHostManager() by default.
func NewLockHostManager ¶
func NewLockHostManager(hm HostManager) HostManager
NewLockHostManager returns a new HostManager based on lock to manage the hosts safely.
func NewRegexpHostManager ¶
func NewRegexpHostManager() HostManager
NewRegexpHostManager returns a new HostManager based on regular expression, which uses the stdlib "regexp" to implement the regular expression syntax.
For the golang regexp syntax, see https://pkg.go.dev/regexp/syntax.
type HostManagerHandler ¶
type HostManagerHandler struct { HostManager // HandleHTTP is used to handle the matched host and handler. // // If not found the matched host and handler, matchedHost and matchedHandler // are ZERO, that's, "" and nil. // // Default: w.WriteHeader(404) HandleHTTP func(w http.ResponseWriter, r *http.Request, matchedHost string, matchedHandler http.Handler) // contains filtered or unexported fields }
HostManagerHandler is an implementation of HostHandler.
func NewHostManagerHandler ¶
func NewHostManagerHandler(hostManager HostManager) *HostManagerHandler
NewHostManagerHandler returns a new HostManagerHandler.
If hostManager is nil, it is NewHostManager(nil) by default.
func (*HostManagerHandler) GetDefaultHost ¶
func (h *HostManagerHandler) GetDefaultHost() (host string, handler http.Handler)
GetDefaultHost returns the default host and handler.
Return ("", nil) if the default host is not set.
func (*HostManagerHandler) ServeHTTP ¶
func (h *HostManagerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the interface http.Handler.
func (*HostManagerHandler) SetDefaultHost ¶
func (h *HostManagerHandler) SetDefaultHost(host string, handler http.Handler)
SetDefaultHost sets the default host and handler.
type Logger ¶
type Logger interface { Tracef(format string, args ...interface{}) Debugf(format string, args ...interface{}) Infof(format string, args ...interface{}) Warnf(format string, args ...interface{}) Errorf(format string, args ...interface{}) }
Logger is logger interface.
Notice: The implementation maybe also has the method { Writer() io.Writer } to get the underlynig writer.
func NewLoggerFromStdlog ¶
NewLoggerFromStdlog converts stdlib log to Logger.
Notice: the returned logger has also implemented the interface { Writer() io.Writer }.
type Middleware ¶
Middleware represents a middleware.
func NewMiddleware ¶ added in v5.1.4
func NewMiddleware(handle func(http.Handler, http.ResponseWriter, *http.Request) error) Middleware
NewMiddleware returns a common middleware with the handler.
Notice: the wrapped http.Handler has implemented the interface
type interface { HandleHTTP(http.ResponseWriter, *http.Request) error }
So it can be used to wrap the error returned by other middleware handlers.
type MuxBinder ¶
type MuxBinder struct {
// contains filtered or unexported fields
}
MuxBinder is a multiplexer for kinds of Binders based on the request header "Content-Type".
func (*MuxBinder) Bind ¶
Bind implements the interface Binder, which looks up the registered binder by the request header "Content-Type" and calls it to bind the value dst to req.
type MuxRenderer ¶
type MuxRenderer struct {
// contains filtered or unexported fields
}
MuxRenderer is a multiplexer for kinds of Renderers.
func (*MuxRenderer) Add ¶
func (mr *MuxRenderer) Add(suffix string, renderer Renderer)
Add adds a renderer with a suffix identifier.
func (*MuxRenderer) Del ¶
func (mr *MuxRenderer) Del(suffix string)
Del removes the corresponding renderer by the suffix.
func (*MuxRenderer) Get ¶
func (mr *MuxRenderer) Get(suffix string) Renderer
Get returns the corresponding renderer by the suffix.
Return nil if not found.
func (*MuxRenderer) Render ¶
func (mr *MuxRenderer) Render(w http.ResponseWriter, name string, code int, data interface{}) error
Render implements the interface Renderer, which will get the renderer the name suffix then render the content.
type OnceRunner ¶
type OnceRunner struct {
// contains filtered or unexported fields
}
OnceRunner is used to run the task only once, which is different from sync.Once, the second calling does not wait until the first calling finishes.
func NewOnceRunner ¶
func NewOnceRunner(task func()) *OnceRunner
NewOnceRunner returns a new OnceRunner.
type Renderer ¶
type Renderer interface {
Render(w http.ResponseWriter, name string, code int, data interface{}) error
}
Renderer is the interface to render the response.
type RendererFunc ¶
type RendererFunc func(http.ResponseWriter, string, int, interface{}) error
RendererFunc is the function type implementing the interface Renderer.
func (RendererFunc) Render ¶
func (f RendererFunc) Render(w http.ResponseWriter, name string, code int, data interface{}) error
Render implements the interface Renderer.
type Response ¶
type Response struct { http.ResponseWriter Size int64 Wrote bool Status int }
Response implements the interface http.ResponseWriter.
func GetResponseFromPool ¶
func GetResponseFromPool(w http.ResponseWriter) *Response
GetResponseFromPool returns a Response from the pool.
func NewResponse ¶
func NewResponse(w http.ResponseWriter) *Response
NewResponse returns a new instance of Response.
func (*Response) Flush ¶
func (r *Response) Flush()
Flush implements the http.Flusher interface to allow an HTTP handler to flush buffered data to the client.
func (*Response) 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 (*Response) Push ¶
func (r *Response) Push(target string, opts *http.PushOptions) error
Push implements the http.Pusher interface to support HTTP/2 server push.
func (*Response) Reset ¶
func (r *Response) Reset(w http.ResponseWriter)
Reset resets the response to the initialized status.
func (*Response) SetWriter ¶
func (r *Response) SetWriter(w http.ResponseWriter)
SetWriter resets the writer to w and return itself.
func (*Response) WriteHeader ¶
WriteHeader implements http.ResponseWriter#WriteHeader().
type Route ¶
type Route struct { // Path and Method represent the unique route in a certain host. // // Path maybe contain the parameters, which is determined by the underlying // router. And if Method is empty, it stands for all the methods. Path string `json:"path,omitempty" xml:"path,omitempty"` Method string `json:"method,omitempty" xml:"method,omitempty"` // Name is the name of the path, which may be empty to represent no name. Name string `json:"name,omitempty" xml:"name,omitempty"` // Handler is the handler of the route to handle the request. Handler Handler `json:"-" xml:"-"` // Data is any additional data associated with the route. Data interface{} `json:"data,omitempty" xml:"data,omitempty"` }
Route is used to represent the information of the registered route.
type RouteBuilder ¶
type RouteBuilder struct {
// contains filtered or unexported fields
}
RouteBuilder is used to build a route.
func (*RouteBuilder) Any ¶
func (r *RouteBuilder) Any(handler Handler) *RouteBuilder
Any registers all the supported methods , which is short for r.Method(handler, "")
func (*RouteBuilder) CONNECT ¶
func (r *RouteBuilder) CONNECT(handler Handler) *RouteBuilder
CONNECT is the short for r.Method(handler, "CONNECT").
func (*RouteBuilder) Clone ¶
func (r *RouteBuilder) Clone() *RouteBuilder
Clone clones a new route builder.
func (*RouteBuilder) DELETE ¶
func (r *RouteBuilder) DELETE(handler Handler) *RouteBuilder
DELETE is the short for r.Method(handler, "DELETE").
func (*RouteBuilder) Data ¶
func (r *RouteBuilder) Data(data interface{}) *RouteBuilder
Data sets the context data.
func (*RouteBuilder) GET ¶
func (r *RouteBuilder) GET(handler Handler) *RouteBuilder
GET is the short for r.Method(handler, "GET").
func (*RouteBuilder) Group ¶
func (r *RouteBuilder) Group() *RouteGroupBuilder
Group returns the route group builder that the current route belongs to, which maybe return nil.
func (*RouteBuilder) HEAD ¶
func (r *RouteBuilder) HEAD(handler Handler) *RouteBuilder
HEAD is the short for r.Method(handler, "HEAD").
func (*RouteBuilder) Map ¶
func (r *RouteBuilder) Map(method2handlers map[string]Handler) *RouteBuilder
Map registers a group of methods with handlers, which is equal to
for method, handler := range method2handlers { r.Method(handler, method) }
func (*RouteBuilder) Method ¶
func (r *RouteBuilder) Method(handler Handler, methods ...string) *RouteBuilder
Method registers the routes with the handler and methods.
It will panic with it if there is an error when adding the routes.
func (*RouteBuilder) Name ¶
func (r *RouteBuilder) Name(name string) *RouteBuilder
Name sets the route name.
func (*RouteBuilder) OPTIONS ¶
func (r *RouteBuilder) OPTIONS(handler Handler) *RouteBuilder
OPTIONS is the short for r.Method(handler, "OPTIONS").
func (*RouteBuilder) PATCH ¶
func (r *RouteBuilder) PATCH(handler Handler) *RouteBuilder
PATCH is the short for r.Method(handler, "PATCH").
func (*RouteBuilder) POST ¶
func (r *RouteBuilder) POST(handler Handler) *RouteBuilder
POST is the short for r.Method(handler, "POST").
func (*RouteBuilder) PUT ¶
func (r *RouteBuilder) PUT(handler Handler) *RouteBuilder
PUT is the short for r.Method(handler, "PUT").
func (*RouteBuilder) Redirect ¶
func (r *RouteBuilder) Redirect(code int, toURL string) *RouteBuilder
Redirect is used to redirect the request to toURL with 301, 302, 307 or 308.
func (*RouteBuilder) Remove ¶
func (r *RouteBuilder) Remove(method string) *RouteBuilder
Remove removes the route.
If the method is "", it will remove all the routes associated with the path.
func (*RouteBuilder) RemoveAny ¶
func (r *RouteBuilder) RemoveAny() *RouteBuilder
RemoveAny is equal to r.Remove("").
func (*RouteBuilder) RemoveCONNECT ¶
func (r *RouteBuilder) RemoveCONNECT() *RouteBuilder
RemoveCONNECT is equal to r.Remove(http.MethodConnect).
func (*RouteBuilder) RemoveDELETE ¶
func (r *RouteBuilder) RemoveDELETE() *RouteBuilder
RemoveDELETE is equal to r.Remove(http.MethodDelete).
func (*RouteBuilder) RemoveGET ¶
func (r *RouteBuilder) RemoveGET() *RouteBuilder
RemoveGET is equal to r.Remove(http.MethodGet).
func (*RouteBuilder) RemoveHEAD ¶
func (r *RouteBuilder) RemoveHEAD() *RouteBuilder
RemoveHEAD is equal to r.Remove(http.MethodHead).
func (*RouteBuilder) RemoveOPTIONS ¶
func (r *RouteBuilder) RemoveOPTIONS() *RouteBuilder
RemoveOPTIONS is equal to r.Remove(http.MethodOptions).
func (*RouteBuilder) RemovePATCH ¶
func (r *RouteBuilder) RemovePATCH() *RouteBuilder
RemovePATCH is equal to r.Remove(http.MethodPatch).
func (*RouteBuilder) RemovePOST ¶
func (r *RouteBuilder) RemovePOST() *RouteBuilder
RemovePOST is equal to r.Remove(http.MethodPost).
func (*RouteBuilder) RemovePUT ¶
func (r *RouteBuilder) RemovePUT() *RouteBuilder
RemovePUT is equal to r.Remove(http.MethodPut).
func (*RouteBuilder) RemoveTRACE ¶
func (r *RouteBuilder) RemoveTRACE() *RouteBuilder
RemoveTRACE is equal to r.Remove(http.MethodTrace).
func (*RouteBuilder) ResetMiddlewares ¶
func (r *RouteBuilder) ResetMiddlewares(ms ...Middleware) *RouteBuilder
ResetMiddlewares resets the middlewares to ms.
func (*RouteBuilder) Routes ¶
func (r *RouteBuilder) Routes(handler Handler, methods ...string) []Route
Routes builds and returns the routes.
func (*RouteBuilder) Ship ¶
func (r *RouteBuilder) Ship() *Ship
Ship returns the ship that the current route is associated with.
func (*RouteBuilder) Static ¶
func (r *RouteBuilder) Static(dirpath string) *RouteBuilder
Static is the same as StaticFS, but listing the files for a directory.
func (*RouteBuilder) StaticFS ¶
func (r *RouteBuilder) StaticFS(fs http.FileSystem) *RouteBuilder
StaticFS registers a route to serve a static filesystem.
func (*RouteBuilder) TRACE ¶
func (r *RouteBuilder) TRACE(handler Handler) *RouteBuilder
TRACE is the short for r.Method(handler, "TRACE").
func (*RouteBuilder) Use ¶
func (r *RouteBuilder) Use(middlewares ...Middleware) *RouteBuilder
Use appends some middlwares.
type RouteError ¶
RouteError represents a route error when adding a route.
func (RouteError) Error ¶
func (re RouteError) Error() string
type RouteGroupBuilder ¶
type RouteGroupBuilder struct {
// contains filtered or unexported fields
}
RouteGroupBuilder is a route group to build a set of routes.
func (*RouteGroupBuilder) AddRoutes ¶
func (g *RouteGroupBuilder) AddRoutes(routes ...Route) *RouteGroupBuilder
AddRoutes registers a set of the routes.
It will panic with it if there is an error when adding the routes.
func (*RouteGroupBuilder) Clone ¶
func (g *RouteGroupBuilder) Clone() *RouteGroupBuilder
Clone clones itself and returns a new one.
func (*RouteGroupBuilder) Data ¶
func (g *RouteGroupBuilder) Data(data interface{}) *RouteGroupBuilder
Data sets the context data.
func (*RouteGroupBuilder) DelRoutes ¶
func (g *RouteGroupBuilder) DelRoutes(routes ...Route) *RouteGroupBuilder
DelRoutes deletes a set of the registered routes.
It will panic with it if there is an error when deleting the routes.
func (*RouteGroupBuilder) Group ¶
func (g *RouteGroupBuilder) Group(prefix string, middlewares ...Middleware) *RouteGroupBuilder
Group returns a new route sub-group.
If the prefix does not start with "/", it will add "/" as the prefix.
func (*RouteGroupBuilder) ResetMiddlewares ¶
func (g *RouteGroupBuilder) ResetMiddlewares(ms ...Middleware) *RouteGroupBuilder
ResetMiddlewares resets the middlewares of the group to ms.
func (*RouteGroupBuilder) Route ¶
func (g *RouteGroupBuilder) Route(path string) *RouteBuilder
Route returns a new route, which is used to build and register the route.
You should call Method() or its short method to register it.
func (*RouteGroupBuilder) Ship ¶
func (g *RouteGroupBuilder) Ship() *Ship
Ship returns the ship that the current group belongs to.
func (*RouteGroupBuilder) Use ¶
func (g *RouteGroupBuilder) Use(middlewares ...Middleware) *RouteGroupBuilder
Use appends some middlwares into the group.
type Runner ¶
type Runner struct { Name string Logger Logger Server *http.Server Signals []os.Signal ConnState func(net.Conn, http.ConnState) // contains filtered or unexported fields }
Runner is a HTTP Server runner.
func NewRunner ¶
NewRunner returns a new Runner.
If the handler has implemented the interface { GetName() string }, it will set the name to handler.GetName().
If the handler has implemented the interface { GetLogger() Logger }, it will set the logger to handler.GetLogger().
func (*Runner) RegisterOnShutdown ¶
func (r *Runner) RegisterOnShutdown(functions ...func())
RegisterOnShutdown registers some shutdown functions to run when the http server is shut down.
type Session ¶
type Session interface { // If the session id does not exist, it should return (nil, nil). GetSession(id string) (value interface{}, err error) SetSession(id string, value interface{}) error DelSession(id string) error }
Session represents an interface about the session.
func NewMemorySession ¶
func NewMemorySession() Session
NewMemorySession return a Session implementation based on the memory.
type Ship ¶
type Ship struct { // Name is the name of the ship. // // Default: "" Name string // Prefix is the default prefix of the paths of all the routes. // // Default: "" Prefix string // The initialization capacity of Context.Data. // // Default: 0 CtxDataInitCap int // The maximum number of the url paramters of the route. // // Default: 4 URLParamMaxNum int // The maximum number of the middlewares. // // Default: 256 MiddlewareMaxNum int // Router is the route manager to manage all the routes. // // Default: echo.NewRouter(&echo.Config{RemoveTrailingSlash: true}) Router Router // The default handler when not finding the route. // // Default: NotFoundHandler() NotFound Handler // Filter the route if returning true when registering and unregistering it. // // Default: nil RouteFilter func(Route) bool // Modify the route before registering and unregistering it. // // Default: nil RouteModifier func(Route) Route // HandleError is used to handle the error at last // if the handler or middleware returns an error. // // Default: respond the error to the client if not responding. HandleError func(c *Context, err error) // Context Settings. Session Session // Default: NewMemorySession() Logger Logger // Default: NewLoggerFromWriter(os.Stderr, "") Binder Binder // Default: nil Renderer Renderer // Default: nil Validator Validator // Default: nil Defaulter Defaulter // Default: SetStructFieldToDefault BindQuery func(dst interface{}, src url.Values) error // Default: BindURLValues(dst, src, "query") Responder func(c *Context, args ...interface{}) error // Default: nil // contains filtered or unexported fields }
Ship is an app to be used to manage the router.
func Default ¶
func Default() *Ship
Default returns a new ship with MuxBinder and MuxRenderer as the binder and renderer.
func (*Ship) AcquireBuffer ¶
AcquireBuffer gets a Buffer from the pool.
func (*Ship) AcquireContext ¶
AcquireContext gets a Context from the pool.
func (*Ship) AddRoutes ¶
AddRoutes registers a set of the routes.
It will panic with it if there is an error when adding the routes.
func (*Ship) Clone ¶
Clone clones itself to a new one with the new name and the new router.
If router is nil, create a new default one automatically.
func (*Ship) DelRoute ¶
DelRoute deletes the registered route, which only uses "Path" and "Method", and others are ignored.
If Method is empty, deletes all the routes associated with the path.
If the route does not exist, do nothing and return nil.
func (*Ship) DelRoutes ¶
DelRoutes deletes a set of the registered routes.
It will panic with it if there is an error when deleting the routes.
func (*Ship) Group ¶
func (s *Ship) Group(prefix string) *RouteGroupBuilder
Group returns a new route sub-group with the group prefix.
If the prefix does not start with "/", it will add "/" as the prefix.
func (*Ship) HandleRequest ¶
HandleRequest is the same as ServeHTTP, but handles the request with the Context.
func (*Ship) Pre ¶
func (s *Ship) Pre(middlewares ...Middleware)
Pre registers the pre-middlewares, which are executed before finding the route.
func (*Ship) ReleaseBuffer ¶
ReleaseBuffer puts a Buffer into the pool.
func (*Ship) ReleaseContext ¶
ReleaseContext puts a Context into the pool.
func (*Ship) ResetMiddlewares ¶
func (s *Ship) ResetMiddlewares(middlewares ...Middleware)
ResetMiddlewares resets the global middlewares to mdws.
func (*Ship) ResetPreMiddlewares ¶
func (s *Ship) ResetPreMiddlewares(middlewares ...Middleware)
ResetPreMiddlewares resets the global pre-middlewares to mdws.
func (*Ship) Route ¶
func (s *Ship) Route(path string) *RouteBuilder
Route returns a new route builder.
func (*Ship) ServeHTTP ¶
func (s *Ship) ServeHTTP(resp http.ResponseWriter, req *http.Request)
ServeHTTP implements the interface http.Handler.
func (*Ship) SetBufferSize ¶
SetBufferSize resets the size of the buffer. The default is 2048.
func (*Ship) Use ¶
func (s *Ship) Use(middlewares ...Middleware)
Use registers the global middlewares, which must be registered before adding the routes using these middlewares.
type Validator ¶
type Validator interface {
Validate(data interface{}) error
}
Validator is used to validate the data is valid or not.
func NothingValidator ¶
func NothingValidator() Validator
NothingValidator returns a Validator that does nothing.
type ValidatorFunc ¶
type ValidatorFunc func(data interface{}) error
ValidatorFunc is a function type implementing the interface Validator.
func (ValidatorFunc) Validate ¶
func (v ValidatorFunc) Validate(data interface{}) error
Validate implements the interface Validator.
Source Files
¶
- binder.go
- constant.go
- content_type.go
- context.go
- doc.go
- error.go
- go_1.13.go
- handler.go
- logger.go
- middleware.go
- mime.go
- render.go
- response.go
- runner.go
- session.go
- set_default.go
- ship.go
- ship_route.go
- ship_route_builder.go
- ship_route_group_builder.go
- utils.go
- validator.go
- vhost.go
- vhost_handler.go
- vhost_regexp.go
Directories
¶
Path | Synopsis |
---|---|
Package middleware is the collection of the middlewares.
|
Package middleware is the collection of the middlewares. |
render
|
|
Package router supplies a router interface ane some implementations.
|
Package router supplies a router interface ane some implementations. |
echo
Package echo supplies a customized Router implementation by referring to github.com/labstack/echo.
|
Package echo supplies a customized Router implementation by referring to github.com/labstack/echo. |