Documentation
¶
Index ¶
- Variables
- func AddCmd(name string, cmd func())
- func EncodeHtml3(rr *http.Request, rw http.ResponseWriter, rs *Result)
- func EncodeJson2(rr *http.Request, rw http.ResponseWriter, rs *Result)
- func Execute(envpre, appname, version, appinfo string)
- func Favicon(ctx *Ctx)
- func FieldInject(target any, value any, tag string, debug bool) bool
- func FieldSetVal(target any, field string, value any)
- func FieldValue(target any, field string) any
- func FieldValue_(target any, field string) any
- func GET(key string, hdl HandleFunc, svc SvcKit)
- func GetAction(uu *url.URL) string
- func GetRemoteIP(req *http.Request) string
- func GetReqType(request *http.Request) string
- func GetTraceID(request *http.Request) string
- func HTML0(zg *Zoo, rr *http.Request, rw http.ResponseWriter, rs any, tp string)
- func HasPrepath(path string, epre string) bool
- func Healthz(ctx *Ctx)
- func Initializ()
- func Inject[T any](kit SvcKit, val T) T
- func JSON(ctx *Ctx, res *Result)
- func JSON0(rr *http.Request, rw http.ResponseWriter, rs *Result)
- func POST(key string, hdl HandleFunc, svc SvcKit)
- func PrintVersion()
- func Ptr[T any](v T) *T
- func ReadBody[T any](rr *http.Request, rb T) (T, error)
- func ReadForm[T any](rr *http.Request, rb T) (T, error)
- func RegKey[T any](kit SvcKit, inj bool, key string, val T) T
- func RegSvc[T any](kit SvcKit, val T) T
- func Register(key string, opts ...any)
- func RunHttpServe()
- func SetRouterKeyByDir(rr *http.Request)
- func SetRouterKeyByHst(rr *http.Request)
- func WriteResp(rw http.ResponseWriter, ctype string, code int, data []byte)
- type BufferPool
- type BufferPoolDef
- type Closed
- type Ctx
- func (ctx *Ctx) Abort()
- func (ctx *Ctx) BYTE(bts io.Reader, hss int, cty string)
- func (ctx *Ctx) Clear()
- func (ctx *Ctx) Clone(hasContext, hasRequest bool) *Ctx
- func (ctx *Ctx) HTML(tpl string, res any, hss int)
- func (ctx *Ctx) IsAbort() bool
- func (ctx *Ctx) JERR(err error, hss int)
- func (ctx *Ctx) JSON(err error)
- func (ctx *Ctx) TEXT(txt string, hss int)
- type DirRouter
- type Engine
- type EngineConf
- type HandleFunc
- type HstRouter
- type MapRouter
- type MuxRouter
- type RaData
- type RdeHelper
- type Ref
- type Result
- type Result2
- type ResultEncoder
- type Server
- type Slice
- type SvcKit
- type TplCtx
- type TplKit
- type Zoo
Constants ¶
This section is empty.
Variables ¶
var ( AppName = "zoo" Version = "v0.0.0" AppInfo = "(https://github.com/suisrc/zoo.git)" G = struct { Server EngineConf }{} IngoreError = errors.New("ignore error") // 路由构建器 Engines = map[string]func(SvcKit) Engine{ "map": NewMapRouter, "mux": NewMuxRouter, "dir": NewDirRouter, "hst": NewHstRouter, } )
var (
ErrTplNotFound = errors.New("tpl not found")
)
var HttpServeDef = true // 是否启动默认 http 服务? 这里不能通过配置,只能通过代码控制
----------------------------------------------------------------------------
var ResultEncoders = map[string]ResultEncoder{ "2": EncodeJson2, "3": EncodeHtml3, }
Functions ¶
func EncodeHtml3 ¶
func EncodeHtml3(rr *http.Request, rw http.ResponseWriter, rs *Result)
func EncodeJson2 ¶
func EncodeJson2(rr *http.Request, rw http.ResponseWriter, rs *Result)
func FieldInject ¶
获取 target 中每个字段的属性,注入和 value 属性的字段 这只是一个演示的例子,实际开发中,请使用 SvcKit 模块
func FieldValue_ ¶
获取字段, 可夸包获取私有字段 闭包原则,原则上不建议使用该方法,因为改方法是在破坏闭包原则
func GetRemoteIP ¶
func HasPrepath ¶
前缀匹配, 用于路径判断, 例如 /api/v1/user, pre=/api, 则返回 true
func JSON0 ¶
func JSON0(rr *http.Request, rw http.ResponseWriter, rs *Result)
响应 JSON 结果: content-type http-status json-data
func RegKey ¶
*
- 注册服务, key 必须唯一, 如果 key 为空, 使用 val.(type).Name() 作为 key
- @param kit 服务容器
- @param inj 自动注入
- @param key 服务 key
- @param val 服务实例
func SetRouterKeyByDir ¶
SetRouterKeyByDir 从 URL.Path 中提取第一个目录作为 key,并设置到 X-Router-Key 头部信息中,同时更新 URL.Path 去掉第一个目录
func SetRouterKeyByHst ¶
SetRouterKeyByHst 从 Host 中提取子域名作为 key,并设置到 X-Router-Key 头部信息中,供后续路由识别使用
Types ¶
type BufferPool ¶
func NewBufferPool ¶
func NewBufferPool(defCap, maxCap int) BufferPool
NewBufferPool 初始化缓冲池 defCap: 新缓冲区的默认容量(如32KB) maxCap: 允许归还的最大容量(如1MB)
type BufferPoolDef ¶
type BufferPoolDef struct {
// contains filtered or unexported fields
}
BufferPoolDef 字节缓冲池:基于sync.Pool实现
func (*BufferPoolDef) Put ¶
func (p *BufferPoolDef) Put(buf []byte)
Put 归还缓冲区:重置后放回池(若容量超过maxCap则丢弃)
type Ctx ¶
type Ctx struct {
Ctx context.Context
// Cancel func
Cancel context.CancelFunc
// All Module
SvcKit SvcKit
// Request action
Action string
// Request Cache
Caches map[string]any
// Params
Params func(key string) string
// Request
Request *http.Request
// Response
Writer http.ResponseWriter
// Trace ID
TraceID string
// X-Request-Rt
ReqType string
// contains filtered or unexported fields
}
请求上下文内容
type DirRouter ¶
type DirRouter struct {
Helper RdeHelper
Router map[string]Engine
// contains filtered or unexported fields
}
func (*DirRouter) Handle ¶
func (aa *DirRouter) Handle(method, action string, handle HandleFunc)
type Engine ¶
type Engine interface {
Name() string // router engine name
Handle(method, action string, handle HandleFunc) // register router handle, [method]可能为"", [action]开头无"/"
ServeHTTP(rw http.ResponseWriter, rr *http.Request) // http.HandlerFunc
}
引擎接口, Engine, 不适用 Router 是为了和 其他 Router 名字上区分开。以便于支持多 Router 而不会出现冲突
func NewDirRouter ¶
func NewHstRouter ¶
func NewMapRouter ¶
func NewMuxRouter ¶
type EngineConf ¶ added in v0.0.2
type EngineConf struct {
Fxser bool `json:"xser"` // 标记 xser 头部信息
Local bool `json:"local"`
Addr string `json:"addr" default:"0.0.0.0"`
Port int `json:"port" default:"80"`
Ptls int `json:"ptls" default:"443"`
Dual bool `json:"dual"` // http and https
Engine string `json:"engine"` // router engine
ApiRoot string `json:"root"` // root api root
TplPath string `json:"tpl"` // templates folder path
ReqXrtd string `json:"xrt"` // X-Request-Rt default value, 1: zoo, 2: ali, 3: html
}
默认配置, Engine 配置需要内嵌该结构体
type HstRouter ¶
type HstRouter struct {
Helper RdeHelper
Router map[string]Engine
// contains filtered or unexported fields
}
func (*HstRouter) Handle ¶
func (aa *HstRouter) Handle(method, action string, handle HandleFunc)
type MapRouter ¶
type MapRouter struct {
Handle_ HandleFunc // 默认函数,没有找到Action触发
Handles map[string]HandleFunc // 接口集合
// contains filtered or unexported fields
}
func (*MapRouter) GetHandle ¶
func (aa *MapRouter) GetHandle(method, action string) (HandleFunc, bool)
func (*MapRouter) Handle ¶
func (aa *MapRouter) Handle(method, action string, handle HandleFunc)
type MuxRouter ¶
func (*MuxRouter) Handle ¶
func (aa *MuxRouter) Handle(method, action string, handle HandleFunc)
type Result ¶
type Result struct {
Success bool `json:"success"`
Data any `json:"data,omitempty"`
ErrCode string `json:"errcode,omitempty"`
Message string `json:"message,omitempty"`
ErrShow int `json:"errshow,omitempty"`
TraceID string `json:"traceid,omitempty"`
Total *int `json:"total,omitempty"`
Ctx *Ctx `json:"-"`
Status int `json:"-"`
Header map[string]string `json:"-"`
TplKey string `json:"-"`
}
定义响应结构体
type Result2 ¶
type ResultEncoder ¶
type ResultEncoder func(rr *http.Request, rw http.ResponseWriter, rs *Result)
type Server ¶
type SvcKit ¶
type SvcKit interface {
Engine() *Zoo // 获取模块管理器 *Zoo 接口
Inject(obj any) SvcKit // 注册服务 inject 使用 `svckit:"xxx"` 初始化服务
Router(key string, hdl HandleFunc) // 注册路由
Get(key string) any // 获取服务
Set(key string, val any) SvcKit // 增加服务 val = nil 是卸载服务
Map() map[string]any // 服务列表, 注意,是副本
}
服务工具接口
type TplKit ¶
type TplKit interface {
Get(key string) *TplCtx
Render(wr io.Writer, name string, data any) error
Load(key string, str string) *TplCtx
Preload(dir string) error
}
模版工具接口
type Zoo ¶
type Zoo struct {
Servers Slice[Server] // 接口服务模块列表
Closeds Slice[Closed] // 模块关闭函数列表
TLSConf *tls.Config // Certificates, GetCertificate
Engine Engine // 路由引擎
SvcKit SvcKit // 服务工具
TplKit TplKit // 模版工具
// contains filtered or unexported fields
}
默认服务实体
func (*Zoo) AddRouter ¶
func (aa *Zoo) AddRouter(key string, handle HandleFunc)
增加处理函数 @param key: [method:]action, 如果 method 为空,则默认为 所有请求
func (*Zoo) ServeHTTP ¶
func (aa *Zoo) ServeHTTP(rw http.ResponseWriter, rr *http.Request)
默认相应函数 http.HandlerFunc(zoo.ServeHTTP)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
gtw
@see ReverseProxy(https://pkg.go.dev/net/http/httputil#ReverseProxy) @see net/http/httputil.ReverseProxy
|
@see ReverseProxy(https://pkg.go.dev/net/http/httputil#ReverseProxy) @see net/http/httputil.ReverseProxy |
|
rdx
Package httprouter is a trie based high performance HTTP request router.
|
Package httprouter is a trie based high performance HTTP request router. |
|
sqlx
Package reflectx implements extensions to the standard reflect lib suitable for implementing marshalling and unmarshalling packages.
|
Package reflectx implements extensions to the standard reflect lib suitable for implementing marshalling and unmarshalling packages. |