Documentation
¶
Index ¶
- Constants
- Variables
- func BindAll(c *gin.Context, obj any, bindType BindType) error
- func BindAndValidate(c *gin.Context, obj interface{}) error
- func BindJSONAndValidate(c *gin.Context, obj interface{}) error
- func BindQueryAndValidate(c *gin.Context, obj interface{}) error
- func FromCtx(c *gin.Context) string
- func GetValidator() *validator.Validate
- func Handle[Req any, Rsp any](fn func(c *gin.Context, req Req) (Rsp, error), cb ...cbZeroIO) gin.HandlerFunc
- func HandleAny(fn any, cb ...cbZeroIO) (gin.HandlerFunc, error)
- func HandleNoResp[Req any](fn func(c *gin.Context, req Req, cb ...cbZeroIO) error) gin.HandlerFunc
- func LocalFile(root string, indexes bool) *localFileSystem
- func RegErrMsg(code int, fn func(lang string) string)
- func RegErrMsgStatic(code int, kvs ...string)
- func RegisterMessages(msgs map[int]map[string]string)
- func Serve(urlPrefix string, fs ServeFileSystem, isGroupPath ...funcIsGroupPath) gin.HandlerFunc
- func ServeRoot(urlPrefix, root string) gin.HandlerFunc
- func SetDefaultSuccessCode(code int)
- func SetupValidator()
- func ValidateStruct(obj any) error
- func ValidateStructWithLang(obj any, lang string) error
- type APIHandler
- type ApiEndpoint
- type ApiGroup
- type ApiMethod
- type BindType
- type Config
- type EmbeddedFileConfig
- type Endpoint
- type EndpointBuilder
- func (b *EndpointBuilder) Build(apiHandlers ...APIHandler) ApiGroup
- func (b *EndpointBuilder) DELETE(path string, handler interface{}, options ...EndpointOption) APIHandler
- func (b *EndpointBuilder) GET(path string, handler interface{}, options ...EndpointOption) APIHandler
- func (b *EndpointBuilder) GROUP(path string, handlers []APIHandler, options ...GroupOption) APIHandler
- func (b *EndpointBuilder) HEAD(path string, handler interface{}, options ...EndpointOption) APIHandler
- func (b *EndpointBuilder) OPTIONS(path string, handler interface{}, options ...EndpointOption) APIHandler
- func (b *EndpointBuilder) PATCH(path string, handler interface{}, options ...EndpointOption) APIHandler
- func (b *EndpointBuilder) POST(path string, handler interface{}, options ...EndpointOption) APIHandler
- func (b *EndpointBuilder) PUT(path string, handler interface{}, options ...EndpointOption) APIHandler
- type EndpointOption
- type Error
- type GroupOption
- type PageData
- type RegistResult
- type RouterGroup
- type Rsp
- type ServeFileSystem
- type Server
- type ServerOption
- func AddApiGroup(group ...ApiGroup) ServerOption
- func AddRouteGroup(prefix string, routes func(*gin.RouterGroup)) ServerOption
- func WithAutoRegister() ServerOption
- func WithBasePath(basePath ...string) ServerOption
- func WithCORS(allowOrigins ...string) ServerOption
- func WithEmbeddedFiles(urlPath string, embeddedFS embed.FS, fsRoot string) ServerOption
- func WithHealthCheck() ServerOption
- func WithMode(mode string) ServerOption
- func WithPProf() ServerOption
- func WithPort(port int) ServerOption
- func WithStaticFiles(urlPath, dirPath string) ServerOption
- func WithSwagger(swaggerOption ...cyswag.RegisterOption) ServerOption
- func WithVersionInfo() ServerOption
- type StaticFileConfig
Constants ¶
const ( ErrCodeInternal = 101001 ErrCodeNotFound = 101404 ErrCodeInvalidParam = 201001 )
---------- 错误码 ----------
const ( LangEN = "en" LangZH = "zh" )
支持的语言
const INDEX = "index.html"
Variables ¶
var ( BuildVersion = "dev" BuildTime = "unknown" )
==================== 全局变量(用于注入版本)====================
Functions ¶
func BindAndValidate ¶
BindAndValidate 绑定请求并验证
func BindJSONAndValidate ¶
BindJSONAndValidate 绑定 JSON 并验证
func BindQueryAndValidate ¶
BindQueryAndValidate 绑定查询参数并验证
func Handle ¶
func Handle[ Req any, Rsp any, ](fn func(c *gin.Context, req Req) (Rsp, error), cb ...cbZeroIO) gin.HandlerFunc
通用版本:可返回数据
func HandleNoResp ¶
func RegErrMsgStatic ¶
RegErrMsgStatic registers static messages for multiple languages Usage: RegErrMsgStatic(10001, "zh", "用户不存在", "en", "User not found")
func RegisterMessages ¶
RegisterMessages registers multiple error codes at once Usage:
cygin.RegisterMessages(map[int]map[string]string{
10001: {"zh": "登录失败", "en": "Login failed"},
})
func Serve ¶
func Serve(urlPrefix string, fs ServeFileSystem, isGroupPath ...funcIsGroupPath) gin.HandlerFunc
Static returns a middleware handler that serves static files in the given directory. For non-existent routes, it returns the index.html file to support SPA routing.
func ServeRoot ¶
func ServeRoot(urlPrefix, root string) gin.HandlerFunc
func SetDefaultSuccessCode ¶
func SetDefaultSuccessCode(code int)
func ValidateStructWithLang ¶
ValidateStructWithLang 验证结构体并返回国际化错误消息
Types ¶
type APIHandler ¶
type APIHandler interface {
RegistRouter(basePath string, g *gin.RouterGroup) *RegistResult
}
type ApiEndpoint ¶
type ApiEndpoint struct {
Path string
Method ApiMethod
Summary string
Description string
Tags []string
Handler any
Middleware []gin.HandlerFunc
}
func (*ApiEndpoint) RegistRouter ¶
func (ae *ApiEndpoint) RegistRouter(basePath string, g *gin.RouterGroup) *RegistResult
type ApiGroup ¶
type ApiGroup struct {
BasePath string
Description string
Tags []string
Middleware []gin.HandlerFunc
APIHandler []APIHandler
}
func (*ApiGroup) RegistRouter ¶
func (ag *ApiGroup) RegistRouter(basePath string, g *gin.RouterGroup) *RegistResult
type BindType ¶
type BindType = int
const ( BindTypeNone BindType = 0 BindTypeUri BindType = 1 << 1 BindTypeQuery BindType = 1 << 2 BindTypeForm BindType = 1 << 3 BindTypeJson BindType = 1 << 4 BindTypeHeader BindType = 1 << 5 BindTypeFile BindType = 1 << 6 BindTypeAll BindType = BindTypeUri | BindTypeQuery | BindTypeForm | BindTypeJson | BindTypeHeader | BindTypeFile )
type Endpoint ¶
type Endpoint struct {
// contains filtered or unexported fields
}
Endpoint 创建一个基础端点
func (Endpoint) WithDescription ¶
WithDescription 设置端点描述
func (Endpoint) WithSummary ¶
WithSummary 设置端点摘要
type EndpointBuilder ¶
type EndpointBuilder struct {
// contains filtered or unexported fields
}
EndpointBuilder 是一个用于构建 API 端点的辅助函数集合
func NewEndpointBuilder ¶
func NewEndpointBuilder(basePath string, description string, tags []string) *EndpointBuilder
NewEndpointBuilder 创建一个新的端点构建器
func (*EndpointBuilder) Build ¶
func (b *EndpointBuilder) Build(apiHandlers ...APIHandler) ApiGroup
Build 构建一个API组
func (*EndpointBuilder) DELETE ¶
func (b *EndpointBuilder) DELETE(path string, handler interface{}, options ...EndpointOption) APIHandler
DELETE 创建一个DELETE方法的端点
func (*EndpointBuilder) GET ¶
func (b *EndpointBuilder) GET(path string, handler interface{}, options ...EndpointOption) APIHandler
GET 创建一个GET方法的端点
func (*EndpointBuilder) GROUP ¶
func (b *EndpointBuilder) GROUP(path string, handlers []APIHandler, options ...GroupOption) APIHandler
func (*EndpointBuilder) HEAD ¶
func (b *EndpointBuilder) HEAD(path string, handler interface{}, options ...EndpointOption) APIHandler
HEAD 创建一个HEAD方法的端点
func (*EndpointBuilder) OPTIONS ¶
func (b *EndpointBuilder) OPTIONS(path string, handler interface{}, options ...EndpointOption) APIHandler
OPTIONS 创建一个OPTIONS方法的端点
func (*EndpointBuilder) PATCH ¶
func (b *EndpointBuilder) PATCH(path string, handler interface{}, options ...EndpointOption) APIHandler
PATCH 创建一个PATCH方法的端点
func (*EndpointBuilder) POST ¶
func (b *EndpointBuilder) POST(path string, handler interface{}, options ...EndpointOption) APIHandler
POST 创建一个POST方法的端点
func (*EndpointBuilder) PUT ¶
func (b *EndpointBuilder) PUT(path string, handler interface{}, options ...EndpointOption) APIHandler
PUT 创建一个PUT方法的端点
type EndpointOption ¶
type EndpointOption func(*ApiEndpoint)
EndpointOption 用于配置端点的可选参数
func WithDescription ¶
func WithDescription(description string) EndpointOption
WithDescription 设置端点描述
func WithMiddleware ¶
func WithMiddleware(middleware ...gin.HandlerFunc) EndpointOption
WithMiddleware 设置端点中间件
type Error ¶
type Error struct {
Code int // Read-only: error code
Details []string // Read-only: detailed context
Status int // Read-only: HTTP status
}
---------- 错误结构 ----------
func (*Error) MarshalJSON ¶
MarshalJSON customizes JSON output (used in c.JSON)
func (*Error) WithDetail ¶
WithDetail returns a new Error with detail
type GroupOption ¶
type GroupOption func(*ApiGroup)
func WithGroupDescription ¶
func WithGroupDescription(description string) GroupOption
func WithGroupMiddleware ¶
func WithGroupMiddleware(middleware ...gin.HandlerFunc) GroupOption
func WithGroupTags ¶
func WithGroupTags(tags ...string) GroupOption
type RegistResult ¶
type RegistResult struct {
// contains filtered or unexported fields
}
func (*RegistResult) Merge ¶
func (r *RegistResult) Merge(other *RegistResult) *RegistResult
type RouterGroup ¶
type RouterGroup interface {
Group() ApiGroup
}
type ServeFileSystem ¶
type ServeFileSystem interface {
http.FileSystem
Exists(prefix string, path string) bool
}
func EmbedFolder ¶
func EmbedFolder(fsEmbed embed.FS, targetPath string) ServeFileSystem
type Server ¶
type Server struct {
Engine *gin.Engine
Config *Config
// PathPrefixFilter 存储需要保护的API路径前缀
PathPrefixFilter map[string]bool
ApiGroups []ApiGroup
// contains filtered or unexported fields
}
==================== Server 结构体 ====================
func (*Server) AddPathPrefix ¶
AddPathPrefix 添加需要保护的API路径前缀 当使用WithEmbeddedFiles注册根路径时,这些前缀将不会被静态文件处理器处理
type ServerOption ¶
type ServerOption func(*Server)
func AddApiGroup ¶
func AddApiGroup(group ...ApiGroup) ServerOption
func AddRouteGroup ¶
func AddRouteGroup(prefix string, routes func(*gin.RouterGroup)) ServerOption
AddRouteGroup 添加路由分组
func WithBasePath ¶
func WithBasePath(basePath ...string) ServerOption
func WithEmbeddedFiles ¶
func WithEmbeddedFiles(urlPath string, embeddedFS embed.FS, fsRoot string) ServerOption
WithEmbeddedFiles 提供内嵌静态文件服务 使用方法: //go:embed assets/* var assets embed.FS server := cygin.NewServer(cygin.WithEmbeddedFiles("/assets", assets, "assets"))
func WithMode ¶
func WithMode(mode string) ServerOption
==================== Gin Mode 控制 ====================
func WithStaticFiles ¶
func WithStaticFiles(urlPath, dirPath string) ServerOption
WithStaticFiles 提供静态文件服务
func WithSwagger ¶
func WithSwagger(swaggerOption ...cyswag.RegisterOption) ServerOption
WithSwagger 启用 Swagger 文档