- Stream[T]: Send(v T) type-safe; message type recovered from the instantiated Send signature (reflect has no type-args API) - method signatures reduced to (error), (T, error), (*Stream[T], error); the (T, *Stream, error) first-message form is gone — send it via Send - return T and stream message T now run checkType at bind time (Stream[any] remains the explicit untyped escape hatch) - generated clients: TS onMessage: (data: T); Go <-chan T - fix vacuous panic assertions (fmt.Sprint(nil) -> "<nil>") in hardening/route test catch helpers Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
34 lines
1.6 KiB
Markdown
34 lines
1.6 KiB
Markdown
# fun
|
||
|
||
基于 [fasthttp](https://github.com/valyala/fasthttp) 的单端点 RPC 框架。业务请求统一走
|
||
`POST /cell`,按 `ServiceName.MethodName` 反射调用;自带依赖注入、Guard 鉴权、
|
||
NDJSON 流式响应、自定义路由与 TypeScript 客户端生成。
|
||
|
||
## 特性
|
||
|
||
- **单端点 RPC**:`POST /cell`,方法签名 `(error)`、`(T, error)`、`(*Stream[T], error)`
|
||
- **依赖注入**:`fun.Wired[T]()` 建单例,`auto` 标签字段递归装配,`New()` 初始化连接资源
|
||
- **Guard 鉴权**:全局 + 服务级中间件,panic 兜底转统一错误响应
|
||
- **NDJSON 流式**:`*fun.Stream[T]` 逐行推送,消息类型由类型实参声明
|
||
- **自定义路由**(v1.3.0+):`BindRoute` 注册 GET/POST 回调、健康检查、通配符路径
|
||
- **请求体上限控制**(v1.3.3+):`SetBodyLimit` 支持大体积 multipart 上传
|
||
- **CORS 跨域**(v1.3.5+):`f.CORS(origins ...string)` 白名单按需放行,预检 204 直答,覆盖 `/cell` 与全部自定义路由
|
||
- **TypeScript 客户端生成**:`BindServiceForGen` + `GenCode(fun.GenTs{})`,免基础设施即可生成,产物带 `result<T>` 归一化错误与拦截器
|
||
|
||
## 快速开始
|
||
|
||
```go
|
||
func main() {
|
||
f := fun.GetFun()
|
||
f.BindService(&UserSvc{}) // 服务结构体嵌入 fun.Ctx,导出方法即 RPC 端点
|
||
|
||
cfg := fun.Wired[config.Config]()
|
||
go f.Start(cfg.ListenPort()) // fasthttp 监听,RPC 只响应 POST /cell
|
||
}
|
||
```
|
||
|
||
## 文档
|
||
|
||
完整使用文档见 [docs/README.zh.md](docs/README.zh.md):DTO 规则、线协议与 Result、
|
||
Guard、自定义路由、流式响应、TS 客户端生成与常见坑。
|