Documentation ¶
Index ¶
- Variables
- func DefaultGroup(prefix string, fn func(r *RouterGroup))
- func DefaultMiddleware(name string, fn func(app *Application))
- type Application
- func (app *Application) Address() string
- func (app *Application) AddressForLog() string
- func (app *Application) Cache() cache.Cache
- func (app *Application) CreateJSONRPC(path string) jsonrpc.Server[*Context]
- func (app *Application) Cron() cron.Cron
- func (app *Application) Debug() debug.Debug
- func (app *Application) Fallback(h HandlerFunc)
- func (app *Application) IsProd() bool
- func (app *Application) JobQueue() jobqueue.JobQueue
- func (app *Application) NotFound(h HandlerFunc)
- func (app *Application) Run(addr ...string) (err error)
- func (app *Application) Runtime() runtime.Runtime
- func (app *Application) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (app *Application) SetTemplates(dir string, fns ...template.FuncMap)
- type ApplicationConfig
- type Context
- func (ctx *Context) Accept() string
- func (ctx *Context) AcceptEncoding() string
- func (ctx *Context) AcceptHTML() bool
- func (ctx *Context) AcceptJSON() bool
- func (ctx *Context) AcceptLanguage() string
- func (ctx *Context) AddHeader(key string, value string)
- func (ctx *Context) Authorization() string
- func (ctx *Context) BasicAuth() (username string, password string, ok bool)
- func (ctx *Context) BearerToken() (token string, ok bool)
- func (ctx *Context) BindBody(obj interface{}) error
- func (ctx *Context) BindForm(obj interface{}) error
- func (ctx *Context) BindHeader(obj interface{}) error
- func (ctx *Context) BindJSON(obj interface{}) (err error)
- func (ctx *Context) BindParams(obj interface{}) error
- func (ctx *Context) BindQuery(obj interface{}) error
- func (ctx *Context) BindYAML(obj interface{}) (err error)
- func (ctx *Context) Bodies() map[string]any
- func (ctx *Context) Body() body.Body
- func (ctx *Context) BodyBytes() ([]byte, error)
- func (ctx *Context) Cache() cache.Cache
- func (ctx *Context) ClientIP() string
- func (ctx *Context) CloneBody() (body io.ReadCloser, err error)
- func (ctx *Context) Connection() string
- func (ctx *Context) Cookie() cookie.Cookie
- func (ctx *Context) Cookies() map[string]string
- func (ctx *Context) Cron() cron.Cron
- func (ctx *Context) Data(status int, contentType string, data []byte)
- func (ctx *Context) Debug() debug.Debug
- func (ctx *Context) Env() env.Env
- func (ctx *Context) Error(status int, message string)
- func (ctx *Context) Fail(err error, code int, message string, status ...int)
- func (ctx *Context) FailWithError(err HTTPError)
- func (ctx *Context) Fetch() *fetch.Fetch
- func (ctx *Context) File(key string) (multipart.File, *multipart.FileHeader)
- func (ctx *Context) Files() map[string]*multipart.FileHeader
- func (ctx *Context) Form() form.Form
- func (ctx *Context) Forms() *safe.Map
- func (ctx *Context) Get(key string) string
- func (ctx *Context) GetRawData() ([]byte, error)
- func (ctx *Context) HTML(status int, html string)
- func (ctx *Context) Header() http.Header
- func (ctx *Context) Headers() *safe.Map
- func (ctx *Context) Host() string
- func (ctx *Context) Hostname() string
- func (ctx *Context) IP() string
- func (ctx *Context) IsConnectionUpgrade() bool
- func (ctx *Context) JSON(status int, obj interface{})
- func (ctx *Context) JobQueue() jobqueue.JobQueue
- func (ctx *Context) Jwt() jwt.Jwt
- func (ctx *Context) Next()
- func (ctx *Context) Origin() string
- func (ctx *Context) Param() param.Param
- func (ctx *Context) Params() *safe.Map
- func (ctx *Context) Protocol() string
- func (ctx *Context) Proxy(target string, cfg *proxy.SingleTargetConfig)
- func (ctx *Context) Queries() *safe.Map
- func (ctx *Context) Query() query.Query
- func (ctx *Context) Redirect(url string, status ...int)
- func (ctx *Context) Referrer() string
- func (ctx *Context) Render(status int, name string, data interface{})
- func (ctx *Context) RequestID() string
- func (ctx *Context) SSE() sse.SSE
- func (ctx *Context) SaveFile(key, path string) error
- func (ctx *Context) Session() session.Session
- func (ctx *Context) Set(key string, value string)
- func (ctx *Context) SetHeader(key string, value string)
- func (ctx *Context) State() state.State
- func (ctx *Context) Status(status int)
- func (ctx *Context) StatusCode() int
- func (ctx *Context) Stream() io.ReadCloser
- func (ctx *Context) String(status int, format string, values ...interface{})
- func (ctx *Context) Success(result interface{})
- func (ctx *Context) Template(status int, name string, data interface{})
- func (ctx *Context) URL() string
- func (ctx *Context) Upgrade() string
- func (ctx *Context) User() user.User
- func (ctx *Context) UserAgent() string
- func (ctx *Context) Write(b []byte)
- func (ctx *Context) XForwardedFor() string
- func (ctx *Context) XForwardedHost() string
- func (ctx *Context) XForwardedPort() string
- func (ctx *Context) XForwardedProto() string
- func (ctx *Context) XRealIP() string
- type GroupFunc
- type H
- type HTTPError
- type HandlerFunc
- type Middleware
- type ResponseWriter
- type RouterGroup
- func (g *RouterGroup) Any(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Connect(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Delete(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Get(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Group(prefix string, cb ...GroupFunc) *RouterGroup
- func (g *RouterGroup) Head(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Options(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Patch(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Post(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Proxy(path, target string, options ...func(cfg *proxy.SingleTargetConfig)) *RouterGroup
- func (g *RouterGroup) Put(path string, handler ...HandlerFunc) *RouterGroup
- func (g *RouterGroup) Static(relativePath string, root string, options ...StaticOptions)
- func (g *RouterGroup) StaticFS(relativePath string, fs http.FileSystem)
- func (g *RouterGroup) Use(middlewares ...HandlerFunc)
- func (g *RouterGroup) WebSocket(path string, handler WsHandlerFunc) *RouterGroup
- func (g *RouterGroup) WebSocketGorilla(path string, handler WsGorillaHandlerFunc) *RouterGroup
- type StaticOptions
- type WsGorillaHandlerFunc
- type WsHandlerFunc
Constants ¶
This section is empty.
Variables ¶
var DefaultGroupsFns = map[string]func(r *RouterGroup){}
DefaultGroupsFns ...
var DefaultMiddlewares = map[string]func(app *Application){}
DefaultMiddlewares is the default global middleware
var DefaultSecretKey = random.String(16)
DefaultSecretKey uses for session encryption and decryption.
var DefaultSessionMaxAge = 1 * 24 * time.Hour
DefaultSessionMaxAge is the default session max age.
var Version = "1.10.4"
Version is the current version of the package.
Functions ¶
func DefaultGroup ¶ added in v1.0.16
func DefaultGroup(prefix string, fn func(r *RouterGroup))
DefaultGroup ...
func DefaultMiddleware ¶ added in v1.0.16
func DefaultMiddleware(name string, fn func(app *Application))
DefaultMiddleware ...
Types ¶
type Application ¶
type Application struct { *RouterGroup // Env env.Env Logger *logger.Logger // Config ApplicationConfig // contains filtered or unexported fields }
Application is the handler for all requests.
func (*Application) AddressForLog ¶ added in v1.7.12
func (app *Application) AddressForLog() string
AddressForLog ...
func (*Application) CreateJSONRPC ¶ added in v1.2.0
func (app *Application) CreateJSONRPC(path string) jsonrpc.Server[*Context]
CreateJSONRPC creates a new CreateJSONRPC handler.
func (*Application) Fallback ¶
func (app *Application) Fallback(h HandlerFunc)
Fallback is the default handler for all requests.
func (*Application) IsProd ¶ added in v1.1.0
func (app *Application) IsProd() bool
IsProd returns true if the app is in production mode.
func (*Application) JobQueue ¶ added in v1.9.9
func (app *Application) JobQueue() jobqueue.JobQueue
JobQueue ...
func (*Application) NotFound ¶
func (app *Application) NotFound(h HandlerFunc)
NotFound defines the 404 handler, replaced of built in not found handler.
func (*Application) Run ¶
func (app *Application) Run(addr ...string) (err error)
Run defines the method to start the server Example:
IP: default(http://0.0.0.0:8080): Run(":8080") port(http://0.0.0.0:8888): Run(":8888") host+port(http://127.0.0.1:8888): Run("127.0.0.1:8888") HTTP: scheme://host+port(http://127.0.0.1:8888): Run("http://127.0.0.1:8888") Unix Domain Socket: /tmp/xxx.sock: Run("unix:///tmp/xxx.sock")
func (*Application) Runtime ¶ added in v1.10.3
func (app *Application) Runtime() runtime.Runtime
Runtime ...
func (*Application) ServeHTTP ¶
func (app *Application) ServeHTTP(w http.ResponseWriter, req *http.Request)
func (*Application) SetTemplates ¶
func (app *Application) SetTemplates(dir string, fns ...template.FuncMap)
SetTemplates set the template
type ApplicationConfig ¶ added in v1.10.0
type ApplicationConfig struct { Protocol string Host string Port int // NetworkType string UnixDomainSocket string // TLS // TLS Certificate TLSCertFile string // TLS Private Key TLSKeyFile string // TLS Ca Certificate TLSCaCertFile string // LogLevel string `config:"log_level"` // SecretKey string `config:"secret_key"` // Session session.Config `config:"session"` // Cache cache.Config `config:"cache"` }
ApplicationConfig defines the config of zoox.Application.
type Context ¶
type Context struct { // origin objects Writer ResponseWriter Request *http.Request // request Method string Path string // App *Application // Logger *logger.Logger // contains filtered or unexported fields }
Context is the request context
func (*Context) AcceptEncoding ¶ added in v1.6.12
AcceptEncoding returns the request accept header.
func (*Context) AcceptHTML ¶ added in v1.3.12
AcceptHTML returns true if the request accepts html.
func (*Context) AcceptJSON ¶ added in v1.0.19
AcceptJSON returns true if the request accepts json.
func (*Context) AcceptLanguage ¶ added in v1.6.12
AcceptLanguage returns the request accept header.
func (*Context) Authorization ¶ added in v1.3.16
Authorization returns the authorization header for auth.
func (*Context) BasicAuth ¶ added in v1.0.1
BasicAuth returns the user/password pair for Basic Authentication.
func (*Context) BearerToken ¶ added in v1.3.16
BearerToken returns the token for bearer authentication.
func (*Context) BindHeader ¶ added in v1.0.1
BindHeader binds the header into the given struct.
func (*Context) BindParams ¶ added in v1.0.1
BindParams binds the params into the given struct.
func (*Context) CloneBody ¶ added in v1.8.1
func (ctx *Context) CloneBody() (body io.ReadCloser, err error)
CloneBody clones the body of the request, should be used carefully.
func (*Context) Connection ¶ added in v1.7.14
Connection return the request connection header.
func (*Context) Data ¶ added in v1.2.19
Data writes some data into the body stream and updates the HTTP code. Align to gin framework.
func (*Context) Error ¶
Error writes the given error to the response. Use for system errors
- Internal server error
- Not found
func (*Context) Fail ¶
Fail writes the given error with code-message-result specification to the response.
func (*Context) FailWithError ¶ added in v1.1.0
FailWithError writes the given error with code-message-result specification to the response.
func (*Context) Files ¶ added in v1.0.1
func (ctx *Context) Files() map[string]*multipart.FileHeader
Files gets all files.
func (*Context) GetRawData ¶ added in v1.2.19
GetRawData returns stream data. Align to gin framework.
func (*Context) Hostname ¶ added in v1.5.7
Hostname gets the hostname from HTTP Header. format: `hostname`
func (*Context) IP ¶ added in v1.0.4
IP gets the ip from X-Forwarded-For or X-Real-IP or RemoteIP. RemoteIP parses the IP from Request.RemoteAddr, normializes and returns the IP (without the port).
func (*Context) IsConnectionUpgrade ¶ added in v1.7.14
IsConnectionUpgrade checks if the connection upgrade.
func (*Context) Next ¶
func (ctx *Context) Next()
Next runs the next handler in the middleware stack
func (*Context) Proxy ¶ added in v1.8.0
func (ctx *Context) Proxy(target string, cfg *proxy.SingleTargetConfig)
Proxy customize the request to proxy the backend services.
func (*Context) StatusCode ¶
StatusCode returns the HTTP response status code.
func (*Context) Stream ¶ added in v1.0.1
func (ctx *Context) Stream() io.ReadCloser
Stream get the body stream.
func (*Context) Success ¶
func (ctx *Context) Success(result interface{})
Success writes the given data with code-message-result specification to the response.
func (*Context) Template ¶ added in v1.6.7
Template renders the given template with the given data and writes the result
func (*Context) XForwardedFor ¶ added in v1.8.15
XForwardedFor return the request x-forwarded-for header.
func (*Context) XForwardedHost ¶ added in v1.8.15
XForwardedHost return the request x-forwarded-host header.
func (*Context) XForwardedPort ¶ added in v1.8.15
XForwardedPort return the request x-forwarded-port header.
func (*Context) XForwardedProto ¶ added in v1.8.15
XForwardedProto return the request x-forwarded-proto header.
type GroupFunc ¶ added in v1.8.2
type GroupFunc func(group *RouterGroup)
GroupFunc defines the group handler used by zoox
type HandlerFunc ¶
type HandlerFunc func(ctx *Context)
HandlerFunc defines the request handler used by zoox
func NotFound ¶
func NotFound() HandlerFunc
NotFound returns a HandlerFunc that replies with a 404 not found
func WrapH ¶
func WrapH(handler http.Handler) HandlerFunc
WrapH wraps a http.Handler to a HandlerFunc
type Middleware ¶ added in v1.0.20
type Middleware = HandlerFunc
Middleware defines the signature of the middleware function.
type ResponseWriter ¶ added in v1.0.12
type ResponseWriter interface { http.ResponseWriter http.Hijacker http.CloseNotifier http.Flusher // Status returns the HTTP response status code of the current request. Status() int // Size returns the number of bytes already written into the response http body. // See Written() Size() int // WriteString writes the string into the response body. WriteString(string) (int, error) // Written returns true if the response body was already written. Written() bool // Pusher get the http.Pusher for server push Pusher() http.Pusher // WriteHeaderNow forces to write the http header (status code + headers). WriteHeaderNow() }
ResponseWriter ...
type RouterGroup ¶
type RouterGroup struct {
// contains filtered or unexported fields
}
RouterGroup is a group of routes.
func (*RouterGroup) Any ¶ added in v1.0.3
func (g *RouterGroup) Any(path string, handler ...HandlerFunc) *RouterGroup
Any defines all request methods (anyMethods)
func (*RouterGroup) Connect ¶ added in v1.5.5
func (g *RouterGroup) Connect(path string, handler ...HandlerFunc) *RouterGroup
Connect defines the method to add CONNECT request
func (*RouterGroup) Delete ¶
func (g *RouterGroup) Delete(path string, handler ...HandlerFunc) *RouterGroup
Delete defines the method to add DELETE request
func (*RouterGroup) Get ¶
func (g *RouterGroup) Get(path string, handler ...HandlerFunc) *RouterGroup
Get defines the method to add GET request
func (*RouterGroup) Group ¶
func (g *RouterGroup) Group(prefix string, cb ...GroupFunc) *RouterGroup
Group defines a new router group
func (*RouterGroup) Head ¶
func (g *RouterGroup) Head(path string, handler ...HandlerFunc) *RouterGroup
Head defines the method to add HEAD request
func (*RouterGroup) Options ¶ added in v1.0.3
func (g *RouterGroup) Options(path string, handler ...HandlerFunc) *RouterGroup
Options defines the method to add OPTIONS request
func (*RouterGroup) Patch ¶
func (g *RouterGroup) Patch(path string, handler ...HandlerFunc) *RouterGroup
Patch defines the method to add PATCH request
func (*RouterGroup) Post ¶
func (g *RouterGroup) Post(path string, handler ...HandlerFunc) *RouterGroup
Post defines the method to add POST request
func (*RouterGroup) Proxy ¶ added in v1.8.0
func (g *RouterGroup) Proxy(path, target string, options ...func(cfg *proxy.SingleTargetConfig)) *RouterGroup
Proxy defines the method to proxy the request to the backend service.
Example:
app.Proxy("/api/v1/tasks/(.*)", "http://zmicro.services.tasks:8080", &proxy.SingleTargetConfig{ Rewrites: rewriter.Rewriters{ {From: "/api/v1/tasks/(.*)", To: "/$1"}, }, }) app.Proxy("*", "https://httpbin.org")
func (*RouterGroup) Put ¶
func (g *RouterGroup) Put(path string, handler ...HandlerFunc) *RouterGroup
Put defines the method to add PUT request
func (*RouterGroup) Static ¶
func (g *RouterGroup) Static(relativePath string, root string, options ...StaticOptions)
Static defines the method to serve static files
func (*RouterGroup) StaticFS ¶ added in v1.0.15
func (g *RouterGroup) StaticFS(relativePath string, fs http.FileSystem)
StaticFS defines the method to serve static files
func (*RouterGroup) Use ¶
func (g *RouterGroup) Use(middlewares ...HandlerFunc)
Use adds a middleware to the group
func (*RouterGroup) WebSocket ¶ added in v1.0.28
func (g *RouterGroup) WebSocket(path string, handler WsHandlerFunc) *RouterGroup
WebSocket defines the method to add websocket route
func (*RouterGroup) WebSocketGorilla ¶ added in v1.6.7
func (g *RouterGroup) WebSocketGorilla(path string, handler WsGorillaHandlerFunc) *RouterGroup
WebSocketGorilla defines the method to add websocket route
type StaticOptions ¶
type StaticOptions struct { Gzip bool Md5 bool CacheControl string MaxAge time.Duration Index bool Suffix string }
StaticOptions is the options for static method
type WsGorillaHandlerFunc ¶ added in v1.6.7
type WsGorillaHandlerFunc func(ctx *Context, client *websocket.GorillaConn)
WsGorillaHandlerFunc defines the websocket handler used by zoox
type WsHandlerFunc ¶ added in v1.0.28
WsHandlerFunc defines the websocket handler used by zoox