Documentation ¶
Overview ¶
HTTP Client & Server.
Index ¶
- func BuildParams(params map[string]string) string
- func Wait()
- type Client
- func (c *Client) Connect(url, data string) (*ClientResponse, error)
- func (c *Client) Delete(url, data string) (*ClientResponse, error)
- func (c *Client) DoRequest(method, url string, data []byte) (*ClientResponse, error)
- func (c *Client) Get(url string) (*ClientResponse, error)
- func (c *Client) Head(url, data string) (*ClientResponse, error)
- func (c *Client) Options(url, data string) (*ClientResponse, error)
- func (c *Client) Patch(url, data string) (*ClientResponse, error)
- func (c *Client) Post(url, data string) (*ClientResponse, error)
- func (c *Client) Put(url, data string) (*ClientResponse, error)
- func (c *Client) SetHeader(key, value string)
- func (c *Client) SetTimeOut(t time.Duration)
- func (c *Client) Trace(url, data string) (*ClientResponse, error)
- type ClientResponse
- func Connect(url, data string) (*ClientResponse, error)
- func Delete(url, data string) (*ClientResponse, error)
- func DoRequest(method, url string, data []byte) (*ClientResponse, error)
- func Get(url string) (*ClientResponse, error)
- func Head(url, data string) (*ClientResponse, error)
- func Options(url, data string) (*ClientResponse, error)
- func Patch(url, data string) (*ClientResponse, error)
- func Post(url, data string) (*ClientResponse, error)
- func Put(url, data string) (*ClientResponse, error)
- func Trace(url, data string) (*ClientResponse, error)
- type Controller
- type Cookie
- func (c *Cookie) Close()
- func (c *Cookie) Get(key string) string
- func (c *Cookie) Map() map[string]string
- func (c *Cookie) Output()
- func (c *Cookie) Remove(key, domain, path string)
- func (c *Cookie) SessionId() string
- func (c *Cookie) Set(key, value string)
- func (c *Cookie) SetCookie(key, value, domain, path string, maxAge int, httpOnly ...bool)
- func (c *Cookie) SetSessionId(sessionid string)
- type CookieItem
- type Domain
- func (d *Domain) BindController(pattern string, c Controller) error
- func (d *Domain) BindControllerMethod(pattern string, c Controller, methods string) error
- func (d *Domain) BindControllerRest(pattern string, c Controller) error
- func (d *Domain) BindHandler(pattern string, handler HandlerFunc) error
- func (d *Domain) BindHookHandler(pattern string, hook string, handler HandlerFunc) error
- func (d *Domain) BindHookHandlerByMap(pattern string, hookmap map[string]HandlerFunc) error
- func (d *Domain) BindObject(pattern string, obj interface{}) error
- func (d *Domain) BindObjectMethod(pattern string, obj interface{}, methods string) error
- func (d *Domain) BindObjectRest(pattern string, obj interface{}) error
- func (d *Domain) BindStatusHandler(status int, handler HandlerFunc)
- func (d *Domain) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc)
- type HandlerFunc
- type HandlerItem
- type HandlerMap
- type Request
- func (r *Request) Exit()
- func (r *Request) Get(k string) string
- func (r *Request) GetClientIp() string
- func (r *Request) GetHost() string
- func (r *Request) GetJson() *gjson.Json
- func (r *Request) GetPost(k string) []string
- func (r *Request) GetPostArray(k string) []string
- func (r *Request) GetPostBool(k string) bool
- func (r *Request) GetPostFloat32(k string) float32
- func (r *Request) GetPostFloat64(k string) float64
- func (r *Request) GetPostInt(k string) int
- func (r *Request) GetPostMap(defaultMap ...map[string]string) map[string]string
- func (r *Request) GetPostString(k string) string
- func (r *Request) GetPostUint(k string) uint
- func (r *Request) GetQuery(k string) []string
- func (r *Request) GetQueryArray(k string) []string
- func (r *Request) GetQueryBool(k string) bool
- func (r *Request) GetQueryFloat32(k string) float32
- func (r *Request) GetQueryFloat64(k string) float64
- func (r *Request) GetQueryInt(k string) int
- func (r *Request) GetQueryMap(defaultMap ...map[string]string) map[string]string
- func (r *Request) GetQueryString(k string) string
- func (r *Request) GetQueryUint(k string) uint
- func (r *Request) GetRaw() []byte
- func (r *Request) GetRequest(k string) []string
- func (r *Request) GetRequestArray(k string) []string
- func (r *Request) GetRequestBool(k string) bool
- func (r *Request) GetRequestFloat32(k string) float32
- func (r *Request) GetRequestFloat64(k string) float64
- func (r *Request) GetRequestInt(k string) int
- func (r *Request) GetRequestMap(defaultMap ...map[string]string) map[string]string
- func (r *Request) GetRequestString(k string) string
- func (r *Request) GetRequestUint(k string) uint
- func (r *Request) IsExited() bool
- func (r *Request) WebSocket() (*WebSocket, error)
- type Response
- func (r *Response) Buffer() []byte
- func (r *Response) BufferLength() int
- func (r *Response) ClearBuffer()
- func (r *Response) OutputBuffer()
- func (r *Response) RedirectTo(location string)
- func (r *Response) SetAllowCrossDomainRequest(allowOrigin string, allowMethods string, maxAge ...int)
- func (r *Response) SetBuffer(buffer []byte)
- func (r *Response) Write(content ...interface{})
- func (r *Response) WriteJson(content interface{}) error
- func (r *Response) WriteJsonP(content interface{}) error
- func (r *Response) WriteStatus(status int, content ...string)
- func (r *Response) WriteXml(content interface{}, rootTag ...string) error
- func (r *Response) Writeln(content ...interface{})
- type ResponseWriter
- type Router
- type Server
- func (s *Server) BindController(pattern string, c Controller) error
- func (s *Server) BindControllerMethod(pattern string, c Controller, methods string) error
- func (s *Server) BindControllerRest(pattern string, c Controller) error
- func (s *Server) BindHandler(pattern string, handler HandlerFunc) error
- func (s *Server) BindHookHandler(pattern string, hook string, handler HandlerFunc) error
- func (s *Server) BindHookHandlerByMap(pattern string, hookmap map[string]HandlerFunc) error
- func (s *Server) BindObject(pattern string, obj interface{}) error
- func (s *Server) BindObjectMethod(pattern string, obj interface{}, methods string) error
- func (s *Server) BindObjectRest(pattern string, obj interface{}) error
- func (s *Server) BindStatusHandler(status int, handler HandlerFunc)
- func (s *Server) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc)
- func (s *Server) Domain(domain string) *Domain
- func (s *Server) EnableAdmin(pattern ...string)
- func (s *Server) EnableHTTPS(certFile, keyFile string) error
- func (s *Server) EnablePprof(pattern ...string)
- func (s *Server) GetCookieMaxAge() int
- func (s *Server) GetLogHandler() func(r *Request, error ...interface{})
- func (s *Server) GetLogPath() string
- func (s *Server) GetName() string
- func (s *Server) GetSessionIdName() string
- func (s *Server) GetSessionMaxAge() int
- func (s *Server) IsAccessLogEnabled() bool
- func (s *Server) IsErrorLogEnabled() bool
- func (s *Server) Restart(newExeFilePath ...string) error
- func (s *Server) Run() error
- func (s *Server) SetAccessLogEnabled(enabled bool)
- func (s *Server) SetAddr(addr string) error
- func (s *Server) SetConfig(c ServerConfig) error
- func (s *Server) SetCookieMaxAge(maxage int)
- func (s *Server) SetErrorLogEnabled(enabled bool)
- func (s *Server) SetHTTPSAddr(addr string) error
- func (s *Server) SetHTTPSPort(port ...int) error
- func (s *Server) SetIdleTimeout(t time.Duration) error
- func (s *Server) SetIndexFiles(index []string) error
- func (s *Server) SetIndexFolder(index bool) error
- func (s *Server) SetLogHandler(handler func(r *Request, error ...interface{}))
- func (s *Server) SetLogPath(path string) error
- func (s *Server) SetMaxHeaderBytes(b int) error
- func (s *Server) SetPort(port ...int) error
- func (s *Server) SetReadTimeout(t time.Duration) error
- func (s *Server) SetServerAgent(agent string) error
- func (s *Server) SetServerRoot(root string) error
- func (s *Server) SetSessionIdName(name string)
- func (s *Server) SetSessionMaxAge(maxage int)
- func (s *Server) SetWriteTimeout(t time.Duration) error
- func (s *Server) Shutdown() error
- func (s *Server) Start() error
- type ServerConfig
- type Session
- func (s *Session) BatchSet(m map[string]interface{})
- func (s *Session) Data() map[string]interface{}
- func (s *Session) Get(k string) interface{}
- func (s *Session) GetBool(k string) bool
- func (s *Session) GetFloat32(k string) float32
- func (s *Session) GetFloat64(k string) float64
- func (s *Session) GetInt(k string) int
- func (s *Session) GetString(k string) string
- func (s *Session) GetUint(k string) uint
- func (s *Session) Id() string
- func (s *Session) Remove(k string)
- func (s *Session) Set(k string, v interface{})
- func (s *Session) UpdateExpire()
- type WebSocket
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
http客户端
func (*Client) DoRequest ¶
func (c *Client) DoRequest(method, url string, data []byte) (*ClientResponse, error)
请求并返回response对象,该方法支持二进制提交数据
type ClientResponse ¶
客户端请求结果对象
func Connect ¶
func Connect(url, data string) (*ClientResponse, error)
func Delete ¶
func Delete(url, data string) (*ClientResponse, error)
func DoRequest ¶
func DoRequest(method, url string, data []byte) (*ClientResponse, error)
该方法支持二进制提交数据
func Get ¶
func Get(url string) (*ClientResponse, error)
func Head ¶
func Head(url, data string) (*ClientResponse, error)
func Options ¶
func Options(url, data string) (*ClientResponse, error)
func Patch ¶
func Patch(url, data string) (*ClientResponse, error)
func Post ¶
func Post(url, data string) (*ClientResponse, error)
func Put ¶
func Put(url, data string) (*ClientResponse, error)
func Trace ¶
func Trace(url, data string) (*ClientResponse, error)
type Cookie ¶
type Cookie struct {
// contains filtered or unexported fields
}
cookie对象
type Domain ¶
type Domain struct {
// contains filtered or unexported fields
}
域名管理器对象
func (*Domain) BindController ¶
func (d *Domain) BindController(pattern string, c Controller) error
控制器注册
func (*Domain) BindControllerMethod ¶
func (d *Domain) BindControllerMethod(pattern string, c Controller, methods string) error
控制器方法注册,methods参数区分大小写
func (*Domain) BindControllerRest ¶
func (d *Domain) BindControllerRest(pattern string, c Controller) error
RESTful控制器注册
func (*Domain) BindHandler ¶
func (d *Domain) BindHandler(pattern string, handler HandlerFunc) error
注意该方法是直接绑定方法的内存地址,执行的时候直接执行该方法,不会存在初始化新的控制器逻辑
func (*Domain) BindHookHandler ¶
func (d *Domain) BindHookHandler(pattern string, hook string, handler HandlerFunc) error
绑定指定的hook回调函数, hook参数的值由ghttp server设定,参数不区分大小写 目前hook支持:Init/Shut
func (*Domain) BindHookHandlerByMap ¶
func (d *Domain) BindHookHandlerByMap(pattern string, hookmap map[string]HandlerFunc) error
通过map批量绑定回调函数
func (*Domain) BindObject ¶
执行对象方法
func (*Domain) BindObjectMethod ¶
执行对象方法注册,methods参数不区分大小写
func (*Domain) BindObjectRest ¶
RESTful执行对象注册
func (*Domain) BindStatusHandler ¶
func (d *Domain) BindStatusHandler(status int, handler HandlerFunc)
绑定指定的状态码回调函数
func (*Domain) BindStatusHandlerByMap ¶
func (d *Domain) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc)
通过map批量绑定状态码回调函数
type HandlerItem ¶
type HandlerItem struct {
// contains filtered or unexported fields
}
http回调函数注册信息
type Request ¶
type Request struct { http.Request Id int // 请求id(唯一) Server *Server // 请求关联的服务器对象 Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全) Session *Session // 与当前请求绑定的Session对象(并发安全) Response *Response // 对应请求的返回数据操作对象 Router *Router // 匹配到的路由对象 EnterTime int64 // 请求进入时间(微秒) LeaveTime int64 // 请求完成时间(微秒) Param interface{} // 开发者自定义参数 // contains filtered or unexported fields }
请求对象
func (*Request) GetPostArray ¶
func (*Request) GetPostBool ¶
func (*Request) GetPostFloat32 ¶
func (*Request) GetPostFloat64 ¶
func (*Request) GetPostInt ¶
func (*Request) GetPostMap ¶
获取指定键名的关联数组,并且给定当指定键名不存在时的默认值 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetPostArray获取特定字段内容
func (*Request) GetPostString ¶
func (*Request) GetPostUint ¶
func (*Request) GetQueryArray ¶
func (*Request) GetQueryBool ¶
func (*Request) GetQueryFloat32 ¶
func (*Request) GetQueryFloat64 ¶
func (*Request) GetQueryInt ¶
func (*Request) GetQueryMap ¶
获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
func (*Request) GetQueryString ¶
func (*Request) GetQueryUint ¶
func (*Request) GetRequest ¶
获得post或者get提交的参数,如果有同名参数,那么按照get->post优先级进行覆盖
func (*Request) GetRequestArray ¶
func (*Request) GetRequestBool ¶
func (*Request) GetRequestFloat32 ¶
func (*Request) GetRequestFloat64 ¶
func (*Request) GetRequestInt ¶
func (*Request) GetRequestMap ¶
获取指定键名的关联数组,并且给定当指定键名不存在时的默认值 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetRequestArray获取特定字段内容
func (*Request) GetRequestString ¶
func (*Request) GetRequestUint ¶
type Response ¶
type Response struct { ResponseWriter Writer *ResponseWriter // io.Writer // contains filtered or unexported fields }
服务端请求返回对象
func (*Response) SetAllowCrossDomainRequest ¶
func (r *Response) SetAllowCrossDomainRequest(allowOrigin string, allowMethods string, maxAge ...int)
允许AJAX跨域访问
func (*Response) WriteStatus ¶
返回HTTP Code状态码
type ResponseWriter ¶
type ResponseWriter struct { http.ResponseWriter Status int // http status Length int // response length }
自定义的ResponseWriter,用于写入流的控制
func (*ResponseWriter) Write ¶
func (w *ResponseWriter) Write(buffer []byte) (int, error)
覆盖父级的WriteHeader方法
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(code int)
覆盖父级的WriteHeader方法
type Router ¶
type Router struct { Uri string // 注册时的pattern - uri Method string // 注册时的pattern - method Domain string // 注册时的pattern - domain Priority int // 优先级,用于链表排序,值越大优先级越高 }
路由对象
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
ghttp.Server结构体
func GetServer ¶
func GetServer(name ...interface{}) *Server
获取/创建一个默认配置的HTTP Server(默认监听端口是80) 单例模式,请保证name的唯一性
func (*Server) BindController ¶
func (s *Server) BindController(pattern string, c Controller) error
绑定控制器,控制器需要实现gmvc.Controller接口 这种方式绑定的控制器每一次请求都会初始化一个新的控制器对象进行处理,对应不同的请求会话
func (*Server) BindControllerMethod ¶
func (s *Server) BindControllerMethod(pattern string, c Controller, methods string) error
这种方式绑定的控制器每一次请求都会初始化一个新的控制器对象进行处理,对应不同的请求会话 第三个参数methods支持多个方法注册,多个方法以英文“,”号分隔,不区分大小写
func (*Server) BindControllerRest ¶
func (s *Server) BindControllerRest(pattern string, c Controller) error
绑定控制器(RESTFul),控制器需要实现gmvc.Controller接口 方法会识别HTTP方法,并做REST绑定处理,例如:Post方法会绑定到HTTP POST的方法请求处理,Delete方法会绑定到HTTP DELETE的方法请求处理 因此只会绑定HTTP Method对应的方法,其他方法不会自动注册绑定 这种方式绑定的控制器每一次请求都会初始化一个新的控制器对象进行处理,对应不同的请求会话
func (*Server) BindHandler ¶
func (s *Server) BindHandler(pattern string, handler HandlerFunc) error
注意该方法是直接绑定函数的内存地址,执行的时候直接执行该方法,不会存在初始化新的控制器逻辑
func (*Server) BindHookHandler ¶
func (s *Server) BindHookHandler(pattern string, hook string, handler HandlerFunc) error
绑定指定的hook回调函数, pattern参数同BindHandler,支持命名路由;hook参数的值由ghttp server设定,参数不区分大小写
func (*Server) BindHookHandlerByMap ¶
func (s *Server) BindHookHandlerByMap(pattern string, hookmap map[string]HandlerFunc) error
通过map批量绑定回调函数
func (*Server) BindObject ¶
绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面 需要注意对象方法的定义必须按照ghttp.HandlerFunc来定义
func (*Server) BindObjectMethod ¶
绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面 第三个参数methods支持多个方法注册,多个方法以英文“,”号分隔,区分大小写
func (*Server) BindObjectRest ¶
绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面 需要注意对象方法的定义必须按照ghttp.HandlerFunc来定义
func (*Server) BindStatusHandler ¶
func (s *Server) BindStatusHandler(status int, handler HandlerFunc)
绑定指定的状态码回调函数
func (*Server) BindStatusHandlerByMap ¶
func (s *Server) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc)
通过map批量绑定状态码回调函数
func (*Server) EnableHTTPS ¶
开启HTTPS支持,但是必须提供Cert和Key文件
func (*Server) GetCookieMaxAge ¶
获取http server参数 - CookieMaxAge
func (*Server) GetLogHandler ¶
获取日志写入的回调函数
func (*Server) GetSessionIdName ¶
获取http server参数 - SessionIdName
func (*Server) GetSessionMaxAge ¶
获取http server参数 - SessionMaxAge
func (*Server) Restart ¶
重启Web Server,参数支持自定义重启的可执行文件路径,不传递时默认和原有可执行文件路径一致。 针对*niux系统: 平滑重启 针对windows : 完整重启
func (*Server) SetAccessLogEnabled ¶
设置是否开启access log日志功能
func (*Server) SetConfig ¶
func (s *Server) SetConfig(c ServerConfig) error
http server setting设置 注意使用该方法进行http server配置时,需要配置所有的配置项,否则没有配置的属性将会默认变量为空
func (*Server) SetCookieMaxAge ¶
设置http server参数 - CookieMaxAge
func (*Server) SetErrorLogEnabled ¶
设置是否开启error log日志功能
func (*Server) SetHTTPSAddr ¶
设置http server参数 - HTTPS Addr
func (*Server) SetHTTPSPort ¶
设置http server参数 - HTTPS Port
func (*Server) SetIdleTimeout ¶
设置http server参数 - IdleTimeout
func (*Server) SetIndexFiles ¶
设置http server参数 - IndexFiles,默认展示文件,如:index.html, index.htm
func (*Server) SetLogHandler ¶
设置日志写入的回调函数
func (*Server) SetMaxHeaderBytes ¶
设置http server参数 - MaxHeaderBytes
func (*Server) SetReadTimeout ¶
设置http server参数 - ReadTimeout
func (*Server) SetServerAgent ¶
设置http server参数 - ServerAgent
func (*Server) SetServerRoot ¶
设置http server参数 - ServerRoot
func (*Server) SetSessionIdName ¶
设置http server参数 - SessionIdName
func (*Server) SetSessionMaxAge ¶
设置http server参数 - SessionMaxAge
func (*Server) SetWriteTimeout ¶
设置http server参数 - WriteTimeout
type ServerConfig ¶
type ServerConfig struct { Addr string // 监听IP和端口,监听本地所有IP使用":端口"(支持多个地址,使用","号分隔) HTTPSAddr string // HTTPS服务监听地址(支持多个地址,使用","号分隔) HTTPSCertPath string // HTTPS证书文件路径 HTTPSKeyPath string // HTTPS签名文件路径 Handler http.Handler // 默认的处理函数 ReadTimeout time.Duration WriteTimeout time.Duration IdleTimeout time.Duration MaxHeaderBytes int // 最大的header长度 IndexFiles []string // 默认访问的文件列表 IndexFolder bool // 如果访问目录是否显示目录列表 ServerAgent string // server agent ServerRoot string // 服务器服务的本地目录根路径 }
HTTP Server 设置结构体
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
单个session对象
func (*Session) GetFloat32 ¶
func (*Session) GetFloat64 ¶
func (*Session) UpdateExpire ¶
func (s *Session) UpdateExpire()
更新过期时间(如果用在守护进程中长期使用,需要手动调用进行更新,防止超时被清除)
Source Files ¶
- ghttp.go
- ghttp_client_request.go
- ghttp_client_response.go
- ghttp_controller.go
- ghttp_func.go
- ghttp_request.go
- ghttp_response.go
- ghttp_server.go
- ghttp_server_admin.go
- ghttp_server_admin_unix.go
- ghttp_server_config.go
- ghttp_server_cookie.go
- ghttp_server_domain.go
- ghttp_server_graceful.go
- ghttp_server_handler.go
- ghttp_server_hooks.go
- ghttp_server_log.go
- ghttp_server_options.go
- ghttp_server_pprof.go
- ghttp_server_router.go
- ghttp_server_service.go
- ghttp_server_session.go
- ghttp_server_status.go
- ghttp_server_websocket.go