Documentation
¶
Overview ¶
Package types 定义框架中通用的类型和接口,解决循环导入问题
Index ¶
- Constants
- type AuthInfo
- type CacheStorage
- type ContextKey
- type ErrorHandler
- type FileStreamer
- type H
- type HandlerFunc
- type JWTManager
- type ListResponse
- type Pagination
- type RequestContext
- type ResourcePool
- type Response
- type ResponseWriter
- type SSEClient
- type SSEEvent
- type SSEHub
- type UploadConfig
- type Validator
Constants ¶
View Source
const ( SuccessCode = 200 // 成功状态码 FailCode = 400 // 失败状态码 ErrorCode = 500 // 错误状态码 ForbiddenCode = 403 // 禁止访问状态码 NotFoundCode = 404 // 资源不存在状态码 )
常量定义
View Source
const ( JWTHeaderKey = "Authorization" JWTQueryKey = "token" JWTCookieKey = "token" JWTPrefix = "Bearer " )
JWT常量
View Source
const ( MethodAny = "ANY" // 任意方法 MethodGet = "GET" // GET 请求 MethodHead = "HEAD" // HEAD 请求 MethodPost = "POST" // POST 请求 MethodPut = "PUT" // PUT 请求 MethodPatch = "PATCH" // PATCH 请求 (RFC 5789) MethodDelete = "DELETE" // DELETE 请求 MethodConnect = "CONNECT" // CONNECT 请求 MethodOptions = "OPTIONS" // OPTIONS 请求 MethodTrace = "TRACE" // TRACE 请求 )
常用 HTTP 方法常量
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthInfo ¶
type AuthInfo struct {
UserID string `json:"user_id,omitempty"`
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
Roles []string `json:"roles,omitempty"`
Extra H `json:"extra,omitempty"`
}
AuthInfo 表示认证用户的基础信息
func (AuthInfo) HasAnyRole ¶
HasAnyRole 判断是否包含任意一个角色
type CacheStorage ¶
type CacheStorage interface {
Set(key string, value interface{}, duration ...time.Duration)
Get(key string) (interface{}, bool)
Delete(key string)
Has(key string) bool
Keys() []string
Clear()
}
CacheStorage 缓存存储接口
type ErrorHandler ¶
type ErrorHandler interface {
HandleError(ctx RequestContext, err error)
}
ErrorHandler 错误处理器接口
type FileStreamer ¶
type FileStreamer interface {
Process(filePath string, processor func(chunk []byte, offset int64) error) error
ProcessReader(reader http.Request, processor func(chunk []byte, offset int64) error) error
}
FileStreamer 文件流处理器接口
type HandlerFunc ¶
type HandlerFunc interface{}
HandlerFunc 定义处理函数类型(前向声明,实际类型在主包中定义) 这里只是为了避免循环导入,实际使用时会被主包的类型覆盖
type JWTManager ¶
type JWTManager interface {
GenerateToken(payload map[string]interface{}) (string, error)
ValidateToken(token string) (map[string]interface{}, error)
ParsePayload(token string) (map[string]interface{}, error)
RevokeToken(jti string, expirationTime time.Time) error
IsTokenRevoked(jti string) bool
}
JWTManager JWT管理器接口
type ListResponse ¶
type ListResponse struct {
Data interface{} `json:"data"`
Pagination Pagination `json:"pagination"`
}
ListResponse 通用列表响应结构体
type Pagination ¶
type Pagination struct {
CurrentPage int `json:"current_page"`
PageSize int `json:"page_size"`
TotalCount int64 `json:"total_count"`
TotalPages int `json:"total_pages,omitempty"`
}
Pagination 分页信息结构体
type RequestContext ¶
type RequestContext interface {
context.Context
// 基础方法
Method() string
Host() string
URL() string
GetHeader(key string) string
GetIP() string
// 参数获取
Param(key string, defaultValue ...string) string
ParamInt(key string, defaultValue ...int) int
Query(key string) string
PostForm(key string) string
// 响应方法
Success(data interface{}, url ...string)
Fail(msg string, url ...string)
Error(msg string, url ...string)
JSON(code int, obj interface{})
// 缓存方法
CacheSet(key string, value interface{}, duration ...time.Duration)
CacheGet(key string) (interface{}, bool)
CacheDelete(key string)
// JWT方法
SetJWT(token string, maxAge int)
GetJWT() string
// 文件上传
FormFile(name string) (*multipart.FileHeader, error)
}
RequestContext 定义请求上下文接口
type ResourcePool ¶
type ResourcePool[T any] interface { Get() (T, error) Put(resource T) Close() Len() int Stats() (active, idle int) }
ResourcePool 资源池接口
type Response ¶
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
URL string `json:"url,omitempty"`
}
Response 统一响应结构
type ResponseWriter ¶
type ResponseWriter interface {
http.ResponseWriter
JSON(code int, obj interface{})
Abort()
}
ResponseWriter 定义响应写入器接口
type SSEEvent ¶
type SSEEvent struct {
Event string `json:"event"`
Data interface{} `json:"data"`
ID string `json:"id"`
Retry int `json:"retry"`
}
SSEEvent SSE事件结构
type SSEHub ¶
type SSEHub interface {
RegisterClient(client SSEClient)
UnregisterClient(client SSEClient)
Broadcast(event *SSEEvent)
GetClients() []string
Close()
}
SSEHub SSE中心接口
type UploadConfig ¶
type UploadConfig struct {
AllowedExts []string // 允许的文件扩展名
MaxSize int64 // 最大文件大小(字节)
SavePath string // 保存路径
}
UploadConfig 文件上传配置
Click to show internal directories.
Click to hide internal directories.