Documentation
¶
Overview ¶
Package zhttp is the HTTP layer the handlers are written against. The server runs on gin by default, `-tags fiber` switches it to fiber; both run the same handler chains with gin's middleware semantics.
Index ¶
- Constants
- Variables
- type Context
- type Engine
- func (e *Engine) Close() error
- func (e *Engine) DELETE(path string, handlers ...HandlerFunc)
- func (e *Engine) GET(path string, handlers ...HandlerFunc)
- func (e *Engine) HEAD(path string, handlers ...HandlerFunc)
- func (e *Engine) Handle(method, path string, handlers ...HandlerFunc)
- func (e *Engine) NoRoute(handlers ...HandlerFunc)
- func (e *Engine) POST(path string, handlers ...HandlerFunc)
- func (e *Engine) PUT(path string, handlers ...HandlerFunc)
- func (e *Engine) Serve(ln net.Listener) error
- func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (e *Engine) Shutdown(ctx context.Context) error
- func (e *Engine) Use(handlers ...HandlerFunc)
- type H
- type HTTPContext
- func (s *HTTPContext) Abort()
- func (c *HTTPContext) AbortWithStatus(code int)
- func (c *HTTPContext) AbortWithStatusJSON(code int, obj interface{})
- func (c *HTTPContext) BasicAuth() (string, string, bool)
- func (c *HTTPContext) Body() io.ReadCloser
- func (c *HTTPContext) Context() context.Context
- func (c *HTTPContext) Data(code int, contentType string, data []byte)
- func (c *HTTPContext) DefaultQuery(key, def string) string
- func (s *HTTPContext) FullPath() string
- func (s *HTTPContext) Get(key string) (interface{}, bool)
- func (c *HTTPContext) GetHeader(key string) string
- func (c *HTTPContext) GetQuery(key string) (string, bool)
- func (c *HTTPContext) Header(key, value string)
- func (c *HTTPContext) IndentedJSON(code int, obj interface{})
- func (s *HTTPContext) IsAborted() bool
- func (c *HTTPContext) JSON(code int, obj interface{})
- func (c *HTTPContext) Method() string
- func (c *HTTPContext) Next()
- func (c *HTTPContext) NextBuffered() func() error
- func (s *HTTPContext) Param(key string) string
- func (c *HTTPContext) Path() string
- func (c *HTTPContext) Query(key string) string
- func (c *HTTPContext) Redirect(code int, location string)
- func (c *HTTPContext) RequestURI() string
- func (c *HTTPContext) ResponseSize() int
- func (c *HTTPContext) ResponseStatus() int
- func (c *HTTPContext) ServeHTTP(h http.Handler)
- func (s *HTTPContext) Set(key string, value interface{})
- func (c *HTTPContext) SetBody(body []byte)
- func (s *HTTPContext) SetParam(key, value string)
- func (c *HTTPContext) Status(code int)
- type HandlerFunc
- type Param
Constants ¶
const Backend = "gin"
Backend names the server the binary was built with.
Variables ¶
var TLSNextProtos = []string{"h2", "http/1.1"}
TLSNextProtos are the ALPN protocols the server speaks.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context interface {
// Method is the request method.
Method() string
// Path is the decoded URL path.
Path() string
// RequestURI is the unmodified request target, path and query.
RequestURI() string
// FullPath is the matched route pattern, empty when no route matched.
FullPath() string
// Param returns a route parameter.
Param(key string) string
// SetParam overrides a route parameter for the rest of the chain.
SetParam(key, value string)
// Query returns a query string value.
Query(key string) string
// DefaultQuery returns a query string value or def when it is absent.
DefaultQuery(key, def string) string
// GetQuery returns a query string value and whether it is present.
GetQuery(key string) (string, bool)
// GetHeader returns a request header.
GetHeader(key string) string
// BasicAuth returns the credentials of the Authorization header.
BasicAuth() (username, password string, ok bool)
// Body returns the request body, never nil.
Body() io.ReadCloser
// SetBody replaces the request body for the rest of the chain.
SetBody(body []byte)
// Context returns the request context.
Context() context.Context
// Set stores a value for the rest of the chain.
Set(key string, value interface{})
// Get returns a value stored with Set.
Get(key string) (interface{}, bool)
// Header sets a response header.
Header(key, value string)
// Status sets the response status code.
Status(code int)
// JSON writes obj as JSON.
JSON(code int, obj interface{})
// IndentedJSON writes obj as indented JSON.
IndentedJSON(code int, obj interface{})
// Data writes raw bytes.
Data(code int, contentType string, data []byte)
// Redirect sends the client to location.
Redirect(code int, location string)
// ResponseStatus is the status code written so far.
ResponseStatus() int
// ResponseSize is the number of body bytes written so far.
ResponseSize() int
// ServeHTTP lets a net/http handler answer the request.
ServeHTTP(h http.Handler)
// Next runs the remaining handlers of the chain.
Next()
// NextBuffered runs the remaining handlers holding their response back.
// Calling flush sends it, otherwise it is dropped and the caller writes
// its own response.
NextBuffered() (flush func() error)
// Abort stops the chain after the current handler.
Abort()
// IsAborted reports whether Abort was called.
IsAborted() bool
// AbortWithStatus aborts and writes the status code.
AbortWithStatus(code int)
// AbortWithStatusJSON aborts and writes obj as JSON.
AbortWithStatusJSON(code int, obj interface{})
}
Context is one request travelling through a handler chain.
A handler that returns without calling Next or Abort lets the chain continue, Next runs the rest of the chain before returning, Abort stops it.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the router. Like gin, a route captures the middlewares added with Use before it, while the NoRoute chain sees all of them.
func (*Engine) DELETE ¶
func (e *Engine) DELETE(path string, handlers ...HandlerFunc)
func (*Engine) GET ¶
func (e *Engine) GET(path string, handlers ...HandlerFunc)
func (*Engine) HEAD ¶
func (e *Engine) HEAD(path string, handlers ...HandlerFunc)
func (*Engine) Handle ¶
func (e *Engine) Handle(method, path string, handlers ...HandlerFunc)
Handle registers a route.
func (*Engine) NoRoute ¶
func (e *Engine) NoRoute(handlers ...HandlerFunc)
NoRoute sets the handlers for requests no route matched.
func (*Engine) POST ¶
func (e *Engine) POST(path string, handlers ...HandlerFunc)
func (*Engine) PUT ¶
func (e *Engine) PUT(path string, handlers ...HandlerFunc)
func (*Engine) Serve ¶
Serve accepts connections on ln until Shutdown or Close, which make it return nil.
func (*Engine) ServeHTTP ¶
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP makes the engine a net/http handler, used by tests.
type HTTPContext ¶
HTTPContext is a Context over net/http. The gin backend serves requests with it and handler tests build one with NewHTTPContext.
func NewHTTPContext ¶
func NewHTTPContext(w http.ResponseWriter, r *http.Request) *HTTPContext
NewHTTPContext returns a context without handlers for w and r.
func (*HTTPContext) AbortWithStatus ¶
func (c *HTTPContext) AbortWithStatus(code int)
func (*HTTPContext) AbortWithStatusJSON ¶
func (c *HTTPContext) AbortWithStatusJSON(code int, obj interface{})
func (*HTTPContext) Body ¶
func (c *HTTPContext) Body() io.ReadCloser
func (*HTTPContext) Context ¶
func (c *HTTPContext) Context() context.Context
func (*HTTPContext) DefaultQuery ¶
func (c *HTTPContext) DefaultQuery(key, def string) string
func (*HTTPContext) GetHeader ¶
func (c *HTTPContext) GetHeader(key string) string
func (*HTTPContext) Header ¶
func (c *HTTPContext) Header(key, value string)
func (*HTTPContext) IndentedJSON ¶
func (c *HTTPContext) IndentedJSON(code int, obj interface{})
func (*HTTPContext) JSON ¶
func (c *HTTPContext) JSON(code int, obj interface{})
func (*HTTPContext) Method ¶
func (c *HTTPContext) Method() string
func (*HTTPContext) Next ¶
func (c *HTTPContext) Next()
func (*HTTPContext) NextBuffered ¶
func (c *HTTPContext) NextBuffered() func() error
func (*HTTPContext) Path ¶
func (c *HTTPContext) Path() string
func (*HTTPContext) Query ¶
func (c *HTTPContext) Query(key string) string
func (*HTTPContext) Redirect ¶
func (c *HTTPContext) Redirect(code int, location string)
func (*HTTPContext) RequestURI ¶
func (c *HTTPContext) RequestURI() string
func (*HTTPContext) ResponseSize ¶
func (c *HTTPContext) ResponseSize() int
func (*HTTPContext) ResponseStatus ¶
func (c *HTTPContext) ResponseStatus() int
func (*HTTPContext) ServeHTTP ¶
func (c *HTTPContext) ServeHTTP(h http.Handler)
func (*HTTPContext) SetBody ¶
func (c *HTTPContext) SetBody(body []byte)
func (*HTTPContext) Status ¶
func (c *HTTPContext) Status(code int)
type HandlerFunc ¶
type HandlerFunc func(Context)
HandlerFunc handles a request or acts as a middleware.