httpsrv 是一个轻量级、net/http 原生的 Go Web 框架。它提供 Fiber v3 风格的路由接口(App/Router/Ctx/Handler),完全基于标准库从零实现。
- net/http 原生:典型处理函数为
func(Ctx) error,同时也接受标准库的http.Handler,可与 Go 生态直接互通 - radix 树路由:按方法分树、
{param}与{*catchAll}路径参数、大小写敏感、静态段优先于参数优先于 catch-all(与注册顺序无关) - 中间件与分组:
Use使用 fiber v3 风格中间件(func(Ctx) error+c.Next()),全局或按前缀作用域;Group(prefix)共享前缀 - 静态文件:
middleware/static子包提供New(root, ...Config),返回httpsrv.Handler:app.Get("/*", static.New("./public"))或app.Get("/*", static.New(".", static.Config{FS: embedFS}));支持目录或嵌入式文件系统 - 模板渲染:
html/template一次解析,Ctx.Render(name, bind, layouts...) - i18n(可选):扁平的 locale 消息存储,
AcceptLanguage中间件(经x/text做 BCP-47 匹配) - 压缩(可选):
middleware/compress子包提供New(config...)(按Accept-Encoding选 gzip/brotli,brotli 优先) - 优雅服务:安全的默认超时,
Shutdown(ctx)
完整指南(中文):doc/zh-CN/,快速开始 · 路由 · 分组 · 中间件 · 服务 · 静态文件 · Ctx 与 Handler · 模板渲染 · i18n · 示例 · 目录
英文文档:doc/
go get -u github.com/hooto/httpsrv/v2package main
import (
"github.com/hooto/httpsrv/v2"
)
func main() {
app := httpsrv.New()
app.Get("/", func(c httpsrv.Ctx) error {
return c.SendString("hello httpsrv")
})
app.Run(":8080")
}运行并访问:
$ go run .
$ curl http://localhost:8080/
hello httpsrv可运行示例见 examples/(hello、i18n、分组/静态/模板)。
httpsrv 保持核心简洁,以下是一些推荐使用的第三方库:
- hlog4g - 日志库
- hini4g - INI 配置文件解析
- hflag4g - 命令行参数处理
- hlang4g - i18n 国际化
- hcaptcha4g - 验证码生成
更多 Go 生态库可参考:awesome-go
- Go 版本: 1.26 或更高
- 推荐系统: Linux、Unix 或 macOS
httpsrv 的 API 接口(App/Router/Ctx/Handler)参考了 Fiber v3,基于 net/http 的从零实现,不依赖 fiber。