Documentation ¶
Overview ¶
Package znet provides web service
package main import ( "github.com/sohaha/zlsgo/znet" ) func main(){ r := znet.New() r.SetMode(znet.DebugMode) r.GET("/", func(c znet.Context) { c.String(200, "hello world") }) znet.Run() }
Index ¶
- Constants
- Variables
- func ClientIP(r *http.Request) (ip string)
- func ClientPublicIP(r *http.Request) string
- func CloseHotRestartFileMd5()deprecated
- func GetIPv(s string) int
- func IPToLong(ip string) (i uint, err error)
- func InNetwork(ip, network string) bool
- func IsIP(ip string) bool
- func IsLocalAddrIP(ip string) bool
- func IsLocalIP(ip net.IP) bool
- func JSONRPC(rcvr map[string]interface{}, opts ...func(o *JSONRPCOption)) func(c *Context)
- func LongToIP(i uint) (string, error)
- func LongToNetIP(i uint) (ip net.IP, err error)
- func LongToNetIPv6(i *big.Int) (ip net.IP, err error)
- func MultiplePort(ports []int, change bool) (int, error)
- func NetIPToLong(ip net.IP) (i uint, err error)
- func NetIPv6ToLong(ip net.IP) (*big.Int, error)
- func OnShutdown(done func())
- func Port(port int, change bool) (newPort int, err error)
- func RemoteIP(r *http.Request) string
- func Run(cb ...func(name, addr string))
- func RunContext(ctx context.Context, cb ...func(name, addr string))
- func Shutdown()
- func WrapFirstMiddleware(fn Handler) firstHandler
- type ApiData
- type Context
- func (c *Context) Abort(code ...int32)
- func (c *Context) ApiJSON(code int32, msg string, data interface{})
- func (c *Context) Bind(obj interface{}) (err error)
- func (c *Context) BindForm(obj interface{}) error
- func (c *Context) BindJSON(obj interface{}) error
- func (c *Context) BindQuery(obj interface{}) (err error)
- func (c *Context) BindValid(obj interface{}, v map[string]zvalid.Engine) error
- func (c *Context) Byte(code int32, value []byte)
- func (c *Context) CompletionLink(link string) string
- func (c *Context) ContentType(contentText ...string) string
- func (c *Context) DefaultFormOrQuery(key string, def string) string
- func (c *Context) DefaultPostForm(key, def string) string
- func (c *Context) DefaultQuery(key string, def string) string
- func (c *Context) File(path string)
- func (c *Context) FileAttachment(filepath, filename string)
- func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
- func (c *Context) FormFiles(name string) (files []*multipart.FileHeader, err error)
- func (c *Context) GetAllParam() map[string]string
- func (c *Context) GetAllQuery() url.Values
- func (c *Context) GetAllQueryMaps() map[string]string
- func (c *Context) GetClientIP() (IP string)
- func (c *Context) GetCookie(name string) string
- func (c *Context) GetDataRaw() (string, error)
- func (c *Context) GetDataRawBytes() ([]byte, error)
- func (c *Context) GetHeader(key string) string
- func (c *Context) GetJSON(key string) *zjson.Res
- func (c *Context) GetJSONs() (json *zjson.Res, err error)
- func (c *Context) GetParam(key string) string
- func (c *Context) GetPostForm(key string) (string, bool)
- func (c *Context) GetPostFormAll() (value url.Values)
- func (c *Context) GetPostFormArray(key string) ([]string, bool)
- func (c *Context) GetPostFormMap(key string) (map[string]string, bool)
- func (c *Context) GetQuery(key string) (string, bool)
- func (c *Context) GetQueryArray(key string) ([]string, bool)
- func (c *Context) GetQueryMap(key string) (map[string]string, bool)
- func (c *Context) GetReferer() string
- func (c *Context) GetUserAgent() string
- func (c *Context) HTML(code int32, html string)
- func (c *Context) Host(full ...bool) string
- func (c *Context) Injector() zdi.Injector
- func (c *Context) IsAbort() bool
- func (c *Context) IsAjax() bool
- func (c *Context) IsSSE() bool
- func (c *Context) IsWebsocket() bool
- func (c *Context) JSON(code int32, values interface{})
- func (c *Context) MultipartForm() (*multipart.Form, error)
- func (c *Context) MustValue(key string, def ...interface{}) (value interface{})
- func (c *Context) Next() bool
- func (c *Context) ParseMultipartForm(maxMultipartMemory ...int64) error
- func (c *Context) PostFormMap(key string) map[string]string
- func (c *Context) PrevContent() *PrevData
- func (c *Context) QueryMap(key string) map[string]string
- func (c *Context) Redirect(link string, statusCode ...int32)
- func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dist string) error
- func (c *Context) SetContent(data *PrevData)deprecated
- func (c *Context) SetContentType(contentType string) *Context
- func (c *Context) SetCookie(name, value string, maxAge ...int)
- func (c *Context) SetHeader(key, value string)
- func (c *Context) SetStatus(code int32) *Context
- func (c *Context) Stream(step func(w io.Writer) bool) bool
- func (c *Context) String(code int32, format string, values ...interface{})
- func (c *Context) Template(code int32, name string, data interface{}, funcMap ...map[string]interface{})
- func (c *Context) Templates(code int32, templates []string, data interface{}, ...)
- func (c *Context) Valid(defRule zvalid.Engine, key string, name ...string) zvalid.Engine
- func (c *Context) ValidForm(defRule zvalid.Engine, key string, name ...string) zvalid.Engine
- func (c *Context) ValidJSON(defRule zvalid.Engine, key string, name ...string) zvalid.Engine
- func (c *Context) ValidParam(defRule zvalid.Engine, key string, name ...string) zvalid.Engine
- func (c *Context) ValidQuery(defRule zvalid.Engine, key string, name ...string) zvalid.Engine
- func (c *Context) ValidRule() zvalid.Engine
- func (c *Context) Value(key string, def ...interface{}) (value interface{}, ok bool)
- func (c *Context) WithValue(key string, value interface{}) *Context
- type Data
- type Engine
- func (e *Engine) AddAddr(addrString string, tlsConfig ...TlsCfg)
- func (e *Engine) Any(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) error
- func (e *Engine) CONNECT(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) CONNECTAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) Customize(method, path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) DELETE(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) DELETEAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) FindHandle(rw *Context, req *http.Request, requestURL string, applyMiddleware bool) (not bool)
- func (e *Engine) GET(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) GETAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) GenerateURL(method string, routeName string, params map[string]string) (string, error)
- func (e *Engine) GetMode() string
- func (e *Engine) GetTrees() map[string]*Tree
- func (e *Engine) Group(prefix string, groupHandle ...func(e *Engine)) (engine *Engine)
- func (e *Engine) HEAD(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) HEADAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) Handle(method string, path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) HandleNotFound(c *Context)
- func (e *Engine) Injector() zdi.TypeMapper
- func (e *Engine) IsDebug() bool
- func (e *Engine) LoadHTMLGlob(pattern string)
- func (e *Engine) Match(requestURL string, path string) bool
- func (e *Engine) NewContext(w http.ResponseWriter, req *http.Request) *Context
- func (e *Engine) NotFoundHandler(handler Handler)
- func (e *Engine) OPTIONS(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) OPTIONSAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) PATCH(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) PATCHAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) POST(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) POSTAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) PUT(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) PUTAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) PanicHandler(handler ErrHandlerFunc)deprecated
- func (e *Engine) PreHandler(preHandler Handler)
- func (e *Engine) Restart() error
- func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (e *Engine) SetAddr(addrString string, tlsConfig ...TlsCfg)
- func (e *Engine) SetCustomMethodField(field string)
- func (e *Engine) SetHTMLTemplate(t *template.Template)deprecated
- func (e *Engine) SetMode(value string)
- func (e *Engine) SetTemplate(v Template)
- func (e *Engine) SetTemplateFuncMap(funcMap template.FuncMap)deprecated
- func (e *Engine) SetTimeout(Timeout time.Duration, WriteTimeout ...time.Duration)
- func (e *Engine) StartUp() []*serverMap
- func (e *Engine) Static(relativePath, root string, moreHandler ...Handler)
- func (e *Engine) StaticFS(relativePath string, fs http.FileSystem, moreHandler ...Handler)
- func (e *Engine) StaticFile(relativePath, filepath string)
- func (e *Engine) TRACE(path string, action Handler, moreHandler ...Handler) *Engine
- func (e *Engine) TRACEAndName(path string, action Handler, routeName string) *Engine
- func (e *Engine) Use(middleware ...Handler)
- type ErrHandlerFunc
- type Handler
- type HandlerFunc
- type JSONRPCOption
- type MiddlewareFunc
- type MiddlewareType
- type Node
- type Parameters
- type PrevData
- type SSE
- type SSEOption
- type Template
- type TemplateOptions
- type TlsCfg
- type Tree
Constants ¶
const ( // DebugMode dev DebugMode = "dev" // ProdMode release ProdMode = "prod" // TestMode test TestMode = "test" // QuietMode quiet QuietMode = "quiet" )
Variables ¶
var ( RemoteIPHeaders = []string{"X-Forwarded-For", "X-Real-IP"} TrustedProxies = []string{"0.0.0.0/0"} LocalNetworks = []string{"127.0.0.0/8", "10.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "172.0.0.0/8", "192.168.0.0/16"} )
var ( // ContentTypePlain text ContentTypePlain = "text/plain; charset=utf-8" // ContentTypeHTML html ContentTypeHTML = "text/html; charset=utf-8" // ContentTypeJSON json ContentTypeJSON = "application/json; charset=utf-8" )
var ( // ErrGenerateParameters is returned when generating a route withRequestLog wrong parameters. ErrGenerateParameters = errors.New("params contains wrong parameters") // ErrNotFoundRoute is returned when generating a route that can not find route in tree. ErrNotFoundRoute = errors.New("cannot find route in tree") // ErrNotFoundMethod is returned when generating a route that can not find method in tree. ErrNotFoundMethod = errors.New("cannot find method in tree") // ErrPatternGrammar is returned when generating a route that pattern grammar error. ErrPatternGrammar = errors.New("pattern grammar error") )
var ( // Log Log Log = zlog.New(zlog.ColorTextWrap(zlog.ColorGreen, "[Z] ")) Cache = zcache.New("__ZNET__") // CloseHotRestart Close Hot Restart CloseHotRestart bool // BindStructDelimiter structure route delimiter BindStructDelimiter = "-" // BindStructSuffix structure route suffix BindStructSuffix = "" )
var Utils = utils{
ContextKey: contextKeyType{},
}
Functions ¶
func CloseHotRestartFileMd5
deprecated
added in
v0.1.26
func CloseHotRestartFileMd5()
Deprecated: If you need to verify if a program is trustworthy, please implement it yourself. CloseHotRestartFileMd5 CloseHotRestartFileMd5
func IsLocalAddrIP ¶ added in v0.1.42
IsLocalAddrIP IsLocalAddrIP
func JSONRPC ¶ added in v1.4.3
func JSONRPC(rcvr map[string]interface{}, opts ...func(o *JSONRPCOption)) func(c *Context)
func LongToNetIP ¶ added in v1.1.18
LongToNetIP LongToNetIP
func LongToNetIPv6 ¶ added in v1.1.18
LongToNetIPv6 LongToNetIPv6
func MultiplePort ¶ added in v1.1.8
MultiplePort Check if the multiple port is available, if not, then automatically get an available
func NetIPToLong ¶ added in v1.1.18
NetIPToLong NetIPToLong
func NetIPv6ToLong ¶ added in v1.1.18
NetIPv6ToLong NetIPv6ToLong
func Port ¶ added in v1.1.8
Port GetPort Check if the port is available, if not, then automatically get an available
func RunContext ¶ added in v1.7.3
func WrapFirstMiddleware ¶ added in v1.4.9
func WrapFirstMiddleware(fn Handler) firstHandler
WrapFirstMiddleware Wrapping a function in the first position of the middleware
Types ¶
type ApiData ¶ added in v0.1.36
type ApiData struct { Data interface{} `json:"data"` Msg string `json:"msg,omitempty"` Code int32 `json:"code" example:"200"` }
ApiData unified return api format
type Context ¶
type Context struct { Writer http.ResponseWriter Request *http.Request Engine *Engine Log *zlog.Logger // Deprecated: Please maintain your own cache Cache *zcache.Table // contains filtered or unexported fields }
Context context
func (*Context) CompletionLink ¶
CompletionLink Complete the link and add the current domain name if it is not linked
func (*Context) ContentType ¶ added in v0.0.19
ContentType returns the Content-Type header of the request
func (*Context) DefaultFormOrQuery ¶ added in v0.0.19
DefaultFormOrQuery Get Form Or Query
func (*Context) DefaultPostForm ¶
DefaultPostForm Get Form Or Default
func (*Context) DefaultQuery ¶
DefaultQuery Get Query Or Default
func (*Context) FileAttachment ¶ added in v1.6.4
func (*Context) FormFile ¶
func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
FormFile FormFile
func (*Context) FormFiles ¶ added in v1.4.3
func (c *Context) FormFiles(name string) (files []*multipart.FileHeader, err error)
FormFiles Multiple FormFile
func (*Context) GetAllParam ¶
GetAllParam Get the value of all param in the route
func (*Context) GetAllQuery ¶ added in v1.3.1
GetAllQuery Get All Queryst
func (*Context) GetAllQueryMaps ¶ added in v1.3.1
GetAllQueryMaps Get All Queryst Maps
func (*Context) GetClientIP ¶ added in v0.1.20
GetClientIP Client IP
func (*Context) GetDataRaw ¶ added in v0.0.19
GetDataRaw Get Raw Data
func (*Context) GetDataRawBytes ¶ added in v1.6.4
GetDataRawBytes Get Raw Data
func (*Context) GetPostForm ¶
GetPostForm Get PostForm
func (*Context) GetPostFormAll ¶ added in v0.0.19
GetPostFormAll Get PostForm All
func (*Context) GetPostFormArray ¶
GetPostFormArray Get Post FormArray
func (*Context) GetPostFormMap ¶
GetPostFormMap Get PostForm Map
func (*Context) GetQueryArray ¶
GetQueryArray Get Query Array
func (*Context) GetQueryMap ¶ added in v1.2.0
GetQueryMap Get Query Map
func (*Context) GetReferer ¶ added in v0.1.20
GetReferer request referer
func (*Context) GetUserAgent ¶ added in v0.1.20
GetUserAgent http request UserAgent
func (*Context) MultipartForm ¶
MultipartForm MultipartForm
func (*Context) Next ¶
Next middleware, if current middleware has been stopped, it will return false
func (*Context) ParseMultipartForm ¶ added in v1.3.1
func (*Context) PostFormMap ¶
PostFormMap PostForm Map
func (*Context) PrevContent ¶ added in v0.0.19
PrevContent current output content
func (*Context) SaveUploadedFile ¶
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dist string) error
SaveUploadedFile Save Uploaded File
func (*Context) SetContent
deprecated
added in
v0.1.21
func (*Context) SetContentType ¶ added in v0.0.26
func (*Context) Template ¶
func (c *Context) Template(code int32, name string, data interface{}, funcMap ...map[string]interface{})
Template export tpl
func (*Context) ValidParam ¶ added in v0.0.20
ValidParam get and verify routing parameters
func (*Context) ValidQuery ¶ added in v0.0.20
ValidQuery get and verify query
type Engine ¶
type Engine struct { Cache *zcache.Table Log *zlog.Logger BindTag string BindStructDelimiter string BindStructCase func(string) string BindStructSuffix string MaxMultipartMemory int64 ShowFavicon bool AllowQuerySemicolons bool // contains filtered or unexported fields }
Engine is a simple HTTP route multiplexer that parses a request path
func (*Engine) BindStruct ¶ added in v0.1.42
BindStruct Bind Struct
func (*Engine) CONNECTAndName ¶ added in v0.1.64
func (*Engine) DELETEAndName ¶
func (*Engine) FindHandle ¶ added in v0.0.19
func (*Engine) GETAndName ¶
func (*Engine) GenerateURL ¶
func (*Engine) HEADAndName ¶
func (*Engine) HandleNotFound ¶
func (*Engine) Injector ¶ added in v1.3.0
func (e *Engine) Injector() zdi.TypeMapper
Injector Call Injector
func (*Engine) LoadHTMLGlob ¶ added in v1.1.3
LoadHTMLGlob Load Glob HTML
func (*Engine) NewContext ¶ added in v0.1.34
func (*Engine) NotFoundHandler ¶ added in v0.0.26
func (*Engine) OPTIONSAndName ¶
func (*Engine) PATCHAndName ¶
func (*Engine) POSTAndName ¶
func (*Engine) PUTAndName ¶
func (*Engine) PanicHandler
deprecated
func (e *Engine) PanicHandler(handler ErrHandlerFunc)
Deprecated: please use znet.Recovery(func(c *Context, err error) {}) PanicHandler is used for handling panics
func (*Engine) PreHandler ¶ added in v0.1.53
func (*Engine) SetCustomMethodField ¶
SetCustomMethodField Set Custom Method Field
func (*Engine) SetHTMLTemplate
deprecated
added in
v1.1.10
func (*Engine) SetTemplate ¶ added in v1.4.8
func (*Engine) SetTemplateFuncMap
deprecated
added in
v1.1.3
func (*Engine) SetTimeout ¶
SetTimeout set Timeout
func (*Engine) StaticFS ¶
func (e *Engine) StaticFS(relativePath string, fs http.FileSystem, moreHandler ...Handler)
func (*Engine) StaticFile ¶
func (*Engine) TRACEAndName ¶ added in v0.1.64
type ErrHandlerFunc ¶ added in v1.3.0
ErrHandlerFunc ErrHandlerFunc
type Handler ¶ added in v1.3.0
type Handler interface{}
Handler handler func
func Recovery ¶ added in v0.1.37
func Recovery(handler ErrHandlerFunc) Handler
Recovery is a middleware that recovers from panics anywhere in the chain
func RewriteErrorHandler ¶ added in v1.3.0
func RewriteErrorHandler(handler ErrHandlerFunc) Handler
RewriteErrorHandler rewrite error handler
type JSONRPCOption ¶ added in v1.4.3
type MiddlewareFunc ¶
MiddlewareFunc Middleware Func
type MiddlewareType ¶
type MiddlewareType Handler
MiddlewareType is a public type that is used for middleware
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node records any URL params, and executes an end handlerFn.
type Parameters ¶
type Parameters struct {
// contains filtered or unexported fields
}
Parameters records some parameters
type SSE ¶ added in v1.4.2
type SSE struct { Comment []byte // contains filtered or unexported fields }