Documentation
¶
Overview ¶
实现服务中心规定的服务注册要求,遵循注册协议 engine.Container
分组路由注册
静态路由注册
Index ¶
- Constants
- func Cross(response http.ResponseWriter)
- type Context
- func (ctx *Context) BaseContext() context.Context
- func (ctx *Context) Deadline() (deadline time.Time, ok bool)
- func (ctx *Context) Done() <-chan struct{}
- func (ctx *Context) Err() error
- func (ctx *Context) GetResponse() http.ResponseWriter
- func (ctx *Context) GetVal(key string) *castkit.GoodleVal
- func (ctx *Context) HasTimeout() bool
- func (ctx *Context) Holder() core.Container
- func (ctx *Context) NewInstanceProvider(name string, params ...interface{}) interface{}
- func (ctx *Context) NewSingleProvider(name string) interface{}
- func (ctx *Context) Next() error
- func (ctx *Context) Request() *http.Request
- func (ctx *Context) SetHasTimeout()
- func (ctx *Context) SetMiddwares(handlers []MiddlewareHandler)
- func (ctx *Context) SetVal(key string, value interface{})
- func (ctx *Context) Value(key interface{}) interface{}
- func (ctx *Context) WriterMux() *sync.Mutex
- type Engine
- func (self *Engine) AddRoute(methoad, prefix, uri string, routeType int8, handler RequestHandler, ...)
- func (this *Engine) Delete(url string, handler RequestHandler, middlewares ...MiddlewareHandler)
- func (this *Engine) FindRouteHandler(request *http.Request) t3WebRoute
- func (this *Engine) Get(url string, handler RequestHandler, middlewares ...MiddlewareHandler)
- func (self *Engine) NewHttpEngine(serviceCenter core.Container) *Engine
- func (this *Engine) Post(url string, handler RequestHandler, middlewares ...MiddlewareHandler)
- func (hc *Engine) Prefix(prefix string) IGroup
- func (this *Engine) Put(url string, handler RequestHandler, middlewares ...MiddlewareHandler)
- func (self *Engine) ServeHTTP(response http.ResponseWriter, request *http.Request)
- func (self *Engine) UseCross()
- func (self *Engine) UseMiddleware(handlers ...MiddlewareHandler)
- type HttpServerProvider
- func (*HttpServerProvider) AfterInit(instance any) error
- func (self *HttpServerProvider) BeforeInit(c core.Container) error
- func (*HttpServerProvider) InitOnBoot() bool
- func (self *HttpServerProvider) Name() string
- func (sp *HttpServerProvider) Params(c core.Container) []interface{}
- func (sp *HttpServerProvider) RegisterProviderInstance(c core.Container) core.NewInstanceFunc
- type HttpServerService
- type IGroup
- type IRequest
- type IResponse
- type MiddlewareHandler
- type Prefix
- func (p *Prefix) Delete(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)
- func (p *Prefix) Get(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)
- func (p *Prefix) Post(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)
- func (p *Prefix) Put(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)
- func (p *Prefix) UseMiddleware(middlewares ...MiddlewareHandler) IGroup
- type ReqStruct
- func (req *ReqStruct) BindXml(obj interface{}) error
- func (req *ReqStruct) ClientIp() string
- func (req *ReqStruct) Cookie(key string) (string, bool)
- func (req *ReqStruct) Cookies() map[string]string
- func (req *ReqStruct) Form(key string) interface{}
- func (req *ReqStruct) FormAll() map[string][]string
- func (req *ReqStruct) FormBool(key string, defaultValue ...bool) (bool, bool)
- func (req *ReqStruct) FormFile(key string, limit ...int) (multipart.File, *multipart.FileHeader, url.Values, error)
- func (req *ReqStruct) FormFloat32(key string, defaultValue ...float32) (float32, bool)
- func (req *ReqStruct) FormFloat64(key string, defaultValue ...float64) (float64, bool)
- func (req *ReqStruct) FormInt(key string, defaultValue ...int) (int, bool)
- func (req *ReqStruct) FormInt64(key string, defaultValue ...int64) (int64, bool)
- func (req *ReqStruct) FormString(key string, defaultValue ...string) (string, bool)
- func (req *ReqStruct) FormStringSlice(key string, defaultValue ...[]string) ([]string, bool)
- func (req *ReqStruct) Get(key string) interface{}
- func (req *ReqStruct) GetBool(key string, defaultValue ...bool) (bool, bool)
- func (req *ReqStruct) GetFloat32(key string, defaultValue ...float32) (float32, bool)
- func (req *ReqStruct) GetFloat64(key string, defaultValue ...float64) (float64, bool)
- func (req *ReqStruct) GetInt(key string, defaultValue ...int) int
- func (req *ReqStruct) GetInt64(key string, defaultValue ...int64) (int64, bool)
- func (req *ReqStruct) GetRawData() ([]byte, error)
- func (req *ReqStruct) GetString(key string, defaultValue ...string) string
- func (req *ReqStruct) GetStringSlice(key string, defaultValue ...[]string) ([]string, bool)
- func (req *ReqStruct) Header(key string) (string, bool)
- func (req *ReqStruct) Headers() map[string][]string
- func (req *ReqStruct) Host() string
- func (req *ReqStruct) JsonScan(s any) error
- func (req *ReqStruct) Method() string
- func (req *ReqStruct) PostBool(key string, defaultValue ...bool) (bool, bool)
- func (req *ReqStruct) PostFloat32(key string, defaultValue ...float32) (float32, bool)
- func (req *ReqStruct) PostFloat64(key string, defaultValue ...float64) (float64, bool)
- func (req *ReqStruct) PostInt(key string, defaultValue ...int) (int, bool)
- func (req *ReqStruct) PostInt32(key string, defaultValue ...int32) (int32, bool)
- func (req *ReqStruct) PostInt64(key string, defaultValue ...int64) (int64, bool)
- func (req *ReqStruct) PostString(key string, defaultValue ...string) (string, bool)
- func (req *ReqStruct) QueryAll() map[string][]string
- func (req *ReqStruct) Uri() string
- type RequestHandler
- type RespStruct
- func (res *RespStruct) Html(file string, obj interface{}) IResponse
- func (res *RespStruct) Json(obj interface{}) IResponse
- func (res *RespStruct) Jsonp(obj interface{}) IResponse
- func (res *RespStruct) Redirect(path string) IResponse
- func (res *RespStruct) SetCookie(key string, val string, maxAge int, path string, domain string, secure bool, ...) IResponse
- func (res *RespStruct) SetHeader(key string, val string) IResponse
- func (res *RespStruct) SetOkStatus() IResponse
- func (res *RespStruct) SetStatus(code int) IResponse
- func (res *RespStruct) Text(format string, values ...interface{}) IResponse
- func (res *RespStruct) Xml(obj interface{}) IResponse
Constants ¶
View Source
const Name = "httpserver"
Variables ¶
This section is empty.
Functions ¶
func Cross ¶ added in v0.10.0
func Cross(response http.ResponseWriter)
Types ¶
type Context ¶
type Context struct {
// 配置服务
Req IRequest
Resp RespStruct
Config config.Service
Cache cache.Service
I18n func(string) string
// contains filtered or unexported fields
}
实现标准库的 Context 基本现在所有第三方库函数都会根据官方的建议将第一个参数设置为标准 Context 接口, 所以定制一个自己的 context 很有用,这里将 request 和 response 封装到 context, 这样就可以在整条请求链路中随时处理输入输出
func NewContext ¶
func (*Context) BaseContext ¶
func (*Context) GetResponse ¶
func (ctx *Context) GetResponse() http.ResponseWriter
func (*Context) HasTimeout ¶
func (*Context) NewInstanceProvider ¶
func (*Context) NewSingleProvider ¶
将服务注册到服务中心
func (*Context) SetHasTimeout ¶
func (ctx *Context) SetHasTimeout()
func (*Context) SetMiddwares ¶ added in v0.10.0
func (ctx *Context) SetMiddwares(handlers []MiddlewareHandler)
请求时中间件
type Engine ¶ added in v0.10.0
type Engine struct {
// contains filtered or unexported fields
}
框架核心结构体
func (*Engine) AddRoute ¶ added in v0.10.0
func (self *Engine) AddRoute(methoad, prefix, uri string, routeType int8, handler RequestHandler, middlewares ...MiddlewareHandler)
添加路由到 map prefix 主要用于请求进来时匹配分组中间件
func (*Engine) Delete ¶ added in v0.10.0
func (this *Engine) Delete(url string, handler RequestHandler, middlewares ...MiddlewareHandler)
func (*Engine) FindRouteHandler ¶ added in v0.10.0
匹配路由,如果没有匹配到,返回 nil
func (*Engine) Get ¶ added in v0.10.0
func (this *Engine) Get(url string, handler RequestHandler, middlewares ...MiddlewareHandler)
func (*Engine) NewHttpEngine ¶ added in v0.10.0
初始化框架核心结构
func (*Engine) Post ¶ added in v0.10.0
func (this *Engine) Post(url string, handler RequestHandler, middlewares ...MiddlewareHandler)
func (*Engine) Put ¶ added in v0.10.0
func (this *Engine) Put(url string, handler RequestHandler, middlewares ...MiddlewareHandler)
func (*Engine) ServeHTTP ¶ added in v0.10.0
func (self *Engine) ServeHTTP(response http.ResponseWriter, request *http.Request)
框架核心结构实现 Handler 接口
func (*Engine) UseMiddleware ¶ added in v0.10.0
func (self *Engine) UseMiddleware(handlers ...MiddlewareHandler)
注册全局中间件
type HttpServerProvider ¶
type HttpServerProvider struct {
core.ServiceProvider
HttpServer *Engine
}
func (*HttpServerProvider) AfterInit ¶ added in v0.10.0
func (*HttpServerProvider) AfterInit(instance any) error
func (*HttpServerProvider) BeforeInit ¶ added in v0.10.0
func (self *HttpServerProvider) BeforeInit(c core.Container) error
func (*HttpServerProvider) InitOnBoot ¶ added in v0.10.0
func (*HttpServerProvider) InitOnBoot() bool
func (*HttpServerProvider) Name ¶
func (self *HttpServerProvider) Name() string
func (*HttpServerProvider) Params ¶
func (sp *HttpServerProvider) Params(c core.Container) []interface{}
func (*HttpServerProvider) RegisterProviderInstance ¶
func (sp *HttpServerProvider) RegisterProviderInstance(c core.Container) core.NewInstanceFunc
type HttpServerService ¶
type HttpServerService struct {
*Engine
// contains filtered or unexported fields
}
type IGroup ¶
type IGroup interface {
Get(string, RequestHandler, ...MiddlewareHandler)
Post(string, RequestHandler, ...MiddlewareHandler)
Put(string, RequestHandler, ...MiddlewareHandler)
Delete(string, RequestHandler, ...MiddlewareHandler)
UseMiddleware(...MiddlewareHandler) IGroup
}
IGroup 路由分组接口
type IRequest ¶
type IRequest interface {
JsonScan(s any) error // json body
BindXml(obj interface{}) error // xml body
GetRawData() ([]byte, error) // 其他格式
// 获取查询字符串中的参数,如: xxx.com?a=foo&b=bar&c[]=barbar
Get(key string) interface{}
GetInt(key string, defaultValue ...int) int
GetInt64(key string, defaultValue ...int64) (int64, bool)
GetFloat64(key string, defaultValue ...float64) (float64, bool)
GetFloat32(key string, defaultValue ...float32) (float32, bool)
GetBool(key string, defaultValue ...bool) (bool, bool)
GetString(key string, defaultValue ...string) string
GetStringSlice(key string, defaultValue ...[]string) ([]string, bool)
PostInt(key string, defaultValue ...int) (int, bool)
PostInt32(key string, defaultValue ...int32) (int32, bool)
PostInt64(key string, defaultValue ...int64) (int64, bool)
PostFloat32(key string, defaultValue ...float32) (float32, bool)
PostFloat64(key string, defaultValue ...float64) (float64, bool)
PostString(key string, defaultValue ...string) (string, bool)
PostBool(key string, defaultValue ...bool) (bool, bool)
// form 表单中的参数
Form(key string) interface{}
FormInt(key string, defaultValue ...int) (int, bool)
FormInt64(key string, defaultValue ...int64) (int64, bool)
FormFloat64(key string, defaultValue ...float64) (float64, bool)
FormFloat32(key string, defaultValue ...float32) (float32, bool)
FormBool(key string, defaultValue ...bool) (bool, bool)
FormString(key string, defaultValue ...string) (string, bool)
FormStringSlice(key string, defaultValue ...[]string) ([]string, bool)
FormFile(key string, args ...int) (multipart.File, *multipart.FileHeader, url.Values, error)
// 其他格式
Uri() string
Method() string
Host() string
ClientIp() string
// header
Headers() map[string][]string
Header(key string) (string, bool)
// cookie
Cookie(key string) (string, bool)
Cookies() map[string]string
// contains filtered or unexported methods
}
为请求封装方法,在 Context 上实现接口 统一返回值中的 bool 代表请求方是否有传递这个数据过来
type IResponse ¶
type IResponse interface {
Json(obj interface{}) IResponse
Html(template string, obj interface{}) IResponse
Jsonp(obj interface{}) IResponse
Xml(obj interface{}) IResponse
Text(format string, values ...interface{}) IResponse
Redirect(path string) IResponse // 重定向
SetHeader(key string, val string) IResponse
SetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse
SetOkStatus() IResponse // 设置 200 状态
SetStatus(code int) IResponse // 设置其他状态码
}
为响应封装方法 方法返回 IResponse 本身代表支持链式调用,例如:res.SetOkStatus().Json("success")
type MiddlewareHandler ¶ added in v0.10.0
type Prefix ¶
type Prefix struct {
// contains filtered or unexported fields
}
实现了 IGroup,按前缀分组
func (*Prefix) Delete ¶
func (p *Prefix) Delete(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)
func (*Prefix) Get ¶
func (p *Prefix) Get(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)
func (*Prefix) Post ¶
func (p *Prefix) Post(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)
func (*Prefix) Put ¶
func (p *Prefix) Put(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)
func (*Prefix) UseMiddleware ¶
func (p *Prefix) UseMiddleware(middlewares ...MiddlewareHandler) IGroup
type ReqStruct ¶
type ReqStruct struct {
IRequest
// contains filtered or unexported fields
}
func (*ReqStruct) FormFile ¶
func (req *ReqStruct) FormFile(key string, limit ...int) (multipart.File, *multipart.FileHeader, url.Values, error)
limit[0] 单位:byte
func (*ReqStruct) FormFloat32 ¶
func (*ReqStruct) FormFloat64 ¶
func (*ReqStruct) FormString ¶
func (*ReqStruct) FormStringSlice ¶
func (*ReqStruct) GetFloat32 ¶
func (*ReqStruct) GetFloat64 ¶
func (*ReqStruct) GetStringSlice ¶
请求 /xxx?a=11&a=22 中的参数 a 是能组成数组的
func (*ReqStruct) JsonScan ¶ added in v0.10.0
将body文本解析到 obj 结构体中 params := ¶msStruct{} ctx.Req.BindJson(params)
func (*ReqStruct) PostFloat32 ¶ added in v0.10.0
func (*ReqStruct) PostFloat64 ¶ added in v0.10.0
func (*ReqStruct) PostString ¶ added in v0.10.0
type RequestHandler ¶
type RequestHandler func(c *Context) // API / 控制器函数
type RespStruct ¶
type RespStruct struct {
// contains filtered or unexported fields
}
func (*RespStruct) Html ¶
func (res *RespStruct) Html(file string, obj interface{}) IResponse
html输出
func (*RespStruct) Json ¶
func (res *RespStruct) Json(obj interface{}) IResponse
func (*RespStruct) SetCookie ¶
func (res *RespStruct) SetCookie(key string, val string, maxAge int, path string, domain string, secure bool, httpOnly bool) IResponse
Cookie
func (*RespStruct) SetHeader ¶
func (res *RespStruct) SetHeader(key string, val string) IResponse
header
func (*RespStruct) Text ¶
func (res *RespStruct) Text(format string, values ...interface{}) IResponse
string
Source Files
¶
Click to show internal directories.
Click to hide internal directories.