API 速查
fun v1.3.6 全部导出符号一览,按主题分组。点击左侧目录跳转。
Fun 实例
go
func GetFun() *Fun // 获取全局单例(内部懒初始化)
func New() *Fun // 新建独立实例(代码生成等场景)注册(均须在 Start 前)
| 方法 | 说明 |
|---|---|
BindService(service any, guardList ...Guard) error | 注册服务 + 服务级 Guard |
BindServiceForGen(service any) | 生成期专用:只登记元信息,不装配依赖 |
BindGuard(guard Guard) error | 注册全局 Guard |
BindRoute(method, path string, handler RouteHandler, guardList ...Guard) error | 注册自定义路由(可通配 /prefix/*) |
服务器
| 方法 | 说明 |
|---|---|
Start(port uint16) | 监听 :port(阻塞) |
StartOn(addr string) | 监听完整地址 |
Shutdown(ctx context.Context) error | 优雅关停 |
SetTimeouts(read, write, idle time.Duration) | 超时三件套 |
SetMaxConcurrency(n int) | 最大并发 |
SetBodyLimit(n int) | 请求体上限(默认 4MB;≤0 恢复默认) |
CORS(origins ...string) | 跨域白名单 |
Ctx(服务内嵌)
go
type Ctx struct {
Ip string
State map[string]string
MethodName string
ServiceName string
Data *map[string]any
RequestCtx *fasthttp.RequestCtx
}Guard
go
type Guard interface {
Guard(ctx Ctx) error // nil 放行;error 短路(fun.Error 可带码)
}Result 与错误
go
type Result[T any] struct {
Code *uint16 `json:"code,omitempty"`
Data *T `json:"data,omitempty"`
Msg *string `json:"msg,omitempty"`
Status uint8 `json:"status"`
}
func Error(code uint16, msg string) error // 业务错误:status=2,code/msg 透传status:0 成功 · 1 框架错误 · 2 业务错误 · 4 外部请求失败/取消(客户端) · 5 外部超时(客户端)。
Stream
go
type Stream struct{ /* ... */ }
func (s *Stream) Send(message any) error // 推一条消息;断开/已关闭返回 error
func (s *Stream) Close() // 结束流(必须调用)
func (s *Stream) OnClose(cb func()) // 注册清理回调自定义路由
go
type RouteHandler func(ctx *RouteCtx) error // nil=已写响应;error=统一 Result
type RouteCtx struct {
RequestCtx *fasthttp.RequestCtx
Data map[string]string // 查询 + urlencoded 表单(表单优先)
Wildcard string // /prefix/* 匹配的剩余路径
}
func (c *RouteCtx) Param(name string) string依赖注入
go
func Wired[T any]() (*T, error) // 按类型建/取单例:先注入 fun:"auto" 字段,再调 New()New() 约束:无参、无返回。
日志
go
func ConfigLogger(log Logger)
type Logger struct {
Level uint8 // PanicLevel 0 ~ TraceLevel 5,默认 Trace 全输出
Mode uint8 // TerminalMode / FileMode
MaxSizeFile uint8 // MB
MaxNumberFiles uint64
ExpireLogsDays uint8
LogFilePath string // 默认 "../log"
}
func DebugLogger(message ...any)
func InfoLogger(message ...any)
func TraceLogger(message ...any)
func WarnLogger(message ...any)
func ErrorLogger(message ...any)
func PanicLogger(message ...any)代码生成
go
func SetOutput(path string) // 输出目录,默认 "./gen";⚠️ GenCode 会清空整个目录
func GenCode(genList ...Gen) // 依次运行生成器
// 内置生成器
fun.GenGo{} // → <out>/go/
fun.GenTs{} // → <out>/ts/TS 客户端(生成产物)
ts
api.create(url: string): defaultApi // 聚合入口,请求 POST ${url}/cell
c.setState(state) // 全局 state
c.addRequestInterceptor((svc, m, state, dto) => void)
c.addResponseInterceptor((svc, m, result, context) => result | void)
// context: { requestState, response? }
type RequestOptions = { signal?: AbortSignal; state?: Record<string, string> }
type StreamOptions = { signal?: AbortSignal; state?: Record<string, string>;
retry?: StreamRetryOptions | false; idleTimeoutMs?: number }
type StreamRetryOptions = { maxAttempts?: number; baseDelayMs?: number; maxDelayMs?: number }