Documentation ¶
Overview ¶
Package zweb 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()
- func HasLocalIP(ip net.IP) bool
- func HasLocalIPddr(ip string) bool
- func IP2Long(ip net.IP) (i uint, err error)
- func IPString2Long(ip string) (i uint, err error)
- func Long2IP(i uint) (ip net.IP, err error)
- func Long2IPString(i uint) (IP string, err error)
- func RemoteIP(r *http.Request) string
- func Run()
- type Api
- type Binder
- type Context
- func (c *Context) Abort(code ...int)
- func (c *Context) Bind(obj interface{}) (err error)
- func (c *Context) Byte(code int, value []byte)
- func (c *Context) CompletionLink(link string) string
- func (c *Context) ContentType() 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) FormFile(name string) (*multipart.FileHeader, error)
- func (c *Context) GetAllParam() ParamsMapType
- func (c *Context) GetAllQueryst() url.Values
- func (c *Context) GetClientIP() (IP string)
- func (c *Context) GetCookie(name string) string
- func (c *Context) GetDataRaw() (result string, err 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, err error)
- 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) GetReferer() string
- func (c *Context) GetUserAgent() string
- func (c *Context) HTML(code int, html string)
- func (c *Context) Host() string
- func (c *Context) IsAjax() bool
- func (c *Context) IsWebsocket() bool
- func (c *Context) JSON(code int, values interface{})
- func (c *Context) MultipartForm() (*multipart.Form, error)
- func (c *Context) Next() (next HandlerFunc)
- func (c *Context) PostFormMap(key string) map[string]string
- func (c *Context) PrevContent() *PrevData
- func (c *Context) Redirect(link string, statusCode ...int)
- func (c *Context) ResJSON(code int, msg string, data interface{})
- func (c *Context) Reset(w http.ResponseWriter, r *http.Request)
- func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dist string) error
- func (c *Context) SetContent(data *PrevData)
- 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 int) *Context
- func (c *Context) String(code int, format string, values ...interface{})
- func (c *Context) Template(code int, name string, data interface{}, funcMap ...map[string]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, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) Customize(method, path string, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) DELETE(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) DELETEAndName(path string, handle HandlerFunc, routeName string)
- func (e *Engine) FindHandle(rw *Context, req *http.Request, requestURL string, applyMiddleware bool) (not bool)
- func (e *Engine) GET(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) GETAndName(path string, handle HandlerFunc, routeName string)
- func (e *Engine) GenerateURL(method string, routeName string, params map[string]string) (string, error)
- func (e *Engine) GetMiddleware() []HandlerFunc
- 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, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) HEADAndName(path string, handle HandlerFunc, routeName string)
- func (e *Engine) Handle(method string, path string, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) HandleNotFound(c *Context)
- func (e *Engine) IsDebug() bool
- 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 HandlerFunc)
- func (e *Engine) OPTIONS(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) OPTIONSAndName(path string, handle HandlerFunc, routeName string)
- func (e *Engine) PATCH(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) PATCHAndName(path string, handle HandlerFunc, routeName string)
- func (e *Engine) POST(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) POSTAndName(path string, handle HandlerFunc, routeName string)
- func (e *Engine) PUT(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
- func (e *Engine) PUTAndName(path string, handle HandlerFunc, routeName string)
- func (e *Engine) PanicHandler(handler PanicFunc)
- func (e *Engine) PreHandle(preHandle func(context *Context) (stop bool))
- 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) SetMode(value string)
- func (e *Engine) SetTimeout(Timeout time.Duration, WriteTimeout ...time.Duration)
- func (e *Engine) Static(relativePath, root string)
- func (e *Engine) StaticFS(relativePath string, fs http.FileSystem)
- func (e *Engine) StaticFile(relativePath, filepath string)
- func (e *Engine) Use(middleware ...HandlerFunc)
- type Error
- type Func
- type HandlerFunc
- type MiddlewareFunc
- type MiddlewareType
- type Node
- type PanicFunc
- type Parameters
- type ParamsMapType
- type PrevData
- type TlsCfg
- type Tree
Constants ¶
const ( MIMEJSON = "application/json" MIMEPlain = "text/plain" MIMEPOSTForm = "application/x-www-form-urlencoded" MIMEMultipartPOSTForm = "multipart/form-data" )
Content-Type MIME of the most common data formats
const ( // DebugMode debug DebugMode = "debug" // ReleaseMode release ReleaseMode = "release" // TestMode test TestMode = "test" )
Variables ¶
var ( // KindBinders is a lookup from the kind of a type to the bind.Func that binds // it. It is less specific than the TypeBinders and used as a fallback. KindBinders map[reflect.Kind]Func // TypeBinders is a lookup from a specific type to the bind.Func that binds it. // Applications may add custom binders to this map to override the default behavior. TypeBinders map[reflect.Type]Func // TimeFormats are the time layout strings used to attempt to parse data into a time.Time. // They are attempted in order. TimeFormats = []string{"2006-01-02 15:04", "2006-01-02"} )
var ( ContentTypePlain = "text/plain; charset=utf-8" ContentTypeHTML = "text/html; charset=utf-8" 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__") // Shutdown Done executed after shutting down the server ShutdownDone func() // CloseHotRestart CloseHotRestart bool )
Functions ¶
func ClientPublicIP ¶
func CloseHotRestartFileMd5 ¶ added in v0.1.26
func CloseHotRestartFileMd5()
CloseHotRestartFileMd5 CloseHotRestartFileMd5
func HasLocalIP ¶
func HasLocalIPddr ¶
func IPString2Long ¶
func Long2IPString ¶
Types ¶
type Binder ¶ added in v0.0.19
type Binder struct { Values map[string][]string Files map[string][]*multipart.FileHeader }
Binder handles binding of parameter maps to Go data structures.
func Request ¶ added in v0.0.19
Request returns a binder initialized with the request's form and query string data (including multipart forms).
type Context ¶
type Context struct { Code int sync.RWMutex StartTime time.Time StopHandle bool Writer http.ResponseWriter Request *http.Request Engine *Engine Log *zlog.Logger 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
func (*Context) DefaultPostForm ¶
func (*Context) GetAllParam ¶
func (c *Context) GetAllParam() ParamsMapType
GetAllParam Get the value of all param in the route
func (*Context) GetAllQueryst ¶
func (*Context) GetClientIP ¶ added in v0.1.20
GetClientIP Client IP
func (*Context) GetDataRaw ¶ added in v0.0.19
func (*Context) GetPostFormAll ¶ added in v0.0.19
func (*Context) GetPostFormMap ¶
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) Next ¶
func (c *Context) Next() (next HandlerFunc)
func (*Context) PrevContent ¶ added in v0.0.19
func (*Context) Reset ¶ added in v0.1.34
func (c *Context) Reset(w http.ResponseWriter, r *http.Request)
func (*Context) SaveUploadedFile ¶
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dist string) error
func (*Context) SetContent ¶ added in v0.1.21
func (*Context) SetContentType ¶ added in v0.0.26
func (*Context) Template ¶
func (c *Context) Template(code int, name string, data interface{}, funcMap ...map[string]interface{})
Template export template
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 { // Log Log Log *zlog.Logger Cache *zcache.Table ShowFavicon bool MaxMultipartMemory int64 // contains filtered or unexported fields }
Engine is a simple HTTP route multiplexer that parses a request path
func (*Engine) Any ¶
func (e *Engine) Any(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) Customize ¶ added in v0.0.19
func (e *Engine) Customize(method, path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) DELETE ¶
func (e *Engine) DELETE(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) DELETEAndName ¶
func (e *Engine) DELETEAndName(path string, handle HandlerFunc, routeName string)
func (*Engine) FindHandle ¶ added in v0.0.19
func (*Engine) GET ¶
func (e *Engine) GET(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) GETAndName ¶
func (e *Engine) GETAndName(path string, handle HandlerFunc, routeName string)
func (*Engine) GenerateURL ¶
func (*Engine) GetMiddleware ¶
func (e *Engine) GetMiddleware() []HandlerFunc
GetMiddleware GetMiddleware
func (*Engine) HEAD ¶
func (e *Engine) HEAD(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) HEADAndName ¶
func (e *Engine) HEADAndName(path string, handle HandlerFunc, routeName string)
func (*Engine) Handle ¶
func (e *Engine) Handle(method string, path string, handle HandlerFunc, moreHandler ...HandlerFunc)
Handle registers new request handler
func (*Engine) HandleNotFound ¶
func (*Engine) NewContext ¶ added in v0.1.34
func (*Engine) NotFoundHandler ¶ added in v0.0.26
func (e *Engine) NotFoundHandler(handler HandlerFunc)
func (*Engine) OPTIONS ¶
func (e *Engine) OPTIONS(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) OPTIONSAndName ¶
func (e *Engine) OPTIONSAndName(path string, handle HandlerFunc, routeName string)
func (*Engine) PATCH ¶
func (e *Engine) PATCH(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) PATCHAndName ¶
func (e *Engine) PATCHAndName(path string, handle HandlerFunc, routeName string)
func (*Engine) POST ¶
func (e *Engine) POST(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) POSTAndName ¶
func (e *Engine) POSTAndName(path string, handle HandlerFunc, routeName string)
func (*Engine) PUT ¶
func (e *Engine) PUT(path string, handle HandlerFunc, moreHandler ...HandlerFunc)
func (*Engine) PUTAndName ¶
func (e *Engine) PUTAndName(path string, handle HandlerFunc, routeName string)
func (*Engine) PanicHandler ¶
func (*Engine) SetCustomMethodField ¶
SetCustomMethodField SetCustomMethodField
func (*Engine) SetTimeout ¶
SetTimeout setTimeout
func (*Engine) StaticFile ¶
func (*Engine) Use ¶
func (e *Engine) Use(middleware ...HandlerFunc)
type Func ¶ added in v0.0.19
Func is a binding function that is responsible for extracting and converting the relevant parameters from the binder and writing the result to the given destination.
type MiddlewareFunc ¶
type MiddlewareFunc func(c *Context, fn HandlerFunc)
MiddlewareFunc MiddlewareFunc
type MiddlewareType ¶
type MiddlewareType HandlerFunc
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 handler.
type Parameters ¶
type Parameters struct {
// contains filtered or unexported fields
}
Parameters records some parameters
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree records node
func (*Tree) Add ¶
func (t *Tree) Add(pattern string, handle HandlerFunc, middleware ...HandlerFunc)