gst

package module
v0.0.0-...-1a2ba69 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Ask DeepWiki

gst

gst 是一个面向 Go 后端项目的快速开发框架。它的核心使用方式是: 开发者在业务项目中编写 model DSL,随后用 gg 生成应用入口、路由注册、 model 注册和 service 注册,再在 service 中补充业务逻辑。

当前仓库是 gst 框架源码仓库。业务项目应通过 gg new 创建;不要在 gst 框架仓库根目录对业务项目运行 gg gen、gg migrate 等生成命令。

前后端接口对接(请求位置、通用查询参数、响应结构)以 API_CONTRACT.md 为准。

快速开始

安装 gg

业务项目直接安装发布版:

go install github.com/hydroan/gst/cmd/gg@latest

如果需要基于当前源码验证 gg 命令,可以在 gst 源码仓库安装本地版本:

make install
创建业务项目
gg new github.com/example/myapp
cd myapp
cp config.ini.example config.ini

gg new 会使用 module path 的最后一段创建项目目录,例如上面的目录名是 myapp。它会生成基础目录、main.go、config.ini.example 和各注册文件的空版本 (与在空项目上执行一次 gg gen 的结果相同,因此第一次 gg gen 留到写下第一个 model 之后),并执行 go mod tidy 和 git init;项目已经在某个 git 仓库里(例如 克隆下来的仓库,或大仓库里的子目录)时跳过 git init,不建嵌套仓库。

已经有项目目录(例如刚克隆下来的空仓库)时,进入该目录,在 module path 后面加 .,项目会直接生成在当前目录;当前目录名必须与 module path 的最后一段一致:

cd myapp
gg new github.com/example/myapp .

无论哪种写法,目标目录里只要已经有 gg new 要生成的目录或文件(如 model/、 main.go、.gitignore、go.mod),就报错退出,不写入任何文件;README.md、 .git 这类其他内容原样保留。

如果 go mod tidy 因网络、代理、本地 Go 缓存权限等原因失败,已生成的项目文件 通常仍保留在项目目录中。进入项目后修复环境并重新执行下面的命令;项目已经在 git 仓库里时,省略 git init:

go mod tidy
git init
了解生成目录

新项目的常用目录含义如下:

路径 责任
model/**/*.go 声明数据结构、接口 DSL、轻量 model hook
service/**/*.go 实现业务逻辑、复杂 hook、查询过滤和返回补充
service/**/*_test.go 与 service 文件同时由 gg gen 生成的测试骨架,含每个包的 main_test.go;生成后归项目维护,gg gen 只同步 TestMain 文件对 pb、interceptor 两个包的空白导入
module/ 注册内置或自定义模块,例如 IAM
configx/ 扩展配置
cronjob/ 注册定时任务(每个调度时刻整个部署只领一次,被打断的一轮再跑一次)
leader/ 注册常驻任务(任一时刻整个部署只有一个副本在跑)
lock/ 声明锁(一件事同一时刻只做一次)
component/ 注册常驻组件(每个副本各跑一份、跑到进程结束)
middleware/ 注册中间件
interceptor/ 注册 gRPC 拦截器;只有声明了 GRPC() 模型的项目才有
pb/ 由 gg gen 从声明了 GRPC() 的模型推导的 .proto 和 Go 代码,提交进仓库;已提交的 .proto 决定字段编号,模型改动会破坏它时 gg gen 报错并写明在 .proto 里改哪一行来接受这次破坏(删掉的编号用 reserved 留住),不要删整个文件;模型不再走 gRPC 时 gg gen 直接删掉它的 Go 文件,.proto 留给 gg prune;别处来的 .proto、.pb.go、.gen.go(如自己用 protoc 编的存根)不要放进 pb/,它们没有 gst 的生成头,gg gen、gg prune 要删它们时会报错停下
router/router.gen.go 由 gg gen 生成的路由注册文件
model/model.gen.go 由 gg gen 生成的模型注册文件
model/apidoc.gen.go 由 gg gen 生成的注释与枚举注册文件,让 Swagger 文档在无源码的部署环境仍带字段说明和枚举值
service/service.gen.go 由 gg gen 生成的 service 注册文件
main.go 由 gg gen 生成的应用入口

通常只手写 model/**/*.go、service/**/*.go 和扩展目录中的业务代码。生成文件 不要手改;如果生成结果不符合预期,先检查 model DSL,再重新执行 gg gen。

开发主线

日常开发按这个顺序走:

  1. 在 model/**/*.go 中声明资源模型或动作模型。
  2. 每次修改 DSL 后运行 gg gen;生成完它会执行 go mod tidy,把生成代码引用的依赖收进 go.mod 和 go.sum(项目在 go.work 工作区里时不跑,见下文工作区一段)。
  3. 在生成的 service/** 文件中实现业务逻辑或 hook,并把同名 _test.go 里的骨架换成接口测试 (骨架被换掉之前 go test 会失败)。
  4. 使用 gg check 检查项目结构和依赖边界。
  5. 删除 model 或关闭 action 后,运行 gg prune 清理 model 不再需要的文件;它只动 service/、middleware/、interceptor/ 和 pb/。

模型 DSL

model 是业务项目的主要输入。先判断当前接口属于哪一种模式。

数据库资源

普通资源使用 model.Base。如果这个资源需要建表或迁移,声明 Migrate()。

框架按固定的列名读写 model.Base/model.AutoBase 的字段和 tenant.Scope 的租户列:id、created_by、updated_by、created_at、updated_at、deleted_at、tenant_id。不要改这些列名:重新声明这些字段时加 column tag,或者嵌入 tenant.Scope 时加前缀,启动期都会报错退出,gg check 也会拒绝。

created_by、updated_by 由框架写成请求调用者的 id;客户端请求里带来的这两列,在业务钩子运行之前就被清掉。上下文里没有调用者时(后台任务、匿名请求),写入保留模型里的值,所以后台任务和业务钩子可以自己设置。

索引一律通过模型的 Indexes() []model.Index 方法集中声明:Fields 写 Go 字段名、顺序即列序,Unique 声明唯一索引,索引名由框架统一生成(idx_ 或 uniq_ 加表名加列名,超过方言上限的截短后带哈希:PostgreSQL 最长 63,MySQL 与 SQLite 64)。唯一的例外是主键——它由 model.Base/model.AutoBase 内置声明,业务模型不写主键。不要用 gorm tag 的 index/uniqueIndex/unique 配置索引,gg check 会拒绝。被索引的字符串字段要写列宽(如 gorm:"size:36"):没有长度的字符串在 MySQL 上是 longtext 列,MySQL 不能给它建索引,配置为 mysql 的项目 gg check 会拒绝,启动期与 gg migrate 也在建表之前拒绝。

Unique 是「没删的记录之间唯一」:软删除的记录不占它的值,删掉之后同样的值可以再建,已删的记录之间可以重复;把已删的记录恢复回来时值已被别的记录占用,返回 409。这种索引在 MySQL 上要求 8.0.13 及以上;在 PostgreSQL 和 SQLite 上它是带条件的索引,自己写 ON CONFLICT (列) 点名它当冲突目标时要带上同样的条件 WHERE deleted_at IS NULL(gorm 的 clause.OnConflict 用 TargetWhere),数据库才认得它,不点名目标的 ON CONFLICT DO NOTHING 和框架的 Upsert 不受影响。

字段的四种 tag 各管一件事:json 管请求体与响应的序列化,gorm 管数据库列,query 管前端查询参数,binding 管请求值的校验。校验规则只认 binding tag(如 binding:"required,max=64",写法同 go-playground/validator),请求不满足时返回 400 并点名是哪个字段;写在 validate tag 里的规则不会执行,gg check 会拒绝。前端能按哪些字段过滤、排序、做游标翻页,由 query tag 声明,参数名就是 tag 的值(下例的 user_id、title);没写 query tag 的字段不是查询参数,前端传了返回 400。

客户端不该写的字段(由别的动作或后端逻辑写的状态、时间)在 binding 里声明 readonly,如 Stage string 写成 `json:"stage" binding:"readonly"`。框架默认的新建、全量更新、部分更新(含批量,HTTP 与 gRPC 相同)都不采用客户端给它送的值,也不因此报错:

  • 新建:在业务钩子运行之前清掉,钩子里赋的值会写入。
  • 部分更新:请求点了名也不改,保持库里的值,钩子里赋的值会写入。
  • 全量更新:不写这一列,请求送没送都保持库里的值,响应回的是库里的值;钩子里给它赋的值也不会写入,要在全量更新时改它就在钩子里自己写库。

它只对走框架默认流程的模型自己的字段生效:写在自定义 XXXReq 的字段或嵌套结构体内部的字段上不起作用,service 自己调用 database 写库也不受它限制。接口文档的请求体里不列只读字段。

package model

import (
	. "github.com/hydroan/gst/dsl"
	"github.com/hydroan/gst/model"
)

// Record is a database-backed resource.
type Record struct {
	UserID string `json:"user_id" query:"user_id"`
	Title  string `json:"title" query:"title"`

	model.Base
}

func (Record) TableName() string { return "records" }

func (Record) Design() {
	Migrate()
	Endpoint("records")
	Param("rec")

	Create(func() {
		Service()
	})
	Patch(func() {
		Service()
	})
	List(func() {
		Service()
	})
	Get(func() {})
}

这个模型会生成类似下面的路由:

  • POST /api/records
  • PATCH /api/records/:rec
  • GET /api/records
  • GET /api/records/:rec

Param("rec") 控制单资源路由中的参数名。未声明 Param(...) 时,单资源路由默认 使用框架默认参数。Route(...) 里的参数段一律写 :name,路由只认这一种写法;写成 {name} 的 gg gen 直接拒绝。

需要自增整数主键的资源改用 model.AutoBase,字段和默认 hook 与 model.Base 一致,区别是 ID 由数据库在插入时分配(框架不会生成)。注意:这类模型通过 model.Register 注入 seed 记录时必须显式指定 ID 或依赖唯一索引,否则重复启动会 重复插入;Base 字符串 ID 的逗号分隔多 ID 查询写法对整数 ID 不适用。

查询参数名

查询参数名只来自 query tag,json 名和数据库列名都不参与。嵌入结构体时按下面的规则命名:

  • 匿名嵌入不占一层: 没有字段名的嵌入(如 model.Base)把字段提升到外层,直接用字段自己的 tag,查询名是 id、created_at,不是 base.id;业务自己的结构体按指针匿名嵌入也一样。
  • 按名字嵌入用点连起来: 字段带 gorm:"embedded" 的结构体,gorm 把它的字段摊成本表的列;这一列的查询名是路径上每一步的 query tag 用 . 连起来。外层 First Part 写 query:"first"、Part 里的 Label 写 query:"label",查询名就是 first.label:裸键 ?first.label=x、运算符 ?first.label[like]=x、排序 _sort_by=first.label desc、游标 _cursor_field=first.label 都用它,gRPC 的 Filters 也写这个名字。路径上任何一步没写 query tag(或写 query:"-"),这一列就不是查询参数。
  • json:"-" 一票否决: 路径上任何一步带 json:"-",这一列对前端整体不可见,写了 query tag 也不能过滤、排序,传了返回 400。
  • 裸键只给标量: 字符串、数字、布尔这类字段收裸键精确匹配;时间、日期、JSON 这类列只收运算符写法(?expires_at[gte]=…),而且运算符只对嵌了 model.Query 的模型生效。框架时间戳 created_at、updated_at 例外:嵌了 model.Query 的模型上,它们的裸键也收,按精确匹配处理(?created_at=2026-01-02T15:04:05Z),值同样只收 RFC 3339。
  • 存成一整列的结构体没有点路径: datatypes.JSON 一类类型或 gorm:"serializer:json" 存成一列的结构体、切片字段只有它自己的查询名,不能点进里面的字段;JSON 文档列(datatypes 的 JSON 类型,或写了 type:json)只收 like 一类运算符和 isnull。
  • 不落库的字段只收裸键: gorm:"-" 的字段写了 query tag 仍是查询参数,裸键解码进模型交给服务的 Filter 钩子使用,不能用运算符、不能排序。
  • 写错直接报错: 两个字段写同一个查询名、业务字段的查询名用 _ 开头(框架参数的保留前缀)、query tag 里带 [、] 或 .,由两处检查拦下:声明了 Migrate() 的模型,服务启动时直接报错退出;gg check 检查这些模型,以及没声明 Migrate() 但嵌了 model.Query 的模型。其余模型(如只嵌 model.Cursor 的)两处都不检查。

每个模型实际收哪些查询参数,以 /docs/index.html 的 Swagger 为准:收裸键的参数在说明里列出它可用的运算符,只收运算符的列按 字段[运算符] 逐个列出。

时间字段

时间分三种,用不同的字段类型声明,框架按类型决定它怎么存、怎么写、怎么比较;日期和时段的类型在 github.com/hydroan/gst/datatypes 包里:

含义 字段类型 库里存成 请求体与 URL 过滤的写法 gRPC
时刻:某一个瞬间 time.Time、*time.Time 各方言统一存 UTC RFC 3339,必须带时区偏移,如 2026-01-02T08:00:00+08:00 google.protobuf.Timestamp
日期:某一天 datatypes.Date、*datatypes.Date 日期列 2026-01-02,带时刻的写法不收 google.type.Date,年、月、日三个数
时段:一天里的几点几分 datatypes.TimeOfDay、*datatypes.TimeOfDay 时间列,MySQL 上不保留小数秒 15:04:05,可带小数秒,17:30:00.5 是 17:30 过半秒 google.type.TimeOfDay,时、分、秒、纳秒四个数
  • 写法不对或超出范围的值答 400(gRPC 答 InvalidArgument),并点名字段;不带时区的时间和 Unix 时间戳一律不收。
  • 响应里时刻写成 RFC 3339,日期写成 2026-01-02;时段写成 17:30:00,有小数秒时写满九位(17:30:00.500000000)。
  • 日期列一律声明成 datatypes.Date,可空的用 *datatypes.Date;给 time.Time 加 gorm:"type:date" 来声明日期列,服务启动时报错退出,gg check 也会拦下。服务代码里 datatypes.Date(t) 取的是 t 在它自己时区里的那一天,要按 UTC 取就写 datatypes.Date(t.UTC());按日期列过滤用生成的列引用,值也给 datatypes.Date。
  • 两个日期用 Equal、Before、After、Compare 比先后,用 AddDays、DaysSince 加减和数天数,都只看是哪一天;不要用 ==,它连造出这个日期的钟点和时区一起比。
  • URL 过滤对这三种字段只支持比较(eq、ne、gt、gte、lt、lte)和 isnull,前端的完整写法见 API_CONTRACT.md。
  • 写库时模型里每个 time.Time、*time.Time 字段都统一转成 UTC、截到毫秒,并写回内存里的模型,所以 time.Now() 写进去的值和回读的一样;框架自己写的 created_at、updated_at、deleted_at 同样。可空的时间用指针:非指针的零值时间在 MySQL 上写不进去,答 500。
  • 定时任务的表达式按 UTC 解释,按别的时区算要写 CRON_TZ= 前缀;分析查询按时间分桶也按存储的 UTC 切。要按运营时区切日,在查询里自己换算。
JSON 字段

存成一列 JSON 的字段,用 github.com/hydroan/gst/datatypes 包的三种类型声明:

含义 字段类型 怎么用
JSON 数组 datatypes.JSONSlice[T] 就是 []T,直接下标、遍历、append;datatypes.NewJSONSlice(s) 把一个 []T 转过来
JSON 对象,键事先不知道 datatypes.JSONMap 就是 map[string]any,直接按键读写
一个值存成一份 JSON 文档 datatypes.JSON[T] datatypes.NewJSON(v) 包起来,.Data() 取出来;要原样保存的文档声明成 datatypes.JSON[json.RawMessage]
  • 三种数据库上都建成 JSON 列(PostgreSQL 上是 jsonb);请求和响应里就是数组、对象和 T 自己的 JSON,gRPC 上分别是 repeated 字段、google.protobuf.Struct 和 T 对应的消息。
  • 元素是字符串的 datatypes.JSONSlice[string] 列可以按「数组里包含某个值」过滤,其余 JSON 列的过滤见上面「查询参数名」。
  • 项目代码里不要用 gorm.io/datatypes 的 Date、Time、JSON、JSONMap、JSONType、JSONSlice,也不要用构造它们的 NewTime、NewJSONType、NewJSONSlice,请求和响应类型里也一样:gg check 逐处拦下并写明该换成哪个名字,用它们声明的列在服务启动时还会报错退出。两个包都叫 datatypes,留意 import 的是框架的那个;这个库的其余部分(JSON 路径查询等)照常可用。
自定义动作

不直接表示数据库表的接口优先使用 model.Empty,并为当前接口单独定义自己的 XXXReq、XXXRsp。两个接口不要写同一个类型名,gg check 会拦下;字段完全一样时写别名 type XXXGetRsp = XXXRsp。

package tool

import (
	. "github.com/hydroan/gst/dsl"
	"github.com/hydroan/gst/model"
)

// Entry is a non-database action model.
type Entry struct {
	model.Empty
}

// EntryPair is one key/value pair submitted for merging.
type EntryPair struct {
	Key   string `json:"key"`
	Value string `json:"value,omitempty"`
}

// EntryMergeReq is the request for merging entries.
type EntryMergeReq struct {
	Entries []EntryPair `json:"entries"`
}

// EntryMergeRsp is the response returned after merging.
type EntryMergeRsp struct {
	Entries []EntryPair `json:"entries"`
}

func (Entry) Design() {
	Route("/entries/merge", func() {
		Create(func() {
			Service("merge")
			Payload[*EntryMergeReq]()
			Result[*EntryMergeRsp]()
		})
	})
}

这个接口会生成 POST /api/entries/merge,并生成 service/tool/entry/merge.go。Service("merge") 给动作起名:service 文件叫 merge.go、service 类型叫 Merge,模型声明了 GRPC() 时 rpc 名是它加模型名(MergeEntry), 避免同一个 model 内多个 Create action 都生成 create.go。

路由和可见性
  • Endpoint("records") 定义默认资源路径,只写一段:开头的斜杠会被去掉,其余位置带斜杠 (如 Endpoint("archive/records"))时 gg gen 报错,多段路径用 Route。
  • Route("/archive/documents", func() {...}) 定义额外路径或完全自定义路径。
  • Public() 表示公开接口,不走认证中间件;默认不写则需要认证。
  • Exact() 表示当前 action 按声明路径原样注册,不追加默认的 /:id、/batch 等后缀。 用在单条资源动作(Get、Update、Patch、Delete)上就是去掉末尾的 /:id:例如 PATCH /api/records/:id 变成 PATCH /api/records,改哪一行由自定义 service 从请求体或当前用户判断;所以这时至少要声明 Payload[T]()、Result[T]() 其中一个(Get 不能有请求体,要声明 Result[T]()),动作才交给自定义 service, 否则 gg gen 报错。
  • Payload[T]() 定义请求体类型;Result[T]() 定义响应体类型。声明了其中任何一个的 action 要同时写 Service():这两个类型是自定义 service 方法的类型,漏写时 gg gen 报错。
  • List、Get 是 HTTP GET 接口,没有请求体,禁止声明 Payload[T](); 只声明 Result[T]() 即成为自定义动作,生成的请求类型固定为 *model.Empty, 查询参数通过 ctx.Query()、路径参数通过 ctx.Param() 读取。
  • Import、Export 的 service 方法签名固定(Import(ctx, io.Reader) 读上传文件、 Export(ctx, ...M) 返回附件字节),禁止声明 Payload[T]() 和 Result[T]()。
  • Service() 表示当前 action 需要生成并注册业务 service,默认按动作命名 (Create 生成 create.go、类型 Creator);Service("merge") 同时给它起名, service 文件和 service 类型用这个名,rpc 名是它加模型名。
  • 只声明 Create(func(){})、List(func(){}) 等 action 就会启用对应接口; 不需要的接口不声明即可。
流与 WebSocket

要在一条长连接上来回发消息(聊天、实时推送、分批上传),用 Stream 动作。它在 HTTP 上就是一条 WebSocket 连接:路由用 GET 握手打开,之后一帧文本是一条 JSON 消息。用它不需要开 gRPC;模型另外声明了 GRPC() 时, 同一个动作同时是带 stream 的 rpc,service 代码还是那一份。

// Tally answers every amount the client sends with the total so far.
type Tally struct {
	model.Empty
}

// TallySumReq is an amount to add to the total.
type TallySumReq struct {
	Amount int64 `json:"amount"`
}

// TallySumRsp is the total once an amount was added.
type TallySumRsp struct {
	Total int64 `json:"total"`
}

func (Tally) Design() {
	Stream(func() {
		Public()
		Service("sum")
		StreamingPayload[*TallySumReq]()
		StreamingResult[*TallySumRsp]()
	})
}

Stream 必须用 Service("name") 起名,不能写 Exact()。每一侧要么是一条消息(Payload、Result), 要么是一串消息(StreamingPayload、StreamingResult),至少有一侧是一串:

声明 谁发什么 service 方法
Payload 加 StreamingResult 客户端发一条请求,服务端发一串 Stream(ctx, req, stream *gst.ServerStream[RSP]) error
StreamingPayload 加 Result 客户端发一串,服务端最后回一条 Stream(ctx, stream *gst.ClientStream[REQ]) (RSP, error)
StreamingPayload 加 StreamingResult 两边各发各的 Stream(ctx, stream *gst.BidiStream[REQ, RSP]) error

service 方法一返回,连接就关闭:返回 nil 是正常关闭,返回错误按下面的关闭码关。想让连接一直开着,就在方法里 循环:stream.Recv() 返回 io.EOF 表示客户端说它发完了;客户端直接走了或服务在停机时,Recv、Send 返回错误,方法照常返回即可。

func (s *Sum) Stream(_ *gst.ServiceContext, stream *gst.BidiStream[*model.TallySumReq, *model.TallySumRsp]) error {
	var total int64
	for {
		req, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			return nil
		}
		if err != nil {
			return gst.NewErrorWithCause(http.StatusBadRequest, "failed to read the amount", err)
		}
		total += req.Amount
		if err := stream.Send(&model.TallySumRsp{Total: total}); err != nil {
			return gst.NewErrorWithCause(http.StatusInternalServerError, "failed to send the total", err)
		}
	}
}

前端或别的客户端照下面的约定连:

  • 握手是 GET /api/<路由>,和普通请求一样先过中间件。握手被拒时答的是普通的 JSON 错误;浏览器的 WebSocket 读不到这次失败的状态码,只会看到连接失败(关闭码 1006)。
  • 一帧文本是一条 JSON 消息,不套响应信封;单条最大 4 MiB,不接受二进制帧。
  • 客户端发完自己这一侧时发一帧 null,之后还可以继续收。
  • 怎么结束看关闭码:1000 是正常结束;4000 加 HTTP 状态是 service 返回的错误,gst.NewError(404, ...) 就是 4404,关闭原因里是提示文字,某条消息没通过解码或校验是 4400;4500 是服务端自己的故障; 1001 是服务在停机,过一会儿重连。
  • 心跳由框架每 15 秒发一次,客户端不用处理。

浏览器里:

const ws = new WebSocket("wss://api.example.com/api/tallies");
ws.onmessage = (event) => console.log(JSON.parse(event.data).total);
ws.onclose = (event) => console.log(event.code, event.reason);
ws.onopen = () => ws.send(JSON.stringify({ amount: 2 }));

Go 里用 client 包,接口测试也这样写,gg gen 给新的流生成的测试骨架就是这个形状:

stream, err := cli.BidiStream[model.TallySumReq, model.TallySumRsp](ctx, "/api/tallies")
if err != nil {
	return err
}
defer stream.Close()
if err = stream.Send(&model.TallySumReq{Amount: 2}); err != nil {
	return err
}
rsp, err := stream.Recv() // rsp.Total 是 2

三点要留意:

  • 跨源:页面和服务同源时直接能连。页面在别的站点上时,把它的源写进 [server] 节的 websocket_origins (环境变量 SERVER_WEBSOCKET_ORIGINS,如 *.example.com,多个用逗号隔开),否则握手答 403; 模式写坏了启动时直接报错。
  • 认证与权限:握手就是一次普通的 GET,没声明 Public() 的流照常过认证中间件。权限不按 GET 判,按「STREAM 加路径」判: 这和 gRPC 上同一个动作认的是同一条策略,授权模块的路由清单里流也列在 STREAM 下,只授了 GET 的角色开不了流。 会话放在 Cookie 里的项目(iam 模块),浏览器握手时会自动带上;浏览器的 WebSocket 带不了自定义请求头, 所以靠 Authorization 请求头认证的项目(middleware.JwtAuth)在浏览器里握手会被拒,程序里用 client 包不受影响。
  • 多副本:一条连接接在哪个副本上,就一直在哪个副本上,框架只管这条连接上的收发。要让连在别的副本上的客户端 也收到消息,得经副本共用的东西送过去,比如 Redis 频道;副本停机时它的连接收到 1001,客户端重连就落到别的副本上。 做法和核对步骤见 examples/cluster 的「WebSocket」一章。

完整示例见 examples/demo/model/tally.go 和它的 service、测试。

gRPC

模型顶层声明 GRPC(),它就同时走 gRPC:gg gen 从 Go 类型和 Design() 推导 .proto,进程内 编译出 pb/ 下的 Go 代码,并在 gRPC 服务上注册和 HTTP 路由相同的动作,业务代码只写 service/ 那一份。 字段用 pb tag 给消息编号(pb:"11",1 到 10 留给框架:model.Base 的字段,以及嵌入 tenant.Scope 时的 tenant_id),缺的 gg gen 会补上。 只定义了 MarshalText、没有 MarshalJSON 的类型(netip.Addr、实现了它的枚举)在消息里是 string,按它写出的文本收发,和 JSON 里一样。 Stream 动作(见上一节)在声明了 GRPC() 的模型上同时是带 stream 的 rpc,和它的 WebSocket 连接由同一份 service 代码提供。 测 gRPC 这一侧时 testutil.GRPCTarget() 给出测试服务器的 gRPC 地址;测试二进制没导入 pb 包、一个 gRPC 服务都没注册时,它直接 panic 并点名要加的导入,gg gen 每次运行都会给声明 TestMain 的测试文件补上这个导入。完整示例见 examples/demo/model/board。

监听在 [grpc] 节配置,环境变量是 GRPC_PORT 这样的写法:listen、port(默认 8081,挨着 HTTP 的 8080)、 tls_enabled、cert_file、key_file(默认明文,和 HTTP 监听一样交给前面的入口终止 TLS)、reflection (默认开,grpcurl 能直接列出服务)。其余键就是 grpc-go 的服务端参数,不填就用 grpc-go 自己的默认值: keepalive_time、keepalive_timeout(服务端多久没动静就 ping 客户端、等多久没回音就断开,默认 2 小时、20 秒); keepalive_min_time、keepalive_permit_without_stream(客户端两次 ping 最少隔多久、没有调用时能不能 ping,默认 5 分钟、不能, ping 得更勤的客户端会被 too_many_pings 断开,客户端的 keepalive 要照这两个值配);max_connection_age、max_connection_age_grace (一条连接活多久就让客户端重连、在途的调用再宽限多久,默认不限;经 Kubernetes Service 这类按连接分流的入口,长连接不换就一直落在老副本上,靠它换到新副本); max_recv_msg_size(收多大的消息,写 8MiB 这样带单位的值或裸字节数,空或 0 用 grpc-go 的默认 4MiB 即 4,194,304 字节,更大的消息答 ResourceExhausted;MiB 按 1024 进、MB 按 1000 进,4MB 只有 4,000,000 字节,比默认值还小)。 时长键不能是负数,keepalive_time、max_connection_age 填了就至少 1 秒,max_recv_msg_size 最多 2GiB;写错的值和读不出来的证书都让启动直接报错退出,没有模型声明 GRPC() 的项目也一样检查。 没有模型声明 GRPC() 的项目不开这个端口。监听上还有两个框架自带的服务:标准的健康服务 grpc.health.v1.Health,进程在服务时答 SERVING,收到停机信号后和 /-/readyz 同时变成 NOT_SERVING, 每个服务按全名也各报一份、和整体同起同落,Kubernetes 的 gRPC 探针指名 service 时问的就是它; 停机延迟过后两个监听一起关闭,在途的流在那一刻以 Unavailable 结束、客户端据此换副本重连,一元调用照常排空;以及反射服务。认证在 interceptor/ 里挂,和 middleware/ 一一对应: interceptor.RegisterAuth(interceptor.IAMSession()) 之后,每个没声明 Public() 的 rpc 都要在 authorization 元数据里带 Bearer <会话 id>;会话绑定登录时的 User-Agent,程序要拿会话调 gRPC,登录时得带和 gRPC 客户端 一样的 User-Agent(grpc-go 默认是 grpc-go/<版本>),浏览器登录的会话在 gRPC 上会被拒;健康与反射服务不经过项目挂的任何拦截器, Register 的通用拦截器和 RegisterAuth 的认证拦截器都不经过。多副本下的用法和核对步骤见 examples/cluster 的「gRPC 与认证」一章; 两条传输线怎么从声明、生成、注册、链路一路接到同一份 service 代码,见 TRANSPORTS.md:它只画架构全景, 不展开细节,细节以本文各节和代码为准。

业务 Service

生成后的业务逻辑主要写在 service/**。service 通常嵌入:

service.Base[M, REQ, RSP]

其中 M 是模型类型,REQ 是请求类型,RSP 是响应类型。

业务项目只需要关注 DSL、生成代码里的 router.Register / service.Register,以及 业务实现里的 service.Base。业务代码不需要依赖更底层的框架执行包;手写高级路由时 也应通过 router.Register 接入。

需要特别注意默认资源和自定义动作的 service 写法不同:

  • 默认资源 CRUD:当 M、REQ、RSP 是同一个类型时,框架会执行默认 数据库流程,业务侧主要实现 CreateBefore、CreateAfter、ListAfter、 Filter 等 hook。
  • 自定义动作:当 Payload 或 Result 让 REQ、RSP 不同于 M 时, 框架会调用 service 的 Create、List、Delete 等 action 方法。

自定义 List 动作接管整个请求,框架不再替它解析查询参数。这类 service 应该用 service.Base 上的 QueryXxx 方法把参数还原成查询条件,而不是自己重写一遍解析, 这样接口的筛选和分页行为与框架驱动的列表完全一致。解析针对的模型来自 Base 的 类型参数 M,不用也不能由调用方另外指定:

query, err := s.QueryModel(ctx)
if err != nil {
	return nil, gst.NewError(http.StatusBadRequest, err.Error())
}
query.TenantID = tenant // 客户端影响不了的强制条件

filters, err := s.QueryFilters(ctx)
if err != nil {
	return nil, gst.NewError(http.StatusBadRequest, err.Error())
}
opts := gst.QueryOptions{
	AllowEmpty:    true,
	PresentFields: s.QueryPresentFields(ctx),
	Filters:       filters,
}
cursor, err := s.QueryCursor(ctx)
if err != nil {
	return nil, gst.NewError(http.StatusBadRequest, err.Error())
}
orders, err := s.QueryOrders(ctx)
if err != nil {
	return nil, gst.NewError(http.StatusBadRequest, err.Error())
}
items := make([]*appmodel.Sample, 0)
if err = database.Database[*appmodel.Sample](ctx).
	WithQuery(query, opts).
	WithCursor(cursor).
	WithOrder(orders...).
	WithPagination(s.QueryPagination(ctx)).
	List(&items); err != nil {
	return nil, gst.NewErrorWithCause(http.StatusInternalServerError, "failed to list samples", err)
}

这六个方法覆盖 field[op] 过滤、零值过滤、排序、分页和 cursor 分页:分页每个 model 都有, 排序与 field[op] 过滤要 model 嵌 model.Query,cursor 要嵌 model.Cursor。WithPagination 的每一页都按 确定的顺序返回:排序列里没有主键(或不可为 NULL 的唯一列)时,框架在 ORDER BY 末尾补主键,方向 跟随最后一个排序项(不带 _sort_by 时按主键升序),翻页不漏不重,索引照常可用。QueryOrders 会校验 _sort_by 的排序列,未知列名直接报错;但它们不保证 List 与 Count 用同一份查询 条件:统计总数时必须传入同样的查询值和 gst.QueryOptions(把 opts 存成变量复用是 最省事的写法),否则 total 会和当页数据对不上。

cursor 分页自带 ORDER BY,首页也是:不带 _cursor_value 的请求按游标列升序返回第一页,之后用 末行的值接着翻。嵌了 model.Cursor 的 model,框架的列表接口只按 cursor 翻页,传 _page 或 _sort_by 直接返回 400;自己写的列表用 QueryCursor 读游标时同样如此,它会返回这个错误,照上面的写法 答 400 即可;Database 链上 WithCursor 与 WithOrder 同时出现也会报错。

OR 查询

QueryOptions 里的条件默认全部 AND 组合。需要 OR 时用 gst.FilterOr 构造分组, 组内条件之间 OR,分组整体仍与其他条件 AND——因此权限隔离这类强制条件不可能被 OR 掉:

Filters: []gst.Filter{
	appmodel.SampleCols.TenantID.Eq(tenant),      // 强制条件,始终 AND
	gst.FilterOr(                               // 一个搜索词横跨多列
		appmodel.SampleCols.Name.Like(keyword),
		appmodel.SampleCols.Code.Like(keyword),
	),
}
// WHERE tenant_id = ? AND (name LIKE ? OR code LIKE ?)

Like 区分大小写。搜索词要不分大小写就换成 ILike(URL 上是 字段[ilike]=值):保证不分大小写的是 英文字母 a 到 z,其他文字分不分随数据库而定。

条件用 gg gen 为模型生成的列引用来写(上例的 appmodel.SampleCols),列名和值类型都在 编译期校验;拿不到具体模型的泛型工具函数用自己的类型参数现造列引用,例如 gst.NewColumn[M, string]("id").In(ids...),值类型同样在编译期校验。

gst.FilterAnd 用于在 OR 组内嵌套 AND,配合出 (a AND b) OR (c AND d):

Filters: []gst.Filter{
	appmodel.SampleCols.TenantID.Eq(tenant),
	gst.FilterOr(
		gst.FilterAnd(
			appmodel.SampleCols.Kind.Eq(KindPrimary),
			appmodel.SampleCols.Status.Eq(StatusDone),
		),
		gst.FilterAnd(
			appmodel.SampleCols.Kind.Eq(KindSecondary),
			appmodel.SampleCols.Status.Eq(StatusPending),
		),
	),
}
// WHERE tenant_id = ? AND ((kind = ? AND status = ?) OR (kind = ? AND status = ?))

分组可任意嵌套,深度不设限。FilterAnd 在顶层是合法但冗余的(等价于把子条件铺平 写)。空分组会收敛成空结果集而不是匹配全部:分组没有子条件一定是调用方写错了。

分组只能由服务端构造,URL 不提供该能力:?field[or]= 这类写法会返回 400, 客户端无法自行改变条件的组合方式。

需要一个恒为空集的条件时,例如权限钩子里拒绝当前用户看到任何行,用 gst.FilterFalse():它和其他过滤器一样是真实条件,渲染成 1 = 0。

默认资源的 hook 示例:

package record

import (
	"net/http"

	appmodel "github.com/example/myapp/model"

	"github.com/hydroan/gst"
	"github.com/hydroan/gst/service"
)

type Creator struct {
	service.Base[*appmodel.Record, *appmodel.Record, *appmodel.Record]
}

func (c *Creator) CreateBefore(ctx *gst.ServiceContext, record *appmodel.Record) error {
	if record.Title == "" {
		return gst.NewError(http.StatusBadRequest, "title is required")
	}
	return nil
}

自定义动作的 service 示例:

package entry

import (
	"github.com/example/myapp/model/tool"

	"github.com/hydroan/gst"
	"github.com/hydroan/gst/service"
)

type Merge struct {
	service.Base[*tool.Entry, *tool.EntryMergeReq, *tool.EntryMergeRsp]
}

func (m *Merge) Create(ctx *gst.ServiceContext, req *tool.EntryMergeReq) (*tool.EntryMergeRsp, error) {
	index := make(map[string]int, len(req.Entries))
	rsp := &tool.EntryMergeRsp{}

	for _, pair := range req.Entries {
		if i, ok := index[pair.Key]; ok {
			rsp.Entries[i] = pair // 同一个 key 以后出现的值为准
			continue
		}
		index[pair.Key] = len(rsp.Entries)
		rsp.Entries = append(rsp.Entries, pair)
	}
	return rsp, nil
}

查询和写库优先使用:

database.Database[*appmodel.Record](ctx)

并按需要组合 WithQuery、WithSelect、WithPagination、WithOrder、 WithLimit 等选项。一次查询或写入使用一个新的 database.Database[T](...) 链式调用,不要在无关操作之间复用同一个 database 句柄。

分析查询(Select)

看板和报表用 database.Select[M, R](ctx, 投影项...),不要把整表 List 进内存再用 Go 循环累加:分组聚合、窗口函数、联合、连接都从它出发。M 决定表、软删除范围和方言, R 是自己声明的结果行结构体:

type categoryTotal struct {
    Category string
    Amount   int64
    Records  int64
}

rows := make([]categoryTotal, 0)
err := database.Select[*appmodel.Record, categoryTotal](ctx,
        appmodel.RecordCols.Category.Group(), // 不带聚合函数的项即分组键
        appmodel.RecordCols.Amount.Sum(),     // 默认别名就是列名,多数情况不用写 As
        gst.Count().As("records"),
    ).
    Where(appmodel.RecordCols.TenantID.Eq(tenantID)).
    Scan(&rows)

框架从分组键推导 GROUP BY,所以 SELECT 和 GROUP BY 不可能写不一致。

项按值比较:同一个变量传两遍才是同一个项,As、Over、Where 都会造出新的项。 要在别处再引用的项先赋给变量:Having、OrderBy、Qualify、窗口的 PartitionBy 和 OrderBy、联合的排序、主查询读子投影的项,都是按项的值在投影里找同一个项,把同一个变量 传两遍最稳,改了别名的项就不再是同一个项。反过来,主查询自己能算出来的项(不带表的 gst.Count()、排名函数,主表或主查询连入模型的项)子投影也投影了同一个,而它还顶着构造时的 默认别名(列名、count、row_number 这些;没写 As 和写了默认名都算),框架分不清是谁的, 构建期报错。改法:想要主查询自己的,给它起个别名;想读子投影的,给子投影的项起个非默认别名再把它 传给主查询;两个都要就两边各起一个不同的别名。只有主查询自己也能算的项才有这一步,子投影自己 模型的项直接传即可。gst.Literal 常量无论别名都算主查询自己的,子投影投了同一个常量就报错; 要知道子投影有没有匹配到行,读它的键列,没匹配到的是 NULL。

几条会影响正确性的约定:

  • SUM 空集恒为 0(内部包了 COALESCE);AVG/MIN/MAX 空集是 NULL, 结果字段必须声明成指针,否则构建期报错。这样「没有数据」和「结果恰好 是 0」在报表上才可区分。可空列上的分组键、时间桶键和 MIN/MAX/AVG 同理: 没值的行自成一组、键是 NULL,结果字段也要是指针或 sql.Null。Where 里对这一列 写了条件就把 NULL 行排除了(IsNotNull、范围、等值都算,IsNull 除外,且不能在 FilterOr 组里),这时可以用普通字段。
  • 条件聚合复用普通过滤器,一次扫描出多列指标: Cols.Amount.Sum().Where(Cols.Status.Eq("done")).As("done_amount")。
  • As 是可选的。默认别名是列名,COUNT(*) 是 count。只有结果字段名和 列名不一致、或同一列上挂了两个度量时才需要写。
  • 别名和 R 的字段双向校验,任一侧对不上都是构建期错误,不会静默给出 一列 0。
  • 聚合规格写错一律报错(未知列、未知函数、别名对不上、Having 比较 nil 或切片等),不像客户端过滤器那样退化成空结果。
  • service 钩子里的隔离不会自动套用。模型自己声明的行规则跟着模型走:软删除和模型嵌入的 租户作用域对投影和 List 一样生效,连入的模型也在 ON 里带着。不会继承的是 List 时由 controller 跑的 service 钩子(Filter)加上的业务隔离:聚合是 service 直接 调用的,那些钩子不会执行——每个这样的条件都必须自己写进 Where。漏掉一个就会跨过钩子 划的边界聚合,且没有任何迹象。

单行结果用 ScanOne,分页报表的总组数用 Count。只问「有没有相关行」的跨表条件用 gst.FilterExists / FilterNotExists 半连接;要把另一张表的字段带进结果行,看下面的 「连接」小节,框架只放行每行最多对上一行的连接,join 到一对多子表让 SUM 静默翻倍的 那条路写不出来。子表与外层的关联列对用 子表列.EqCol(外层列) 作为谓词传入,复合键就多传几对,每一对都 必须成立;没有任何关联对的子查询直接报错。它们是普通的 Filter 算子, List/Count/Export 同样能用。

窗口函数

窗口函数让每一行保留,旁边多出一列由同组其他行算出来的值:每组最新一行、累计、 排名、上一行的值都是它。窗口用 gst.PartitionBy 开出分区,再用 OrderBy 定 组内顺序,把它交给度量或排名函数的 Over;行级投影直接把列引用传给 Select, 它们按原值投影:

// 每个租户最新一条:ROW_NUMBER() OVER (PARTITION BY tenant_id ORDER BY created_at DESC, id ASC)
rn := gst.RowNumber().
    Over(gst.PartitionBy(RecordCols.TenantID).OrderBy(RecordCols.CreatedAt.Desc())).
    As("rn")
err := database.Select[*appmodel.Record, latest](ctx, RecordCols.ID, RecordCols.TenantID, RecordCols.Amount, rn).
    Qualify(rn.Eq(1)). // WHERE 看不见窗口列,Qualify 由框架套一层派生表再筛
    Scan(&rows)

// 累计:COALESCE(SUM(amount) OVER (PARTITION BY tenant_id ORDER BY created_at ASC, id ASC
//                 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), 0)
running := RecordCols.Amount.Sum().
    Over(gst.PartitionBy(RecordCols.TenantID).OrderBy(RecordCols.CreatedAt.Asc())).
    As("running")

// 同组合计:每一行旁边带上本租户的总额,窗口不带 OrderBy 就不累计,整组一个数
// COALESCE(SUM(amount) OVER (PARTITION BY tenant_id), 0)
tenantTotal := RecordCols.Amount.Sum().Over(gst.PartitionBy(RecordCols.TenantID)).As("tenant_total")

// 排行:分组投影上开窗,窗口按度量排序,RANK() OVER (ORDER BY COALESCE(SUM(amount), 0) DESC)
total := RecordCols.Amount.Sum().As("total")
rank := gst.Rank().Over(gst.OrderBy(total.Desc())) // gst.OrderBy 是 gst.PartitionBy().OrderBy 的短写法:只排序、不分区的窗口

关键字与规则:

  • RowNumber、Rank、DenseRank 是包级函数,必须配带 OrderBy 的窗口; Lag、Lead 挂在列上,取窗内上一行、下一行的值,同样必须配带 OrderBy 的窗口, 分区两端是 NULL,结果字段必须能装 NULL。五个聚合函数加 .Over 即在窗口上算;CountDistinct 不能开窗, 分组键和时间桶也不能。
  • 决胜列由框架补:带 OrderBy 的窗口在行级投影末尾补主键升序,在分组投影补 分组键,同值行的编号和累计在每次运行都一样。Rank、DenseRank 不补,它们按定义 把并列行当同一名次;模型没有框架的 id 主键列时也不补,窗口就按写的顺序算。
  • 累计的帧固定为 ROWS:带顺序的聚合总是从分区第一行累加到当前行,不会像 SQL 默认的 RANGE 那样把同值行一起算进去。
  • 分组投影上的窗口读的是组:PartitionBy 只能用分组键或子投影的项,度量不行, 即便它读的列正是分组键;OrderBy 用分组键或已投影的度量;聚合函数在这里作用于组的度量,Sum().Over(...) 渲染成 COALESCE(SUM(SUM(amount)) OVER (...), 0),Avg、Lag、Lead 在分组投影上直接拒绝。
  • Qualify 按窗口列筛行,条件只能引用已投影的窗口项;Where、Having 留在 内层,排序、分页和 Count 作用在筛完之后的行集。
  • ScanOne 只读纯度量投影;行级投影用 Scan。既无聚合也无窗口的独立投影仍然拒绝, 那是 List 的活;作为联合分支或带连接时放行。行级投影里裸列直接传入即可,写成 .Group() 会把查询变成分组投影。
联合查询

几张表的同形态行叠成一份列表,例如收款和退款按时间统一分页,用 database.UnionAll[R]。 分支是普通的 Select,各自扫进同一个结果行 R;框架按 R 的字段顺序渲染每个分支的 SELECT 列表,按位置对齐写反的错误写不出来。列名不一致的用 列.As("...") 对齐,来源 标记用 gst.Literal 投影一列常量:

type flow struct {
    Kind   string    // 来源标记:payment 或 refund,由 Literal 写进每一行
    ID     string
    Amount int64
    At     time.Time // 付款的 paid_at、退款的 settled_at,两边都用 As 对齐到 at
}

paidAt := PaymentCols.PaidAt.As("at") // 排序还要用它,提成变量共享
payments := database.Select[*appmodel.Payment, flow](ctx,
    gst.Literal("payment").As("kind"), PaymentCols.ID, PaymentCols.Amount, paidAt).
    Where(PaymentCols.TenantID.Eq(tenantID))
refunds := database.Select[*appmodel.Refund, flow](ctx,
    gst.Literal("refund").As("kind"), RefundCols.ID, RefundCols.Amount, RefundCols.SettledAt.As("at")).
    Where(RefundCols.TenantID.Eq(tenantID))

feed := database.UnionAll[flow](ctx, payments, refunds).
    OrderBy(paidAt.Desc(), PaymentCols.ID.Desc()). // 按结果列 at、id 排
    Page(3, 20) // 第 3 页、每页 20 行
err := feed.Scan(&rows)  // 这一页
err = feed.Count(&total) // 总数

排序项按结果列名对齐,有两种写法。一是某个分支投影过的项(值相等即可,共享变量最稳),它要求 在场的分支里有一条投影了这个项;二是列名恰好等于结果列名的列引用,它只认名字,所属的表在这里 不参与判定,分支在不在场都成立(上例的 PaymentCols.ID)。As 改过的名字通常没有同名的列 引用,只能用第一种,分支会按条件增减时(比如只查一种类型就只放一条分支)就按在场的分支挑项:

settledAt := RefundCols.SettledAt.As("at") // refunds 分支投影的就是这个项
at := paidAt
if !withPayments { // 只查退款时 branches 里没有付款分支
    at = settledAt
}
feed := database.UnionAll[flow](ctx, branches...).OrderBy(at.Desc(), PaymentCols.ID.Desc())

结果列另有同名的列引用时(例如这份结果本身声明成了模型,带着生成的列引用),直接用它排也行, 分支怎么变都成立。

规则:

  • 只有 UNION ALL,不做去重的 UNION:两笔金额相同的记录会被去重版悄悄合并。 INTERSECT、EXCEPT 用 FilterExists、FilterNotExists 表达。
  • 分支可以是纯列投影,也可以分组、开窗、Qualify,各自带 Where、Having; 分支上不能写 OrderBy、Limit、Page,它们属于联合,写了构建期报错。 联合结果没有 Where,条件写进分支,每个分支用自己的索引。
  • Page 必须带 OrderBy:没有排序的行谈不上分页,每次执行的切法可能不同,Scan 直接报 ErrUnionPageWithoutOrder;只封顶的 Limit 和 Count 不受限。排序的最后一项要能区分每一行 (上例的 PaymentCols.ID),否则排序值相同的行会在页与页之间重复或漏掉。
  • 排序分页下推:带 Limit 或 Page 时框架把同样的排序和页尾行数(offset + limit)下推到每个分支, 各分支按索引只读前几十行,外层最多排 N 倍这个行数。
  • Count 是各分支计数相加,不物化任何一行,同样忽略排序分页。
  • Literal 的值只能是标识符(字母、数字、下划线,不以数字开头),内联为 'x' 而不是绑定参数,且必须 As 起别名。
  • 四种方言渲染同一条语句;ClickHouse 用 UnionAllOn,分支在同一实例上用 SelectOn 建。
连接

行里要带出另一张表的字段,例如付款单带出账户名称,用 Join:被连模型直接用它自己的 Cols 投影、过滤、分组、开窗、排序,多张表时列名自动带表名限定。只有裸列的投影也可以连接, 这是 List 跨不了的:

type paymentWithAccount struct {
    ID          string
    Amount      int64
    AccountName *string // LEFT JOIN 对不上的行这一列是 NULL,字段必须能装 NULL
}

err := database.Select[*appmodel.Payment, paymentWithAccount](ctx,
        PaymentCols.ID, PaymentCols.Amount, AccountCols.Name.As("account_name")).
    Join(gst.LeftJoin[*appmodel.Account](AccountCols.Code.EqCol(PaymentCols.Account))).
    Where(PaymentCols.TenantID.Eq(tenantID)).
    Scan(&rows)
// SELECT `payments`.`id` AS `id`, `payments`.`amount` AS `amount`, `accounts`.`name` AS `account_name`
// FROM `payments`
// LEFT JOIN `accounts` ON `accounts`.`code` = `payments`.`account` AND `accounts`.`deleted_at` IS NULL
// WHERE `payments`.`tenant_id` = ? AND `payments`.`deleted_at` IS NULL

规则:

  • 只允许每行最多对上一行的连接。ON 里的 EqCol 和常量等值必须覆盖被连模型的主键或某个 Indexes() 唯一索引,框架在构建期证明,证明不了报错。一对多从「多」的一侧出发写,或者 只问有没有就用 FilterExists;join 到一对多子表会让 SUM 静默翻倍,这条路写不出来。
  • Join 丢掉对不上的行,LeftJoin 保留它们并把被连列置 NULL;左连接来的字段必须是指针 或 sql.Null 类型,否则构建期报错。被连表的条件写在 Where 里会把 LeftJoin 没对上的行一起 过滤掉(那些行的被连列是 NULL),要保留它们就把条件写进 ON;ON 里不带表的裸列名指的是 被连模型的列。
  • 被连模型的软删条件和租户条件由框架写进 ON,List 在它上面看不到的行连接也看不到; ON 里可以再加条件,例如 AccountCols.Tier.Eq("gold");EqCol 两边先写谁都行,框架按表名分辨。
  • 分组投影里被连侧只能做分组键或 Min、Max、CountDistinct:组内每一行都对着同一 个被连行,Sum、Count、Avg 会把它按组内行数重复计入,直接拒绝。
  • 一张表只能作为一个来源:同一模型只能连一次,不能连自己,派生表连接也算在内;ClickHouse 没有唯一约束,模型连接在它上面直接报错。
  • Where 里的条件按列引用所属的表落到对应表上;引用了没连进来的表,构建期报错。列引用带着 它所属的表,这条规则在 List 那一侧同样生效:WithQuery 的过滤器、WithOrder、WithCursor 的排序和 UpdateByID 的赋值引用了别的模型,一律构建期报错,即便两个模型恰好有同名列。

一对多的关系用 JoinSelect:先把「多」的一侧按键分组成一条 Select,每个键只剩一行,再把它当 一张临时表连上来。主查询里直接传子投影的那个项,就能把它读成一列;对不上的行用 LeftJoinSelect 保留:

type tagsPerRecord struct {
    RecordID string
    Tags     int64
}
tags := TagCols.ID.Count().As("tags")
counts := database.Select[*appmodel.Tag, tagsPerRecord](ctx, TagCols.RecordID.Group(), tags)

type recordWithTags struct {
    ID   string
    Tags *int64 // 没有标签的记录对不上,这一列是 NULL
}
err := database.Select[*appmodel.Record, recordWithTags](ctx, RecordCols.ID, tags).
    Join(gst.LeftJoinSelect(counts, TagCols.RecordID.EqCol(RecordCols.ID))).
    Scan(&rows)
// SELECT `records`.`id` AS `id`, `j0`.`tags` AS `tags`
// FROM `records`
// LEFT JOIN (SELECT `record_id` AS `record_id`, COUNT(`id`) AS `tags`
//            FROM `tags` WHERE `tags`.`deleted_at` IS NULL GROUP BY `record_id`) AS `j0`
//   ON `j0`.`record_id` = `records`.`id`
// WHERE `records`.`deleted_at` IS NULL
  • 子投影必须是分组投影,ON 里的 EqCol 和常量等值要覆盖它的全部分组键,键通过子投影模型 的列引用来写;子投影自己不能带 OrderBy、Limit、Page。ON 里不能写 FilterExists: 临时表的行是子投影的组,没有可关联的行,这种条件写进子投影的 Where。
  • 子投影的分组键必须全是它自己模型的列,每列一次:它自己再连进来的表的列当不了键,主查询 没法用引用指到它;它从自己连的子投影里读来的项是临时表的列,不算键。
  • 主查询只能读子投影投影出来的项,传同一个项即可,改了别名就不是同一个项;子投影模型的 其他列不是临时表的列,ON 和 Where 里也只能用它的键列。要按子投影的度量筛行,条件写进子投影 的 Having;要筛它的行,写进子投影的 Where。子投影的项可以做主查询窗口的分区键和 排序项,行级、分组投影都行。读子投影的项,三步:子投影里把项赋给变量并 As 一个别名; 主查询的投影里再传这个变量;结果行按这个别名声明字段,用 LeftJoinSelect 时字段能装 NULL。
  • 主查询本身分组时,子投影的项会成为主查询的分组键,这只在主查询按连接列分组时才成立, 框架会检查,例如按账户分组再连每个账户的退款合计。主查询没有自己的度量时是行级读: 每条主表行带上子投影的项,时间桶只是每行的标签;要按桶汇总就加上度量。
  • 子投影读的表不能是主查询的表,也不能是已经连进来的表:临时表是通过子投影模型的列引用来 寻址的,同一张表出现两次就分不清。子投影自己再连进来的表不受此限。主查询自己能算出来的项 (不带表的 gst.Count()、排名函数,主表或主查询连入模型的项)子投影也投影了同一个、还顶着 默认别名时报 ErrDuplicateAlias,起了非默认别名的项当作读透;gst.Literal 常量无论别名都算 主查询自己的。要给每一行带上本表按某个维度的合计,用窗口 Sum().Over(PartitionBy(键)), 不用连接。子投影不能连到自己,也不能两两互连。
  • 子投影不能按时间桶分组后再连:桶是列的标签(如 2024-01-10),没有哪一列等于它,构建期报错。
  • 临时表由数据库物化,行数就是子投影的组数:条件写进子投影,物化得越少越好。
  • 派生表连接在 ClickHouse 上可用;那里的 LEFT JOIN 对不上的行也返回 NULL,框架连 ClickHouse 时开着 join_use_nulls。这是连接级的服务端设置,同一实例上业务自己写的 LEFT JOIN 对不上的行也会因此返回 NULL,而不是列的默认值。

派生表连接构建期报错时的改法:

报错 改法
ErrJoinSelectNotGrouped 子投影加分组键,Cols.X.Group()
ErrJoinSelectKey 子投影只按自己模型的列分组,每列一次
ErrJoinSelectBucketKey 不按时间桶分组后再连,同粒度的汇总改用窗口
ErrNestedSelectOrdered 子投影去掉 OrderBy、Limit、Page,它们属于主查询
ErrJoinSelectInstance 子投影用 SelectOn 开在主查询的实例上
ErrJoinNoCorrelation ON 里至少一对 EqCol 连到主查询或更早声明的来源:连到后面才声明的来源就调整声明顺序
ErrJoinNotUnique ON 用 EqCol、常量等值钉住子投影的全部分组键
ErrJoinSelectColumn 主查询只能读子投影投影出来的项,把同一个项(共享变量)再传一遍,别改它的别名,也别加 Over/Where;ON 和 Where 里只能用它的键列,别的条件写进子投影
ErrDuplicateAlias,两个子投影投了同一个项 给其中一个 As 别的别名
ErrDuplicateAlias,主查询自己能算的项子投影也投了且还顶着默认别名 想要主查询自己的:给它起个别名;想读子投影的:给子投影的项起个非默认别名,再把它传给主查询;两个都要就两边别名各不相同
ErrDuplicateAlias,主查询和子投影投了同一个 gst.Literal 常量 常量算主查询自己的,换个值或别名;要知道子投影有没有匹配到行,读它的键列,没匹配到的是 NULL
ErrDuplicateAlias,子投影的项加了 Over/Where 再传给主查询 开窗、加条件的项不再是子投影的项:想读子投影的就原样传;主查询自己要开窗、加条件就换个别名
ErrJoinSelectNotKeyed 主查询分组时按连接列分组;连接列是另一个子投影的键时,把那个键项也投影成分组键
ErrJoinDuplicateTable 一张表只能作为一个来源,同组合计用窗口;子投影不能连到自己或两两互连
ErrJoinMeasure 分组投影里被连模型的列只能做分组键或 Min、Max、CountDistinct
ErrWindowTermNotSelected 分区键用分组键或子投影的项,窗口排序用分组键或已投影的度量

框架不做递归 CTE,聚合能力也不向 URL 暴露: 报表口径属于服务端契约,让客户端自选分组键等于开放一个无界扫描入口。

调用其他 gst 服务

client 包是调用另一个 gst 服务的官方入口,也是接口测试发请求的入口。它按 DSL 的形态设计: Design() 能声明的每种 HTTP 接口在这里都有对应的调用方式,响应按统一信封解析;Stream 在 HTTP 上是 WebSocket 连接,按它流的是哪一侧各有一个入口。

DSL 声明 client 入口
Create / Update / Patch / Delete / Get / List Post / Put / Patch / Delete / Get
Payload[*XxxReq]() 动词方法的 payload 参数
Result[*XxxRsp]() 动词方法的类型参数,从信封的 data 解码
Route("xxx/:id/action") 方法加路径组合
批量动作 BatchItems / BatchIDs 构造 /batch 路由的请求体
Export() / Import() Download / Upload
SSE() SSE,返回的事件流用 Recv 逐条取
Stream(),响应一侧是流 ServerStream
Stream(),请求一侧是流 ClientStream
Stream(),两侧都是流 BidiStream
分页、排序、展开、游标 WithPage / WithSortBy / WithExpand / WithCursor

一个客户端对应一个服务,带着地址、凭据和连接复用,可以并发使用,在包变量里建一次反复用即可; 路径、请求体和查询参数按调用传入:

cli, err := client.New("http://sample-service:8080",
	client.WithToken(token),
	client.WithTimeout(5*time.Second),
)
if err != nil {
	return err
}

// 默认 List:框架的列表参数和业务过滤都走请求选项。第一个参数是本次调用的
// 上下文,在 service 里就传 ctx 本身:调用方请求取消、超时,上游请求跟着结束
page, err := cli.Get[client.ListResult[model.Sample]](ctx, "/api/samples",
	client.WithPage(1, 20),
	client.WithQuery("status", "active"),
)
// page.Items 是这一页的记录,page.Total 是匹配到的总数(游标分页时为 0),
// page.HasMore 说明这一页之外还有没有记录,翻页翻到它为 false 为止

// 自定义动作:payload 对应 Payload,类型参数对应 Result
rsp, err := cli.Post[model.SampleSealRsp](ctx, "/api/samples/seal", &model.SampleSealReq{Reason: "audit"})

返回值分三种情况处理:调用成功得到解码后的响应;服务端拒绝(非 2xx)返回 *client.Error,带 HTTP 状态、消息和 trace_id,用 errors.As 取出后按状态决定 怎么处理;连接失败、超时这类传输错误是普通 error。需要信封本身(trace_id、响应 cookie)时用 Do。登录接口返回的 cookie 由客户端自动带到后续请求上。

两条注意事项:

  • 每个入口的第一个参数都是本次调用的上下文,它的截止时间和取消会结束这次请求。service 里传 ctx 本身,不要另起 context.Background();没有挂在请求上的调用(定时任务、常驻组件)传自己 收到的 ctx。WithTimeout 是客户端级别的兜底上限,和 ctx 的截止时间取先到者。
  • 请求带着 ctx 所属的追踪:OTEL 开启时注入 W3C 的 traceparent,并带上框架自己的 X-Trace-ID;ctx 不属于任何追踪(不在请求里,也不在定时任务或 leader 的一轮里)时两个头 都不带。上游沿用这个 trace_id,它的访问日志、SQL 注释和响应里的 trace_id 都是调用方 的那一个;client.Error 的 TraceID 也是它,排查时拿一个 id 就能把整条调用链查到底。 服务端只沿用 W3C 形态的 trace id(32 位小写十六进制、不全为零),其他值一律另生成一个, OTEL 开着关着都是这一条规则。

接口测试用的是同一个客户端:testutil.Run 启动整个应用,client.New(testutil.BaseURL()) 发请求, 上下文传 t.Context(),testutil.DecodeResp、testutil.RequireError 配合它断言,见 examples/demo/service/ping/list_test.go。

配置和迁移

config.ini.example 是新项目的默认配置模板。复制为 config.ini 后按环境修改。 默认模板会开启 sqlite,适合本地快速启动。SQLite 驱动依赖 cgo:用 CGO_ENABLED=0 构建的静态二进制照常编译, 但启用 SQLite 会在启动时报错,这类部署要换成 MySQL 或 PostgreSQL。

每个配置键都按"环境变量 > 配置文件 > 默认值"取值,配置文件里没写的键、configx 里 config.Register 注册的自定义段都一样。变量名是键名转大写、点换成下划线:server.port 对应 SERVER_PORT, logger.http_body.enabled 对应 LOGGER_HTTP_BODY_ENABLED,自定义段 Sample 的 endpoint 对应 SAMPLE_ENDPOINT。设成空串、false、0 同样生效,只有时长和 map 类型的键没有空的写法,设成空串启动失败。时长一律带单位(15s、500ms、2160h),配置文件里写成裸数字(如 YAML 的 read_timeout: 15)启动失败,只有 0 例外。启动时这几种情况直接失败:变量的值转不成对应类型 (报错写明变量名和原值,例如 SERVER_PORT=tcp://…);自定义段和框架内置段重名(比如类型叫 Mysql); 两个类型注册成同一个段;自定义段的 default 标签写错(比如 bool 字段写成 default:"yes");配置文件在框架内置段下写了框架没有的键(比如 [logger] 下写成 leve),报错点名段和键,不认识的段名不检查,自定义段按注册的类型解码;取值固定的键写了集合外的值(比如 logger.output = syslog、database.type = oracle),报错点名键、原值和可选的值,这类键不分大小写;带大小的键(logger.http_body.max_body_size、grpc.max_recv_msg_size)写带单位的值或裸字节数,其他写法启动失败;有范围的键(cache.max_entries 0 到 10,000,000,ldap.scope 0 到 2,ldap.deref 0 到 3)越界启动失败;端口、容量、大小这类不能为负的键写了负数启动失败。变量名写错、 或者只写到段名(比如 SERVER)都不会报错,也不生效,按上面的规则核对。

服务启动默认不自动建表、不自动迁移(database.auto_migrate = false):启动期只校验注册模型 的表是否存在,缺表直接报错退出并提示执行 gg migrate。gg new 生成的配置和 examples/demo 显式开启 auto_migrate = true,本地开发、测试环境按需开启即可;生产环境保持默认关闭, schema 变更一律走 gg migrate。

MySQL 默认明文连接。要加密时在 [mysql] 节打开 tls_enabled,主库、从库和 gg migrate 的连接都按这一组设置走: ca_file 是校验服务器证书用的 CA 文件,不写就用系统的根证书;cert_file、key_file 是服务器要求客户端证书时 出示的那一对,要么都写、要么都不写,只写一个启动失败;insecure_skip_verify = true 只加密、不校验服务器是谁, 不要用在生产环境。

给客户端的提示默认是英文。用户读中文的项目在 [app] 节写 language = zh(zh-CN、zh-Hans 同义)用简体中文, 写 language = zh-TW(zh-Hant、zh-HK、zh-MO 同义)用繁体中文,用词是台湾的习惯:框架、框架自带的中间件和模块 给出的提示(如 请求的资源不存在。、登录失败的 用户名或密码错误),以及字段校验的句子(如 name为必填字段), 改用这种语言,HTTP、gRPC 和 WebSocket 的关闭原因是同一句;项目自己用 gst.NewError 写的提示原样返回,用什么语言写 就返回什么语言。框架会英文(en)、简体中文和繁体中文三种,写了别的(如 ja)启动报错。点名具体查询参数或具体 字段值的提示(如排序列写错、值放不进字段)联调时才遇到,始终是英文。

gg config defaults 打印框架的默认配置:不带参数是全部,带一个节名只打印那一节(连同它下面的子节); --format 选 ini(默认,即配置文件的格式)、json、toml、yaml。内容走标准输出,要存成文件就重定向:

gg config defaults
gg config defaults server --format yaml
gg config defaults > config.ini
日志输出

logger.output 决定所有日志写到哪里,默认 stdout:

  • stdout:全局日志和访问、SQL、请求体、定时任务、选主、锁、各 provider 的日志都写进标准输出,一行一条, logger 字段标明来自哪一路(access、gorm、cronjob 等,即文件模式下的文件名去掉 .log;全局那一路取 logger.file 去掉 .log,没配时是 global)。不建文件、不轮转,Kubernetes 这类平台直接采集标准输出。
  • file:每一路写 logger.dir 下自己的文件,按 max_age、max_size、max_backups 轮转,适合由采集器读文件的部署。

项目代码用标准库写的日志也走这里:框架初始化后,slog.Default 和 log 包的默认输出都接到 app 那一路(app.log), slog.InfoContext(ctx, …) 带上请求的 ctx 时,和框架自己的日志一样带 trace_id、route、user_id 这些请求字段; slog.Group 和 WithGroup 分出来的组折成一个 JSON 字符串字段,日志库里的字段数不会随分组长大。

写标准输出和写日志文件都先缓冲,最迟 1 秒写出,停机时写完;file 模式下 logger.file 没配或配成 /dev/stdout、/dev/stderr 时,全局那一路直接写、不缓冲。值写错启动直接失败。由采集器读日志文件的部署要写 output = file(或环境变量 LOGGER_OUTPUT=file),默认的 stdout 不落文件。

模型声明 Migrate() 后,字段变化先预览迁移计划:

gg migrate --dry-run

确认无误后执行:

gg migrate

命令会生成 generated/migrate/<dbtype>/schema.sql,并在执行前要求确认。 执行前先确认 config.ini 指向目标环境,避免把开发中的模型变化迁移到错误数据库。 执行时逐条显示进度;某条语句失败时会说明是第几条、前面的语句还在不在:MySQL 每条语句单独生效, 前面的保留;PostgreSQL 和 SQLite 整份计划在一个事务里,全部回滚。

迁移只管模型声明的表。库里没有模型声明的表不删也不改,计划下面会把它们列出来。模型声明的表里, 模型没有的索引和列都在计划里删。删列会丢数据:计划下面会列出是哪几列,执行前的确认会点明要删 它们,答 y 才执行。想保留某一列,就在模型里把它声明出来。

--yes 只跳过确认,不代表同意丢数据:计划里有删列时要同时带上 --accept-data-loss,否则整份 计划都不执行:

gg migrate --yes --accept-data-loss

用 SQLite 时迁移的是 sqlite.path 指向的库文件;内存库(sqlite.is_memory = true,默认值)每次启动都按模型建表, gg migrate 提示没有可迁移的内容后直接退出。 SQLite 的 ALTER TABLE 改不了已有列的类型、非空、默认值和 CHECK,sqldef 也不会为这类变化生成语句:gg migrate 发现库里的列定义与模型不一致时直接报错,点名表和列并给出 SQLite 官方的重建步骤(按模型新建一张表、拷贝数据、 删旧表、改名),不会把这种库当成「无变化」放过。

表改名、列改名先确认

只对比模型和库,分不出「改名」和「删一个、加一个」。照删加执行,表改名会新建一张空表、数据留在 旧表里;列改名会删掉旧列连同数据。所以 gg migrate 遇到下面两种情况会当成疑似改名提出来,只在 一对一对得上时才提:

  • 库里有一张没有模型声明的表,模型要新建的表保留了它的全部列,定义也相同;
  • 同一张表少一列、多一列,两列定义相同。

直接运行 gg migrate 会逐个询问,答 y 就先执行改名语句、再重新出计划,数据都在:表是 RENAME TABLE(MySQL)或 ALTER TABLE ... RENAME TO(PostgreSQL、SQLite),列是 ALTER TABLE ... RENAME COLUMN。带 --dry-run 或 --yes 时不询问也不改名,只在计划下面列出 疑似改名和现成的语句;此时计划仍是删加,带 --yes 时其中的删列没有 --accept-data-loss 不会执行。

索引改名必须先迁移后发布

改索引名必须先执行 gg migrate 再发布新代码:先迁移则服务启动时按名命中、无额外动作。 若环境开启了 database.auto_migrate(本地开发、测试),先发布会让服务启动失败:启动期发现 同定义的索引还挂在旧名下,直接报错并给出改名语句,启动期不会 DROP 任何索引;生产环境默认 关闭 auto_migrate,先发布不会报错,但模型与库会持续漂移,直到执行 gg migrate 为止。

模型里的索引换了名字、库里同一张表上还有一个定义相同的旧名索引时,迁移计划直接是索引改名 (MySQL 是 RENAME INDEX,PostgreSQL 是 ALTER INDEX ... RENAME TO,瞬时的元数据操作); SQLite 不能给索引改名,计划是删掉再建。

内置模块

业务项目可以在 module/ 中注册内置模块。下面是注册 IAM 模块的形式:

package module

import "github.com/hydroan/gst/module/iam"

func init() {
	iam.Register()
}

应用入口会空导入 module,因此 init() 会在启动阶段执行。Register() 只注册模型和路由, 会话检查由项目自己挂:middleware/middleware.go 里 middleware.RegisterAuth(middleware.IAMSession()), 声明了 GRPC() 的项目再在 interceptor/interceptor.go 里 interceptor.RegisterAuth(interceptor.IAMSession()); 用 authz 模块时 middleware.Authz()、interceptor.Authz() 挂在它们之后(认证链按注册顺序跑);策略对象按路由清单的写法写, /api/records/{id}、/api/records/* 或静态路径两条线一致,写具体路径(/api/records/42)的只对 HTTP 生效,:id 这种写法哪边都不命中, 项目自己要读当前用户的中间件也排在会话检查之后。内置的 root 账号由 iam 模块在第一次启动时写入, 口令来自环境变量 IAM_ROOT_PASSWORD,没配就不写,已存在的不动(换口令走 iam 的接口);其余基线账号属于业务数据, 由项目在启动钩子(如 router.OnRoutesReady)里用 iam.CreateAccount 写入(用户行与口令凭据在一个事务里,写法见它的文档), 或者像 examples/demo 的测试那样走公开的 POST /api/signup 注册。

多租户

模型嵌入 tenant.Scope 就按租户隔离行:建行时框架把请求所在的租户盖进 tenant_id 列,之后这张表的每条语句都只到这个租户, service 和 hook 不写租户条件,也到不了别的租户。请求所在的租户来自上下文里的租户键 requestctx.KeyTenantID:用 iam 模块时,登录选的租户由会话检查写进去, 项目可以在它之后、middleware.Authz() 之前改写(可信网关的请求头、子域名),authz 在这个租户里判定,放行后把同一个租户交给行级隔离; 没选租户就是默认租户 default,单租户项目什么都不用配。持系统角色 system_root 的调用者不在任何一个租户里判定,请求跨全部租户, 建行时在 HTTP 请求体里点名租户(gRPC 消息里的 tenant_id 只出不进,点不了)。请求之外的代码(定时任务、启动钩子)没有授权来定租户,上下文默认在 default 租户里, tenant.In(ctx, id) 进一个租户,tenant.Across(ctx) 跨全部;建行钩子要知道行会盖上哪个租户时用 tenant.From(ctx)。

写租户模型守住两条,gg check 和启动期都会拦:嵌入 tenant.Scope 不加前缀,列名固定是 tenant_id;唯一索引的字段里要有 TenantID, 唯一性在租户内。这一列只在建行时写:整条更新(含批量)带了别的租户答 400,补丁跳过这个键。

examples/multitenant 把这些用法写成八组照着敲就能核对的场景:登录选租户、行级隔离、租户内的权限、 system root、网关头、gRPC、请求之外、工具链。

多副本部署

同一个项目可以起多个副本而不改代码。需要"整个部署只做一次"的能力都建在主库的一张租约表 gst_leases 上,不依赖 Redis 或 etcd;项目用上其中任何一种能力后,这张表随 gg migrate 一起建。

怎么选

按"这件事长什么样"选:

事情的样子 用什么
到点做,整个部署只做一次 cronjob.Register
到点做,每个副本各做一次:刷新本进程的缓存、上报本机状态 cronjob.RegisterPerInstance
一直跑的循环,同一时刻只能有一个副本在跑:轮询外部系统、转发待发消息 leader.Register
一直跑的循环,每个副本各跑一份:消费循环、监听变更 component.Register
由人或事件触发,同一时刻只能有一个在做 包变量里用 lock.New 声明,触发处调用 TryRun
结果只能发生一次:同一条记录不能处理两遍 数据库唯一约束或带状态条件的更新,上面几种都不保证

各自的用法:

  • cronjob.Register:表达式按 UTC 解释(按别的时区算要写 CRON_TZ= 前缀),@every 从 Unix 纪元起对齐、周期必须是不小于 1 秒的整秒,永不触发的表达式注册期就报错, 所有副本算出同一组时刻;每个时刻整个部署只领一次,其他副本跳过;上一轮跑超时,期间到点的时刻直接跳过, 之后也不补;启动时补跑最近一个没人领过的时刻;被打断时 fn 把 ctx 的结束原样返回,这一轮会再跑,所以 fn 必须幂等。
  • cronjob.RegisterPerInstance:每个副本按同一个表达式各跑各的,不走租约,不补跑,被打断也不再跑。
  • leader.Register:任一时刻只有一个副本在跑 fn。fn 要一直跑到 ctx 结束,中途返回(有没有错误都算)就交出 这个名字,几秒后重新竞选、从头再跑。在跑的副本停机,别的副本约 6 秒内接手;进程崩溃的,最多约 21 秒。 接手后 fn 从头跑,进度要落库。每个名字单独竞选,不同名字可能落在不同副本上,没有"查询自己是不是主"的方法。
  • lock:在包变量里用 lock.New(name) 声明,启动之后再声明会 panic;在触发的地方调用 TryRun(ctx, fn)。 只试一次不等待,被占着返回 lock.ErrHeld,接口一般回 409,定时任务跳过即可;跑一半租约丢失返回 lock.ErrLost,fn 没报错也一样,因为别的持有者可能已经开始做同一件事;先拿锁、在 fn 里开事务,在已开的 事务里(包括模型钩子)调用直接返回 lock.ErrInTransaction,因为锁会在外层事务提交前放掉;fn 的 ctx 还跟着 调用方的 ctx 结束,请求断开时锁下的工作一起停。
  • component.Register:每个副本各跑一份,不做协调;播种之后、监听之前启动,停机时先于 provider 停下,fn 收到 的 ctx 在停机时结束。ctx 结束前返回(返回 nil 也算)或 panic 都按失败退出进程,暂时性的失败要在 fn 里重试。

leader、lock、component 包的 godoc 示例(example_*_test.go)逐个演示了这些用法和容易踩的坑。examples/cluster 把定时任务、选主和锁部署成三个副本,给了在 Kubernetes 里制造故障、核对结果的命令。

部署到 Kubernetes 时要配的
  • server.shutdown_delay(默认 0):进程收到停止信号后就绪探针立刻失败,再等这么久才开始排空。 设成负载均衡发现并摘掉这个副本所需的时间(探针间隔加余量);保持 0 时,滚动发布期间仍会有新连接被送到正在关闭的副本上。
  • terminationGracePeriodSeconds:要大于框架停机的最长用时:shutdown_delay,加最多 30 秒等在途请求, 再加最多 30 秒等在途任务,开了链路追踪或调试端点的各再加最多 5 秒。Kubernetes 默认只给 30 秒, 不够时进程在收尾中途被强制结束,它持有的租约要等到期才被别的副本接手。
  • enableServiceLinks: false:Kubernetes 默认把同一命名空间里每个 Service 的地址注入成环境变量, 例如 MYSQL_PORT=tcp://10.96.0.10:3306,名字和框架的配置键 mysql.port 的环境变量相同,进程会把它当配置读,因为不是数字而启动失败。
  • 只在本进程内的能力不跨副本:sse.Hub 只推给连在本副本上的订阅者,要推给所有副本上的订阅者得自己经 Redis 这类共享通道转发, examples/cluster 第九章的通知就是这个做法;middleware/ratelimiter 的额度按副本各自计,总额度随副本数成倍。
保证与边界

框架对这几种能力保证什么、管不到什么,逐条列在仓库根目录的 LEASE_PROTOCOL.md。写业务代码时最容易 出问题的几条:

  • fn 收到的 ctx 结束时必须停下:丢租约后 5 秒还不返回,进程立即以失败退出。
  • 只有在 fn 的 ctx 上用 database.Transaction / TransactionOn 开的事务会先核对租约;普通的 Create / Update / Delete 和外部调用都不核对,外部调用要幂等,关键写入要自带条件(唯一约束、按状态更新)。
  • 不要用 context.Background() 另起 ctx 写库:框架看不见它,gg check 的「Detached context」规则会拦,model 钩子里也一样:钩子收到的 ctx 带着本次写入的事务,另起 ctx 写库在 SQLite 上会卡住整个进程。
  • 定时任务每个时刻至多跑两轮,fn 必须幂等;所有副本同时停机期间错过的调度,启动时只补最近一个。
  • 本地用 SQLite 开发时,fn 里单个事务控制在 8 秒内,连续的事务每个 5 秒内;内存 SQLite 不能用于多副本部署。
  • 副本必须直连主库或走会话级连接池;租约表的结构有变化时,先 gg migrate 再发布。

生成和检查命令

命令 用途
gg new <module path> 创建业务项目,见创建业务项目
gg gen 根据 model DSL 生成注册文件、service action 文件和它们的测试骨架
gg gen ts 生成接口收发类型的 TypeScript 声明到 generated/typescript/,供前端复制使用
gg check 检查业务项目结构、命名、依赖边界和 tag 约束
gg lint 用框架自己所用版本的 golangci-lint 检查项目代码,读项目里 gg new 写下的 .golangci.yml;不需要另外安装 golangci-lint
gg prune 清理 model 不再需要的文件,只动 service/、middleware/、interceptor/ 和 pb/,删前问一次
gg routes 按 model 的层级画出项目的 model 和它们声明的接口:方法、路径、动作、service 文件、是否公开;后面跟几个词就只留匹配的
gg migrate 生成当前数据库方言的 schema,预览并按确认执行数据库迁移
gg migrate schema [path] 只打印 model 对应的建表 SQL,可以只看某个文件或目录下的 model;标准输出只有 SQL,可以重定向到文件
gg module list 列出框架内置模块,以及各自能不能登记、能不能复制
gg module add <name>、gg module remove <name> 在项目的 module/module.go 里登记、取消登记一个内置模块,接口由框架在运行时注册
gg module copy <name> 将内置模块复制为业务项目本地源码(model、service、中间件,项目用 gRPC 时还有模块的 gRPC 拦截器),并删除框架源已移除的过时 model/service 文件(_test.go 与生成文件除外);gg module 各子命令的完整规则见 cmd/gg/MODULE.md
gg config defaults [section] 打印框架的默认配置,见配置和迁移
gg upgrade 把项目升到这个 gg 的框架版本,见升级框架

命令行写错(不存在的命令或参数)时 gg 以退出码 2 结束并指向 --help;命令跑了但失败是退出码 1。

gg check 会检查依赖边界、model/service 文件边界、命名规范、json tag、 REQ/RSP 命名、业务项目根目录结构、框架字段的列名,以及声明了 GRPC() 的 model 的 protobuf 定义(pb tag、撞名、与 pb/ 下已提交契约的兼容)和它们的 service 有没有调用 只有 HTTP 才有的 ServiceContext 方法。gg gen 生成前也会执行这些检查(protobuf 定义那条由生成本身完成,不重复跑;索引声明和框架字段列名两条要先编译项目,生成前 项目未必编得过,不在生成前跑);检查失败会停止生成。

gg check、gg gen(含 gg gen ts)和 gg routes 读项目 代码时,跳过项目 Git 忽略规则排除的路径,也跳过 Go 工具链内置忽略的路径。 后者不是 gg 自定的名单,就是 go build ./... 本来就不看的那些(见 go help packages):名字以 . 或 _ 开头的文件和目录、vendor、 testdata、自带 go.mod 的子目录,以及 go.mod 里 ignore 声明的目录。 gg prune 认项目代码的方式也一样(找 model、判断 service 目录还有没有代码 在用),但删东西时这两类规则不保护任何路径,只认 prune.ignore,见下文。

项目放在 go.work 工作区里时,gg 和 go build 一样按工作区解析依赖,不用额外设置。 go mod tidy 不读 go.work,所以 gg new、gg gen、gg module copy、gg upgrade 收尾时在工作区里不跑它, 只打印一行说明;要在工作区外构建这个项目,先在项目目录里跑一次 go mod tidy。 但工作区里 go build 会报 ambiguous import: google.golang.org/genproto/googleapis/rpc/status: 框架依赖的 cockroachdb/errors 还引用旧版的 genproto,和新版拆分出来的包重名。在 go.work 里加一行,把它指到新版即可:

replace google.golang.org/genproto => google.golang.org/genproto v0.0.0-20261012220534-4341edf3f941
升级框架

gg 和项目依赖的框架必须是同一个版本:gg 按自己的版本生成代码,两边不一致时项目命令直接报错,并给出出路 (安装项目要的那一版 gg,或者用 gg upgrade 把项目升上来)。升级按这个顺序做:

go install github.com/hydroan/gst/cmd/gg@latest
gg upgrade
gg migrate --dry-run

gg upgrade 把项目升到这个 gg 的框架版本,做的就是手工升级的那几步:go get 把 go.mod 里的框架版本改成 gg 的版本, 把项目里用 gg module copy 复制过的框架模块按新版本重新复制一遍,重新生成代码,最后执行 go mod tidy;输出只列有变化的 文件。中途哪一步失败(例如新版本多了一条检查规则,项目有代码没通过),修好后再跑一次 gg upgrade 即可。

  • 重新复制模块会覆盖项目在 model/<模块>、service/<模块> 下改过的内容,并删掉那里框架没有的文件,所以项目里有复制的 模块时先问一次,答 y 才开始,答别的什么都不改;--yes 跳过提问。
  • go.mod 用 replace 把框架指到别处,或项目在 go.work 工作区里时,项目用哪份框架由它们决定:gg upgrade 不改 go.mod 里的框架版本,也不动 replace,只把复制的模块和生成的代码对齐到那份框架。
  • 只往新版本升:gg 比项目旧时报错,并给出安装项目所需那一版 gg 的命令。
  • 数据库不归它管:升级后用 gg migrate --dry-run 看模型有没有带来表结构变化。
生成 TypeScript 类型

gg gen ts 把 model Design() 中各路由收发的 Go 类型(请求 Payload、响应 Result 与模型本身) 生成为 TypeScript 声明,写到 generated/typescript/。目录结构镜像 model 目录:model/sample 生成 sample.ts,model/sample/item 生成 sample/item.ts,model 包自身生成 model.ts,路径里不重复 model 这一层;model 之外被模型引用到的类型(例如 helper/notifier 里作为字段类型出现的结构)保留 自己的相对路径 helper/notifier.ts,一眼能看出它不来自 model。另有一个以 app.name 命名的文件(未配置时用框架名 gst.ts),声明成功响应的信封 Envelope<T>、失败响应的信封 ErrorEnvelope、默认列表结果 ListResult<T> 和默认批量请求体 ItemsPayload<T>、IDsPayload。成功还是 失败只看 HTTP 状态码:200 的响应体按 Envelope<T> 读,4xx、5xx 的按 ErrorEnvelope 读。

产物只有类型,文件之间用相对路径互相引用,不依赖任何 npm 包,也不限定请求库和前端框架:把整个 目录复制到前端项目即可使用。命令和 gg gen 一样先执行项目检查、应用 gst.yaml 的忽略规则;模型 变化后重新执行,再把目录复制给前端。

产物始终与 model 保持一致:模型删除、改名或被 gst.yaml 屏蔽后,上次为它生成的文件会被删除,空目录 一并清理;所有模型都没有了,整个 generated/typescript 目录也会消失。目录里不是 gg 生成的文件一律 不动:挡在产物路径上的直接报错,其余原样保留并在输出里提示。

类型按 encoding/json 的实际编码规则生成:

  • 字段名取 json tag;json:"-" 的字段不出现;嵌入结构体的字段展开到外层。
  • 指针、切片、map 字段写成 ?: T | null;带 omitempty 或 omitzero 的字段写成 ?:;其余字段必有。底层是切片或 map 的具名类型,声明本身就带 | null:它的 nil 值编码为 null,作为响应 Result 时 data 也可能是 null。
  • 数字一律是 number,超过 2^53 的 64 位整数在前端会丢失精度;带 ,string 选项的字段是 string。
  • 同包声明了常量的 string、整数类型生成字面量联合,常量注释写进类型注释,与 Swagger 一致;常量用 <<、| 等位运算定义的视为位标志,类型是 number;没有零值常量的枚举在使用处补 | "" 或 | 0。
  • time.Time、datatypes.Date、datatypes.TimeOfDay,以及只定义了 MarshalText、没有 MarshalJSON 的类型(netip.Addr、实现了它的枚举) 是字符串;any、json.RawMessage(单独用或放在 datatypes.JSON 里)是 unknown。时刻和日期字段的注释里带一行格式说明, Format: date-time(RFC3339 的时刻)或 Format: date(2006-01-02 的日期),格式名与 Swagger 文档一致。

JSON 形态由代码决定、无法从类型声明读出的写法,命令会列出全部位置后失败,不写任何文件:自定义了 MarshalJSON 的类型(上面列出的类型除外)、项目内的泛型类型、带方法的接口、 channel 与函数字段、键不是字符串或整数的 map,以及 encoding/json 不认的 tag 名和 tag 选项。只定义了 UnmarshalJSON 等解码方法的类型照常按自身结构生成,即服务端返回它时的形态。

运行期注册的路由同样在生成范围内:项目 module 包里用 gg module add 或手写注册的框架模块 (iam.Register() 这类),以及项目自己写的 module.Use。框架模块的类型放在把模块 gg module copy 进项目后会在的位置,例如 iam 模块的账号类型在 iam/account.ts,所以模块是注册还是复制,前端引用的 路径相同。gg module copy 复制进项目的模块和业务 model 一样会生成。

项目级配置 gst.yaml

在业务项目根目录(与 go.mod 同级)可放置可选的 gst.yaml,这是 gg 工具的 构建期工程配置,与运行时 config.ini 无关。

当前支持在 gg gen(含 gg module copy 后的重新生成)中忽略指定路由。 忽略只作用于生成的注册文件:router/router.gen.go 不注册路由、 service/service.gen.go 不注册 service,也不会为其生成新的 service 文件; 磁盘上已有的 service 文件(例如 module copy 拷贝来的)原样保留, gg prune 也不会把它们当作待删除文件,项目文件与 module copy 输出保持一致。适合屏蔽 module copy 带来的不需要的接口, 或把被框架模块占用的路径让给业务自己的实现:

gen:
  routes:
    ignore:
      /api/signup: [POST]
      /api/iam/admin/users/:id: [GET, DELETE]
      # 对象形式:from 限定只忽略声明在该目录下的 model,
      # 业务可在自己的 model 目录重新声明同一路由
      /api/iam/admin/users:
        methods: [GET]
        from: model/iam
  • 每个 path 写一次,值是要忽略的 HTTP method 列表;/api 前缀可省略, 路径可直接粘贴 gg routes 的输出。 参数段(:id)按位置匹配,不比较参数名。
  • 需要用自己的实现替换框架路由时,用对象形式加 from(如 model/iam) 把规则限定到框架模块目录;否则规则会把业务自己声明的同路径 action 一并 忽略。无 from 的规则命中多个 model 目录时会输出 warning 提醒。
  • 未匹配到任何路由的条目会在生成时输出 warning,提示配置可能已过期。
  • 忽略不影响 model 的 Migrate 注册:表结构照常创建,模块内部逻辑 (如登录查询用户表)不受影响。

gen.models.ignore 让 gg gen 不为指定的 model 生成 model.Register 注册: 这个 model 的路由、service 和生成文件照常保留,只是不再注册,表也就不再由它创建。

gen:
  models:
    ignore:
      # 键是 model 的 Go 结构体名
      Widget:
      # 对象形式:from 限定只忽略声明在该目录下的 model,
      # 别处声明的同名 model 照常注册
      Profile:
        from: model/iam

gg prune 只动 service/、middleware/、interceptor/ 和 pb/ 四个目录, 项目其他地方一个文件都不删、不改。它清理停用 action 的 service 文件连同配对的测试 文件、孤儿 service 目录、被删掉的复制模块留下的中间件和 gRPC 拦截器文件,以及 pb/ 下这次 gg gen 不会再写出的 .proto 文件、由它们编出来的 .pb.go、_grpc.pb.go 文件和对应的 .gen.go 适配文件 (model 去掉 GRPC()、model 被删或改了路径之后留下的),要删的先一次列出、问一次再删; prune.ignore 列出的路径一律跳过:

prune:
  ignore:
    - service/legacy           # 该目录及其下全部内容
    - service/record/list.go   # 只保护这一个文件
  • 每项是 service/、middleware/、interceptor/ 或 pb/ 下的一个路径(相对项目根目录),按目录层级匹配: service/legacy 覆盖该目录及其下全部内容,但不覆盖 service/legacyx; 写到具体文件就只保护这一个文件。它不是通配符,也不是正则。
  • 列出的路径在任何情况下都不会被删:停用 action 的 service 文件及其配对测试文件、 孤儿目录里的文件、清理后变空的目录、被删掉的复制模块留下的中间件和拦截器文件、 过期的 .proto 文件都算。pb/ 下的条目只保护 .proto:那里的 .gen.go、.pb.go、 _grpc.pb.go 完全由 model 推出来、每次 gg gen 重写,写成目录的条目(如 pb/legacy) 不保护它们,直接指向它们的条目报错。
  • pb/ 下要删的文件必须以 gst 的生成头 // Code generated by gst; DO NOT EDIT. 开头:后缀相同、 开头不是它的文件(如自己用 protoc 编的存根)gg gen 和 gg prune 一个都不删,列出它们后报错停下, 改名或挪出 pb/ 后重跑。
  • 要保留的路径只能写进 prune.ignore:service/ 和 pb/ 归 gg 管,要保持干净,项目 的 Git 忽略规则和 Go 工具链的内置忽略不保护任何路径。被 Git 忽略的文件、 testdata、以 _ 开头的目录等和其他路径一样按规则清理;这些位置里的代码 也不算在用 service 目录,不会让它们留下来。
  • 不在 service/、middleware/、interceptor/ 或 pb/ 下、指向 pb/ 下 Go 文件、写法不规整或重复的条目 直接报错;指向不存在路径的条目在 prune 时输出 warning。
  • gg 只读取 gst.yaml,项目里如果还有 .gg.yaml、gst.yml 这类同类 文件,会输出 warning 提示它们不会被读取。

prune 删什么、不删什么、按什么顺序删、什么时候问你,完整规则见 cmd/gg/PRUNE.md。

示例

当前仓库的 examples/demo 是推荐阅读的完整业务项目示例:

examples/bench 是压测专用项目(由 gg new 生成),提供 BENCHMARK.md 中全部压测接口。

examples/cluster 是多副本部署示例:纯 YAML 清单在 Kubernetes 里起三个副本,演示并检验定时任务、选主、锁和多副本同时建表,见它的 README。

examples/multitenant 是多租户示例:两个租户、五个账号,演示并核对登录选租户、行级隔离、租户内的权限、system root、网关头、gRPC、请求之外的租户和工具链的检查,见它的 README。

性能

框架不是瓶颈,有数据支撑(单机本地口径,完整压测协议、对照程序与 pprof 分析见 BENCHMARK.md):

  • 中间件链净开销为零:带完整中间件链(tracing、access log 落盘、body logger、CORS、recovery)的 ping 约 13.1w QPS,与同场裸 gin.New() 对照(约 12.5w)持平甚至略高。
  • 框架完整 CRUD 路径(dry run,无 DB I/O)10.5w-12w QPS:参数绑定、路由、钩子判定、SQL 构建、日志、响应序列化合计每请求不足 10µs。
  • 真实 MySQL 路径贴着往返数上限跑:单语句写(update/delete/updatebyid)3.6w-3.9w QPS 与单键读(get)同量级,每请求恰好一次 DB 往返。
  • pprof 定量实证:纯框架路径上应用层合计仅约 2% CPU,带 DB 路径上框架自有 CPU 份额 <5%、无单点热点,其余为网络 syscall、DB driver 与 runtime。

常见问题

什么时候用 model.Base,什么时候用 model.Empty?

需要数据库表、默认 CRUD、迁移和模型生命周期 hook 时使用 model.Base。只表示一个 动作、工具接口、登录跳转、批处理等非数据库接口时使用 model.Empty。

什么时候用 model.AutoBase?

数据库资源默认用 model.Base(UUIDv7 字符串主键)。写入量大、增长快、且不需要 对外暴露不可猜测 ID 的表(例如流水、明细类),可以改用 model.AutoBase 获得更窄 的自增整数主键和更小的二级索引。

什么时候需要 Service()?

默认 CRUD 没有额外业务逻辑时不需要。需要 hook、过滤、返回补充、复杂查询,或当前 action 是自定义动作时再开启 Service()。声明了 Payload[T]() 或 Result[T]() 的 action 必须写 Service(),漏写时 gg gen 报错。

为什么我写了 service 的 Create 方法但没有被调用?

如果 M、REQ、RSP 是同一个类型,默认资源 CRUD 会执行框架内置流程, 只调用 service hook 和过滤方法。要让 action 主方法被调用,需要用 Payload[T]() 或 Result[T]() 绑定当前接口专用的 REQ/RSP,让它成为自定义动作。 List、Get 只能通过 Result[T]() 触发自定义动作,请求类型固定为 *model.Empty。

Route 和 Endpoint 有什么区别?

Endpoint 是资源默认路径;Route 是额外路径或完全自定义路径。同一个 model 可以 同时声明默认资源路由和多个额外 Route。如果多个 Route 中有相同 phase 的 service,比如多个 Create,应各自 Service("name") 起名,避免生成文件冲突。

生成文件可以手改吗?

通常不要。main.go 和所有 .gen.go 文件(model/model.gen.go、 model/apidoc.gen.go、service/service.gen.go、router/router.gen.go 等) 由 gg gen 维护。手写业务逻辑放在 model/**/*.go、service/**/*.go 和扩展目录。

如何确认接口路径?

gg routes 读的是 model 的 DSL,读法和 gg gen 相同,改完 DSL 不用先生成就能看:

gg routes

输出按 model 文件的层级画成树,和接口路径的嵌套一致:model/sample/ 下的 model 挂在 model/sample.go 声明的 model 下面,没有同名文件的目录单独成一层;model/ 下的第一层各自顶格。每个 model 一行: 文件,建表的标 table,同时走 gRPC 的标 grpc;它的接口在它下面,一行一个:方法、路径、 动作、声明了 Service() 的动作的 service 文件,不需要认证的标 public,走 gRPC 的带 rpc 名。 只建表、没有接口的 model 也在树上。

后面跟几个词就只留匹配的:一个词留下「这一行或它所属 model 那一行里含有它」的接口, 不分大小写,几个词要同时满足;命中项上面的各层照样留着,标出它在哪。带 / 的词还会当成 一个真实地址,找出接它的接口:

gg routes record
gg routes public
gg routes /api/records/42/items
gg routes delete /api/records/42

gg module add 登记的框架模块在运行时注册接口,不在这个列表里,命令末尾会点名是哪些模块; 要看全部接口,启动服务后访问 Swagger 文档:

/docs/index.html
运维端点不做认证

/-/healthz、/-/readyz、/metrics、/openapi.json、/docs/* 这一类端点,以及 gRPC 监听上的 健康与反射服务,不是业务接口,不走 /api 那套认证,框架也不为它们提供口令——框架发行的默认口令保护不了任何东西,只会让人 误以为这里有防护。

保护它们是部署的事。在 Kubernetes 下这件事由拓扑决定,而不是由配置决定:指标抓取直接访问 Pod 端口,Ingress 只转发写进规则的路径——只转发 /api 前缀,这一类端点在集群外就不可达, 不需要谁额外记住一条规则。反过来,把整个端口挂到公网地址上,等于把下面这些一起发布出去:

端点 暴露什么
/-/healthz、/-/readyz 进程是否存活、是否正在停机,此外没有别的
/metrics 已被访问过的路由(gin 路由模式)及其请求数与延迟分布、缓存计数器上的数据库表名、进程内存与 CPU、构建信息;框架自己的指标名以 gst_backend_ 开头、进程指标以 gst_process_ 开头,gRPC 的是 grpc-go 生态默认的 grpc_server_
/openapi.json 本服务注册的全部路由,以及每个路由的请求模型、成功响应与会答的失败状态
/docs 同一份文档的 Swagger UI 渲染;页面资源编译进二进制,不从任何 CDN 加载脚本,离线可用
gRPC 的 grpc.health.v1.Health 与反射服务 进程和每个服务是否在服务,以及注册了哪些服务和消息;不经过项目挂的任何拦截器,Register 的通用拦截器和 RegisterAuth 的认证拦截器都不经过
为什么 gg gen 之后 go test 是红的?

gg gen 每新建一个 service 文件,就在旁边生成同名的 _test.go 骨架(外部测试包): 测试的第一行是一句 t.Fatal,下面是用框架 client 包按这个接口的方法、路由和请求、 响应类型写好的示例请求(流式动作则是用生成的 gRPC 客户端调它的 rpc);删掉第一行, 示例就是一个能跑的测试,再按业务补断言。包里还没有 TestMain 时,再生成一个只声明 TestMain 的 main_test.go,用 testutil.Run 起默认的测试 服务器(sqlite,不需要容器);需要 MySQL、Redis 或播种数据的包,在它的 testutil.Server 上改一次。这些文件生成后归项目维护;gg gen 只按 main.go 的口径同步声明 TestMain 的 测试文件对 pb、interceptor 两个包的空白导入,其余不改写;gg check 要求每个由 gg gen 生成的 service 文件都有配对的测试文件,删掉骨架而不写测试会被它拦下。

为什么删除 action 后 service 文件还在?

gg gen 默认保留已有 service 文件,避免误删手写业务代码。确认旧文件不再需要后 运行 gg prune,清理规则见 cmd/gg/PRUNE.md。

为什么测试跑完后 gst-test-mysql-8-4 这些容器还在运行?

testutil 的测试容器按镜像版本全局共享:所有测试二进制挂到同一个固定名容器上 (gst-test-mysql-8-4、gst-test-redis-7-alpine、gst-test-postgres-17-alpine、 gst-test-clickhouse-clickhouse-server-24-8-alpine),隔离在容器内部完成——每个 测试二进制拿到自己的数据库(Redis 是自己的 DB index),结束时删库/清空;进程被 强杀漏掉的残留库会在下一次测试运行时按持有进程存活状态自动回收。容器本身刻意 常驻,让后续测试免掉容器冷启动。

  • 清理或重置容器:docker rm -f <容器名>,下次测试会自动重建。修改容器启动参数 (如连接数上限)后也要先这样删掉旧容器才会生效。
  • 需要每个测试二进制独占容器时(远程 docker daemon、容器被折腾坏、需要全新实例 调试),设置 GST_TEST_DEDICATED_CONTAINERS=1 切换到独占模式,容器随测试结束销毁。

Documentation

Overview

Package gst defines the public contracts between the framework and business projects: the Model, Service, Database, Selector, Cache, RBAC, and Logger interfaces, the query building blocks they exchange (Filter, Order, Cursor, Column, Term, Window), the per-request ServiceContext, the streams the service of a Stream action reads and writes, and the Error a request is answered with.

Index

Constants

View Source
const (
	OperationCreate = gst.OperationCreate
	OperationDelete = gst.OperationDelete
	OperationUpdate = gst.OperationUpdate
	OperationPatch  = gst.OperationPatch
	OperationList   = gst.OperationList
	OperationGet    = gst.OperationGet
	OperationExport = gst.OperationExport
	OperationImport = gst.OperationImport

	OperationCreateMany = gst.OperationCreateMany
	OperationDeleteMany = gst.OperationDeleteMany
	OperationUpdateMany = gst.OperationUpdateMany
	OperationPatchMany  = gst.OperationPatchMany
)

The operations an operation log records.

View Source
const (
	FilterOpEq         = gst.FilterOpEq         // equal: column = value
	FilterOpNe         = gst.FilterOpNe         // not equal: column <> value
	FilterOpGt         = gst.FilterOpGt         // greater than: column > value
	FilterOpGte        = gst.FilterOpGte        // greater than or equal: column >= value
	FilterOpLt         = gst.FilterOpLt         // less than: column < value
	FilterOpLte        = gst.FilterOpLte        // less than or equal: column <= value
	FilterOpIn         = gst.FilterOpIn         // set membership: column IN (comma-separated values)
	FilterOpNotIn      = gst.FilterOpNotIn      // set exclusion: column NOT IN (comma-separated values)
	FilterOpLike       = gst.FilterOpLike       // substring match: column LIKE %value%
	FilterOpNotLike    = gst.FilterOpNotLike    // substring exclusion: column NOT LIKE %value%
	FilterOpILike      = gst.FilterOpILike      // substring match whatever the case of the letters a to z: LOWER(column) LIKE LOWER(%value%)
	FilterOpStartsWith = gst.FilterOpStartsWith // prefix match: column LIKE value% (can use an index)
	FilterOpEndsWith   = gst.FilterOpEndsWith   // suffix match: column LIKE %value
	FilterOpIsNull     = gst.FilterOpIsNull     // null check: value true means IS NULL, false means IS NOT NULL
)

URL-exposed operators, which a request spells as "field[op]=value".

View Source
const (
	FilterOpRegex        = gst.FilterOpRegex        // regular expression match: column REGEXP value (dialect-aware)
	FilterOpNotRegex     = gst.FilterOpNotRegex     // regular expression exclusion: NOT (column REGEXP value)
	FilterOpJSONContains = gst.FilterOpJSONContains // JSON array membership: value is a member of the JSON array column
	FilterOpOr           = gst.FilterOpOr           // group: the []Filter value is OR-combined, the group itself AND-combined
	FilterOpAnd          = gst.FilterOpAnd          // group: the []Filter value is AND-combined, for nesting inside an OR group
	FilterOpExists       = gst.FilterOpExists       // correlated subquery: EXISTS or NOT EXISTS over a related model
	FilterOpEqCol        = gst.FilterOpEqCol        // column equals another column: the enclosing query's inside a subquery, a table read beside it inside a join
	FilterOpFalse        = gst.FilterOpFalse        // constant predicate: matches nothing, see FilterFalse
)

Service-only operators, which service code builds and no request can spell.

View Source
const (
	QueryExpand = gst.QueryExpand
	QueryFormat = gst.QueryFormat
)

The framework-owned URL query parameters a project reads itself: the expansion a Get applies and the format an Export writes. Framework parameters all live in the "_" prefix namespace, so bare query keys always belong to model filter fields.

View Source
const DefaultCountAlias = gst.DefaultCountAlias

DefaultCountAlias is the alias COUNT(*) projects under when the caller does not rename it. A column term defaults to its column name, but COUNT(*) names no column, so without a default of its own it would be the one term that always had to be renamed.

View Source
const FrameworkName = gst.FrameworkName

FrameworkName is the name of the framework, gst: the name generated code and the logs spell it by, the User-Agent of the client, the issuer of the tokens, and the default of the application, service and namespace names the configuration takes.

Variables

View Source
var (
	// NewError creates the error a request is answered with: status and a
	// client-safe message.
	//
	// The status must be a 4xx or 5xx HTTP status code. Invalid statuses, including
	// 2xx/3xx success or redirect statuses such as http.StatusOK, are normalized to
	// http.StatusInternalServerError and the provided message is discarded.
	NewError = gst.NewError

	// NewErrorWithCause creates the error a request is answered with, carrying
	// an internal cause.
	//
	// The status must be a 4xx or 5xx HTTP status code. Invalid statuses, including
	// 2xx/3xx success or redirect statuses such as http.StatusOK, are normalized to
	// http.StatusInternalServerError and the provided message is discarded.
	//
	// The cause is reported by Error for logs and available through Unwrap, but
	// is never exposed as the response message.
	NewErrorWithCause = gst.NewErrorWithCause
)

The constructors are forwarded as variables instead of wrapper functions on purpose: a wrapper function would add its own frame on top of the stack trace captured at the construction site.

View Source
var ErrEntryNotFound = gst.ErrEntryNotFound

ErrEntryNotFound is returned when a cache entry is not found, or when the stored value cannot be decoded as the handle's type and the entry is dropped.

View Source
var ErrTTLNotSupported = gst.ErrTTLNotSupported

ErrTTLNotSupported is returned by Cache.Set when the backend cannot honor the requested ttl semantics, such as a per-entry lifetime on a backend without per-entry expiration.

Functions

func RequestUserID

func RequestUserID(ctx context.Context) string

RequestUserID reports the authenticated subject of the request ctx descends from, or "" when no request is behind it.

Types

type AnyColumnRef

type AnyColumnRef = gst.AnyColumnRef

AnyColumnRef is the type-erased view of every generated column reference, for options that take a heterogeneous column list: WithSelect accepts columns of different Go types in one call, which the parameterized ColumnRef cannot express. The unexported method keeps the set of implementations closed to the framework, so a stray type that happens to carry a Name method cannot slip into a column list.

type Assignment

type Assignment = gst.Assignment

Assignment is one column-value write, the unit UpdateByID accepts. Service code builds assignments through the generated column references (SampleCols.Status.Set(v)), whose typed front end stops a wrong-typed value or a misspelled column at compile time; generic code assigns through a reference minted for its type parameter. Its fields are unexported, so those references are the only way to build one; Table, Column and Value read it back.

type BidiStream

type BidiStream[REQ Request, RSP Response] = gst.BidiStream[REQ, RSP]

BidiStream is the request and response stream of a Stream action declaring both a StreamingPayload and a StreamingResult: Recv and Send as on the two above, independent of each other.

type Bound

type Bound[T any] = gst.Bound[T]

Bound is one end of the range a column's comparison filters confine it to: the value at that end, whether the range includes it, and whether any filter gave that end at all. An absent end leaves the range open on that side.

type Cache

type Cache[T any] = gst.Cache[T]

Cache provides a typed key/value cache abstraction.

type ClientStream

type ClientStream[REQ Request] = gst.ClientStream[REQ]

ClientStream is the request stream of a Stream action declaring a StreamingPayload: the service's Stream method reads each request through Recv, which answers io.EOF once the client finished sending.

type Column

type Column[T any] = gst.Column[T]

Column is a typed reference to a database column, generated per model by gg gen. T is the Go type of the column, so a filter built through a Column cannot name a column that does not exist nor bind a value of the wrong type: both mistakes stop at compile time instead of surfacing as a SQL error or a silently wrong result set.

func NewColumn

func NewColumn[M TableNamer, T any](name string) Column[T]

NewColumn returns a typed reference to the named column of M's table. gg gen emits the calls in each model's generated file, naming the model as the first type argument, so the table comes from the model's own TableName and is never restated as a literal. Handwritten code, model hooks included, reads those generated Cols vars; gg check flags project code that mints a reference instead, with two exceptions. Generic code has no concrete model and so no Cols var: it names its type parameter as the model, and NewColumn[M, string]("id").In(ids...) keeps the value type checked where a plain column name would not. Module sources have no generated file and name their model the way gg gen does. The fields are unexported so a shared reference cannot be repointed at another column after construction.

type ColumnRef

type ColumnRef[T any] = gst.ColumnRef[T]

ColumnRef is the shared typed view of every generated column reference. Helpers that accept a column take this interface rather than a concrete struct, because embedding is not subtyping in Go: NumericColumn[T] cannot be passed where Column[T] is expected, so a helper typed on the struct would reject exactly the numeric and time columns it is most often used with.

type ControllerConfig

type ControllerConfig[M Model] = gst.ControllerConfig[M]

ControllerConfig customizes how router.Register builds an internal handler for a route. It is the public configuration surface for controller behavior; the concrete controller handlers and their runtime state remain framework-owned.

type Cursor

type Cursor = gst.Cursor

Cursor is where a cursor-paginated read starts and which way it goes: the feed's stable ordering, the boundary row, and whether the read travels along that ordering or back down it. Its fields are unexported, so a cursor comes from CursorForward, CursorBackward or the framework's URL parsing; Order, Value, Backward and Bounded read it back. The cursor orders the feed on every page, the first one included.

func CursorBackward

func CursorBackward(order Order, value string) Cursor

CursorBackward pages against order, starting just before value, or at the feed's last row when value is empty.

func CursorForward

func CursorForward(order Order, value string) Cursor

CursorForward pages along order, starting just past value, or at the feed's first row when value is empty.

type Database

type Database[M Model] = gst.Database[M]

Database defines the model-scoped database operation contract. It provides CRUD operations, query builders, and optional dry-run behavior for a single Model type.

type DatabaseOption

type DatabaseOption[M Model] = gst.DatabaseOption[M]

DatabaseOption provides chainable options for a single Database operation chain. Options apply to the next terminal operation and are reset afterward. Start a new chain with database.Database[M](ctx) for each independent operation.

type DateColumn

type DateColumn = gst.DateColumn

DateColumn is the reference generated for a column holding a calendar day, a Date: its filters take the day itself, where a TimeColumn takes an instant. It embeds Column and adds the bucketing a day carries, by day and by month.

func NewDateColumn

func NewDateColumn[M TableNamer](name string) DateColumn

NewDateColumn returns the date reference to the named column of M's table, carrying the bucketing group keys on top of everything Column has.

type Decision

type Decision = gst.Decision

Decision is the outcome of one authorization check.

type Error

type Error = gst.Error

Error is the error a request is answered with: a status and a client-safe message, with an internal cause for the logs. A service method, a model hook, a middleware or an interceptor returns it to refuse a request with a status of its own; any other error is the server's failure, answered 500.

type Expr

type Expr = gst.Expr

Expr is what a projection selects and a window partitions by: a column reference, projected as it is stored, or a Term. The set is closed to the framework, so a projection can never carry SQL text.

type Filter

type Filter = gst.Filter

Filter is one field-level filter to apply as an AND condition: the column it compares, the table that column belongs to, the operator and the value. Its fields are unexported, so a filter comes from the generated column references, a reference minted for a type parameter, the grouping, subquery and constant constructors below, or the framework's URL parsing. Table, Column, Op and Value read it back; Split, Values, ExcludedValues and Bounds on a column reference read one column's filters converted to the column's type. A filter carrying another table is applied to that table when the query joins it and fails the query otherwise, and the value is always bound as a statement parameter.

func FilterAnd

func FilterAnd(filters ...Filter) Filter

FilterAnd groups filters that are AND-combined with each other. Filters are already AND-combined at the top level, so the group exists to nest an AND inside an OR group.

func FilterExists

func FilterExists[C Model](filters ...Filter) Filter

FilterExists matches rows of the queried model that have at least one related row in C satisfying filters. EqCol predicates tie the related rows to the queried row, one per column pair, next to the ordinary conditions narrowing them.

func FilterFalse

func FilterFalse() Filter

FilterFalse matches nothing. It is the condition a permission hook returns when the caller may see no row at all. Unlike an empty filter list it is a real condition, so it disables the empty-query safety check; unlike a filter the renderer cannot apply it is deliberate, so nothing is logged. It renders as 1 = 0 on every dialect and composes like any other filter, inside groups, subqueries and conditional measures included.

func FilterNotExists

func FilterNotExists[C Model](filters ...Filter) Filter

FilterNotExists matches rows that have no related row in C satisfying filters. Note that it is not the negation of a filtered FilterExists over the same rows: a row whose related rows all fail filters matches, and so does a row with no related rows at all. A subquery without any EqCol predicate fails the query here as well, so that negating "match nothing" cannot widen it into "match everything".

func FilterOr

func FilterOr(filters ...Filter) Filter

FilterOr groups filters that are OR-combined with each other. The group as a whole stays AND-combined with every other condition of the query, so a mandatory condition such as tenant scoping can never be absorbed into the alternatives.

type FilterOp

type FilterOp = gst.FilterOp

FilterOp is a field-level filter operator: the comparison a Filter applies, which Filter.Op reads back. Operators never widen a query: unknown values are rejected during parsing, and the database layer never drops a condition it cannot apply, narrowing the query to an empty result on one of a URL-exposed operator and failing it on one of a service-only operator, which no client can have sent.

type JSONStringsColumn

type JSONStringsColumn = gst.JSONStringsColumn

JSONStringsColumn is the reference generated for a column storing a JSON array of strings. It embeds Column over datatypes.JSONSlice[string], the type that binds such an array as a JSON document, and adds the membership test, which only such a column answers alike on every dialect.

func NewJSONStringsColumn

func NewJSONStringsColumn[M TableNamer](name string) JSONStringsColumn

NewJSONStringsColumn returns the reference to the named column of M's table storing a JSON array of strings, carrying JSONContains on top of everything Column has.

type JoinSource

type JoinSource = gst.JoinSource

JoinSource is a source a select joins to its model. The set is closed to the framework: Join and LeftJoin join a model on a unique key, JoinSelect and LeftJoinSelect join a grouped select on its group keys.

func Join

func Join[C Model](on ...Filter) JoinSource

Join joins model C on a unique key: JOIN, keeping only the rows of the query that match a row of C. The predicates are the ON condition.

func JoinSelect

func JoinSelect[R any](sub SelectBranch[R], on ...Filter) JoinSource

JoinSelect joins a grouped select as a derived table, JOIN (SELECT ...) AS jN ON ..., keeping only the rows of the query that match one of its groups. This is how a one-to-many relation is read beside its one side: the many side is grouped by the key first, so every key has one row, and that row is joined.

func LeftJoin

func LeftJoin[C Model](on ...Filter) JoinSource

LeftJoin joins model C on a unique key, keeping the rows of the query that match no row of C with the joined columns NULL: LEFT JOIN. The rules match Join; the result fields the joined columns bind to must hold NULL.

func LeftJoinSelect

func LeftJoinSelect[R any](sub SelectBranch[R], on ...Filter) JoinSource

LeftJoinSelect joins a grouped select as a derived table, keeping the rows of the query that match none of its groups with the select's terms NULL: LEFT JOIN. The rules match JoinSelect; the result fields the select's terms bind to must hold NULL.

type Logger

type Logger = gst.Logger

Logger is the logger the framework hands to services and modules and keeps in the logger package's streams. It writes an entry plain and printf-style, sugared with key/value fields (the "w" methods) and with typed zap.Field values (the "z" methods); With attaches string key/value fields and WithContext derives a logger carrying request metadata fields.

type Model

type Model = gst.Model

Model defines the framework contract for database-backed and action models. Typical database resources embed model.Base (UUIDv7 string primary key) or model.AutoBase (auto-increment integer primary key). Action-only models may use model.Empty when they do not represent persistent rows.

type Module

type Module[M Model, REQ Request, RSP Response] = gst.Module[M, REQ, RSP]

Module describes a registered API module: route metadata, auth exposure, resource parameter name, and the service implementation used by controllers.

type NumericColumn

type NumericColumn[T any] = gst.NumericColumn[T]

NumericColumn is the reference generated for a column whose Go type is numeric. It embeds Column, so every filter and order stays available, and adds the aggregate functions that only make sense over a number.

func NewNumericColumn

func NewNumericColumn[M TableNamer, T any](name string) NumericColumn[T]

NewNumericColumn returns the numeric reference to the named column of M's table, carrying Sum and Avg on top of everything Column has.

type Operation

type Operation = gst.Operation

Operation is the operation an operation log records, one per action.

type Order

type Order = gst.Order

Order is one ORDER BY term: a column and the direction to sort it by. Its fields are unexported, so an order comes from a generated column reference, a reference minted for a type parameter, or the framework's URL parsing; Table, Column and Descending read it back, and SortsBy tells whether it sorts by a given column. Table is filled in by a column reference and left empty by URL parsing. The chain's reads and a select check it: a select that joins tells two tables' columns of one name apart by it, and a chain refuses an order of another model. A union orders its result columns by name and reads no table, and WithExpand orders the associated table, so neither checks it. An Order with an empty column is skipped rather than rendered.

type Ordering

type Ordering = gst.Ordering

Ordering is what the OrderBy methods of a select, a window and a union accept: an Order sorting by a column reference, or a TermOrder sorting by a projection term. The set is closed, so an ordering can never carry SQL the way a free-form string could.

type Permission

type Permission = gst.Permission

Permission is one operation a role is allowed to perform on one object. It is the unit the whole-set replacement methods on RBAC take, so a caller states a role's permissions as a set rather than as a sequence of grants.

type QueryOptions

type QueryOptions = gst.QueryOptions

QueryOptions tunes how WithQuery turns a model value into WHERE conditions. Every condition it produces is AND-combined; the zero value means exact matching with the empty-query safety check enabled. See the WithQuery method for usage examples.

type RBAC

type RBAC = gst.RBAC

RBAC provides tenant-scoped role, permission, and subject assignment operations. A process holding no policy set — RBAC disabled, or not initialized — answers reads as the deployment they describe, denying every request and reporting no roles, and refuses every write rather than reporting a change it did not make.

type Request

type Request = gst.Request

Request and Response are the framework-facing types of one action's request and response payloads. They constrain the REQ and RSP type parameters of Service and Module; the concrete types are declared per action by the model layer.

type Response

type Response = gst.Response

Request and Response are the framework-facing types of one action's request and response payloads. They constrain the REQ and RSP type parameters of Service and Module; the concrete types are declared per action by the model layer.

type SQLStatement

type SQLStatement = gst.SQLStatement

SQLStatement contains a generated SQL statement in executable and rendered forms.

type SelectBranch

type SelectBranch[R any] = gst.SelectBranch[R]

SelectBranch is a select in the role of a branch of a union: every Selector is one, with its model type erased, so that selects over different models stack into one result as long as they scan into the same row type. The role is what UnionAll takes. Only the selects the database layer builds can fill it; UnionAll fails when handed anything else, a union among them.

type Selector

type Selector[M Model, R any] = gst.Selector[M, R]

Selector runs an analytical read over the table of M and scans the result rows into R. It is deliberately separate from Database[M]: a projected row is not a model row, so model hooks, association preloading and cursor pagination have nothing to act on and are absent here rather than present and inert.

type ServerStream

type ServerStream[RSP Response] = gst.ServerStream[RSP]

ServerStream is the response stream of a Stream action declaring a StreamingResult: the service's Stream method sends each response through Send, which fails once the client went away or the call was canceled.

type Service

type Service[M Model, REQ Request, RSP Response] = gst.Service[M, REQ, RSP]

Service defines the controller-facing business operation contract for a model. Generated controllers call these methods for CRUD, batch CRUD, lifecycle hooks, import/export, filtering, and logging.

type ServiceContext

type ServiceContext = gst.ServiceContext

ServiceContext is the per-request context the framework hands to every service method. It implements context.Context by delegating to the request context, exposes request metadata (route, params, user identity, trace), and carries the response helpers a service needs without touching Gin directly.

type TableNamer

type TableNamer = gst.TableNamer

TableNamer is the one method a column reference needs from its model: the table the column belongs to. Every Model satisfies it; the narrower interface lets a reference name its model as a type argument alone, with nothing else of the model contract in play.

type Term

type Term = gst.Term

Term is one term of a projection: a group key, a plain column, a constant, a measure, or a window function. Column references build it, as do Count, the ranking functions and Literal below.

func Count

func Count() Term

Count counts rows: COUNT(*). It counts a row even when every column is NULL, which is what a plain row count means; use a column reference's Count for COUNT(column), which skips NULLs.

func DenseRank

func DenseRank() Term

DenseRank ranks like Rank without skipping: 1, 2, 2, 3.

func Literal

func Literal(value string) Term

Literal projects a constant, which is how the branches of a union tell their rows apart.

func Rank

func Rank() Term

Rank ranks the rows of each partition in the window's order. Rows sorting equal share a rank and the next rank skips past them: 1, 2, 2, 4.

func RowNumber

func RowNumber() Term

RowNumber numbers the rows of each partition from 1 in the window's order, with no ties: two rows sorting equal still get consecutive numbers, in a stable order the framework completes with the primary key. Like Rank and DenseRank it only exists over a window whose OrderBy is set, which Over declares: without an order there is no first row to number.

type TermCondition

type TermCondition = gst.TermCondition

TermCondition is one condition on a projected term: a Having condition on a measure, or a Qualify condition on a window function. It carries the term itself rather than an alias string, which has two consequences: a condition can never name a term the projection did not declare, and the renderer can emit the full expression instead of the alias, which HAVING requires because PostgreSQL does not accept an output alias there.

type TermOrder

type TermOrder = gst.TermOrder

TermOrder is one ORDER BY term of a select or of a window. Unlike Order it sorts by a projection term, which is what a TopN report ranks by.

type TimeColumn

type TimeColumn = gst.TimeColumn

TimeColumn is the reference generated for a column holding an instant, time.Time or a type holding one. It embeds Column and adds time bucketing, which is only meaningful over a time value and produces garbage rather than an error on some dialects when it is not.

func NewTimeColumn

func NewTimeColumn[M TableNamer](name string) TimeColumn

NewTimeColumn returns the time reference to the named column of M's table, carrying the bucketing group keys on top of everything Column has.

type Union

type Union[R any] = gst.Union[R]

Union stacks the rows of several selects into one result: UNION ALL, the one set operation the framework offers. UNION proper would fold two rows that happen to be equal — two payments of the same amount on the same day — into one, which no report wants; INTERSECT and EXCEPT are the semi joins FilterExists and FilterNotExists already express.

type Window

type Window = gst.Window

Window names the rows a window function reads for each row: PartitionBy splits the rows into partitions, and OrderBy orders each partition, which is what gives a running total its direction and a row number its sequence.

func OrderBy

func OrderBy(orders ...Ordering) Window

OrderBy returns a Window with no partition and the given orders: the window a ranking across every row reads. It is the short spelling of PartitionBy().OrderBy(orders...), the two building the same window; a window with keys starts from PartitionBy. It orders the window, not the result: the result is ordered by the Selector's OrderBy.

func PartitionBy

func PartitionBy(keys ...Expr) Window

PartitionBy opens a window partitioned by keys. Without keys the whole result is one partition, which is what a ranking over every row wants, and the window is the one OrderBy opens: the two spellings build the same value, so a term declared with one is found by the other.

Directories

Path Synopsis
Package apidoc registers the doc comments the generated OpenAPI document describes a project's API with.
Package apidoc registers the doc comments the generated OpenAPI document describes a project's API with.
Package authn exposes the authentication extension points that optional modules install into the mandatory IAM login flow.
Package authn exposes the authentication extension points that optional modules install into the mandatory IAM login flow.
jwt
Package jwt issues and checks the tokens of a JWT login: GenTokens signs an access token and a refresh token for a user, ParseToken and Verify check the access token a request carries, and RefreshTokens trades a refresh token for a new pair.
Package jwt issues and checks the tokens of a JWT login: GenTokens signs an access token and a refresh token for a user, ParseToken and Verify check the access token a request carries, and RefreshTokens trades a refresh token for a new pair.
authz
rbac
Package rbac decides authorization from stored policy rules and keeps those rules in step with the records they are derived from.
Package rbac decides authorization from stored policy rules and keeps those rules in step with the records they are derived from.
Package bootstrap brings a gst process up and takes it down again.
Package bootstrap brings a gst process up and takes it down again.
Package cache is the public facade of the framework's in-memory cache.
Package cache is the public facade of the framework's in-memory cache.
Package client is the official HTTP client for gst backends, designed as the client-side pairing of the framework's DSL: every HTTP interface shape a model's Design() can declare has a first-class counterpart here.
Package client is the official HTTP client for gst backends, designed as the client-side pairing of the framework's DSL: every HTTP interface shape a model's Design() can declare has a first-class counterpart here.
cmd
gg command
Gg works on the gst project in the current directory: it generates the code the models declare, checks the project against the framework's conventions, and migrates the database to the models.
Gg works on the gst project in the current directory: it generates the code the models declare, checks the project against the framework's conventions, and migrates the database to the models.
Package component runs a project's own long-running work alongside the server — a consumer loop, a poller, a watcher: work that runs on every replica for the life of the process.
Package component runs a project's own long-running work alongside the server — a consumer loop, a poller, a watcher: work that runs on every replica for the life of the process.
Package config loads the application configuration: the framework's own sections, held by App, and the sections a project adds with Register.
Package config loads the application configuration: the framework's own sections, held by App, and the sections a project adds with Register.
Package cronjob runs the jobs a project registers on a schedule.
Package cronjob runs the jobs a project registers on a schedule.
Package database provides the framework database facade built on top of GORM.
Package database provides the framework database facade built on top of GORM.
clickhouse
Package clickhouse is the ClickHouse dialect of the framework's database layer: Init opens the default connection when the configuration selects ClickHouse, and New opens a handle from a configuration of the caller's.
Package clickhouse is the ClickHouse dialect of the framework's database layer: Init opens the default connection when the configuration selects ClickHouse, and New opens a handle from a configuration of the caller's.
mysql
Package mysql is the MySQL dialect of the framework's database layer: Init opens the default connection when the configuration selects MySQL, New opens a handle from a configuration of the caller's, and DryRun returns a handle that builds statements without connecting.
Package mysql is the MySQL dialect of the framework's database layer: Init opens the default connection when the configuration selects MySQL, New opens a handle from a configuration of the caller's, and DryRun returns a handle that builds statements without connecting.
postgres
Package postgres is the PostgreSQL dialect of the framework's database layer: Init opens the default connection when the configuration selects PostgreSQL, and New opens a handle from a configuration of the caller's.
Package postgres is the PostgreSQL dialect of the framework's database layer: Init opens the default connection when the configuration selects PostgreSQL, and New opens a handle from a configuration of the caller's.
sqlite
Package sqlite is the SQLite dialect of the framework's database layer: Init opens the default connection when the configuration selects SQLite, and New opens a handle from a configuration of the caller's.
Package sqlite is the SQLite dialect of the framework's database layer: Init opens the default connection when the configuration selects SQLite, and New opens a handle from a configuration of the caller's.
Package datatypes holds the types a model declares the fields of a kind of its own with: a calendar day, a time of day, and a JSON document stored in one column.
Package datatypes holds the types a model declares the fields of a kind of its own with: a calendar day, a time of day, and a JSON document stored in one column.
Package dbmigrate renders registered Go models into a target schema and migrates a database towards it.
Package dbmigrate renders registered Go models into a target schema and migrates a database towards it.
Package dcache provides a replicated in-memory cache: a per-process store whose set and delete operations propagate to the store of every other instance through Kafka events.
Package dcache provides a replicated in-memory cache: a per-process store whose set and delete operations propagate to the store of every other instance through Kafka events.
ds
heap/binaryheap
Package binaryheap is a generic binary heap ordered by a comparison function: a min-heap by default, the smallest element coming out first, and a max-heap with WithMaxHeap.
Package binaryheap is a generic binary heap ordered by a comparison function: a min-heap by default, the smallest element coming out first, and a max-heap with WithMaxHeap.
list/arraylist
Package arraylist provides a generic implementation of a resizable array-backed list.
Package arraylist provides a generic implementation of a resizable array-backed list.
list/linkedlist
Package linkedlist provides a doubly-linked list with a front and a back.
Package linkedlist provides a doubly-linked list with a front and a back.
list/skiplist
Package skiplist is a generic skip list: keys kept in the order of a comparison function, each with its value, and found, inserted and deleted in logarithmic time on average.
Package skiplist is a generic skip list: keys kept in the order of a comparison function, each with its value, and found, inserted and deleted in logarithmic time on average.
mapset
Package mapset is a generic set backed by a map.
Package mapset is a generic set backed by a map.
multimap
Package multimap is a generic map holding any number of values under each key, the values told apart by a comparison function.
Package multimap is a generic map holding any number of values under each key, the values told apart by a comparison function.
queue/arrayqueue
Package arrayqueue is a generic first-in, first-out queue backed by a slice.
Package arrayqueue is a generic first-in, first-out queue backed by a slice.
queue/circularbuffer
Package circularbuffer is a generic circular buffer of a fixed size: once full it overwrites its oldest element with each new one, or, with WithDrop, refuses the new one.
Package circularbuffer is a generic circular buffer of a fixed size: once full it overwrites its oldest element with each new one, or, with WithDrop, refuses the new one.
queue/linkedqueue
Package linkedqueue is a generic first-in, first-out queue backed by a linked list.
Package linkedqueue is a generic first-in, first-out queue backed by a linked list.
queue/priorityqueue
Package priorityqueue is a generic priority queue built on a binary heap and ordered by a comparison function: the smallest element comes out first by default, the largest with WithMaxPriority.
Package priorityqueue is a generic priority queue built on a binary heap and ordered by a comparison function: the smallest element comes out first by default, the largest with WithMaxPriority.
stack/arraystack
Package arraystack is a generic last-in, first-out stack backed by a slice.
Package arraystack is a generic last-in, first-out stack backed by a slice.
stack/linkedstack
Package linkedstack is a generic last-in, first-out stack backed by a linked list.
Package linkedstack is a generic last-in, first-out stack backed by a linked list.
tree/avltree
Package avltree is a generic AVL tree: a self-balancing binary search tree holding keys in the order of a comparison function, each with its value.
Package avltree is a generic AVL tree: a self-balancing binary search tree holding keys in the order of a comparison function, each with its value.
tree/rbtree
Package rbtree is a generic red-black tree: a self-balancing binary search tree holding keys in the order of a comparison function, each with its value.
Package rbtree is a generic red-black tree: a self-balancing binary search tree holding keys in the order of a comparison function, each with its value.
tree/splaytree
Package splaytree is a generic splay tree: a binary search tree holding keys in the order of a comparison function, each with its value, in which a lookup moves the node it ended on to the root, so keys looked up often stay near it.
Package splaytree is a generic splay tree: a binary search tree holding keys in the order of a comparison function, each with its value, in which a lookup moves the node it ended on to the root, so keys looked up often stay near it.
tree/trie
Package trie is a generic trie: a tree keyed by sequences of K, the runes of a string for one, in which keys sharing a prefix share the path that spells it, each key with its value.
Package trie is a generic trie: a tree keyed by sequences of K, the runes of a string for one, in which keys sharing a prefix share the path that spells it, each key with its value.
types
Package types holds what the data structure packages under ds share: the iterator and container interfaces they implement, the locker a structure guards itself with, and the errors they return.
Package types holds what the data structure packages under ds share: the iterator and container interfaces they implement, the locker a structure guards itself with, and the errors they return.
Package dsl provides a Domain Specific Language (DSL) for defining REST API designs for Go models.
Package dsl provides a Domain Specific Language (DSL) for defining REST API designs for Go models.
Package filetype tells the type of a file from its content, as a Filetype with its Mime.
Package filetype tells the type of a file from its content, as a Filetype with its Mime.
Package grpc is the gRPC listener's API for a project's own code.
Package grpc is the gRPC listener's API for a project's own code.
Package i18n registers the translations of the sentences a module, or a project, answers its clients with.
Package i18n registers the translations of the sentences a module, or a project, answers its clients with.
Package inspect forwards the framework's model rules to the program gg builds to inspect a project's models.
Package inspect forwards the framework's model rules to the program gg builds to inspect a project's models.
Package interceptor is the gRPC counterpart of package middleware: the interceptors a project mounts on the gRPC listener.
Package interceptor is the gRPC counterpart of package middleware: the interceptors a project mounts on the gRPC listener.
internal
apiaction
Package apiaction holds the vocabulary of an action a model declares: the phases of an action, the action itself and the hooks run before and after it, and the rule of the path of a route, the route under the API prefix and the segment the CRUD layout appends to it for an action.
Package apiaction holds the vocabulary of an action a model declares: the phases of an action, the action itself and the hooks run before and after it, and the rule of the path of a route, the route under the API prefix and the segment the CRUD layout appends to it for an action.
apidoc
Package apidoc keeps what the generated OpenAPI document describes an API with: the doc comments of structs, their fields and enums, the security schemes a request authenticates by, and the summary and description of an operation, registered or derived from the doc comment of its model.
Package apidoc keeps what the generated OpenAPI document describes an API with: the doc comments of structs, their fields and enums, the security schemes a request authenticates by, and the summary and description of an operation, registered or derived from the doc comment of its model.
auditmanager
Package auditmanager writes the operation log: an entry for every operation the audit configuration does not exclude, written by the handler that performed the operation, before the request answers.
Package auditmanager writes the operation log: an entry for every operation the audit configuration does not exclude, written by the handler that performed the operation, before the request answers.
cache/bigcache
Package bigcache adapts github.com/allegro/bigcache/v3 to gst.Cache: a sharded cache that stores serialized values, encoded through codec, so it is for plain data only (see the public cache package).
Package bigcache adapts github.com/allegro/bigcache/v3 to gst.Cache: a sharded cache that stores serialized values, encoded through codec, so it is for plain data only (see the public cache package).
cache/cachetest
Package cachetest provides the conformance suite every gst.Cache backend must pass.
Package cachetest provides the conformance suite every gst.Cache backend must pass.
cache/capacity
Package capacity resolves the per-type entry bound shared by the entry-addressed cache backends, so the configuration is read in one place rather than copied into each of them.
Package capacity resolves the per-type entry bound shared by the entry-addressed cache backends, so the configuration is read in one place rather than copied into each of them.
cache/ccache
Package ccache adapts github.com/karlseguin/ccache/v3 to gst.Cache: a cache of live values with a lifetime per entry, bounded by the per-type entry bound (see capacity).
Package ccache adapts github.com/karlseguin/ccache/v3 to gst.Cache: a cache of live values with a lifetime per entry, bounded by the per-type entry bound (see capacity).
cache/cmap
Package cmap adapts github.com/orcaman/concurrent-map/v2 to gst.Cache: a sharded map of live values with no expiration and no entry bound.
Package cmap adapts github.com/orcaman/concurrent-map/v2 to gst.Cache: a sharded map of live values with no expiration and no entry bound.
cache/codec
Package codec encodes cache values for the backends that store bytes rather than live objects.
Package codec encodes cache values for the backends that store bytes rather than live objects.
cache/defaultcache
Package defaultcache is the in-memory cache backend the framework forwards as its default, freelru: the one place the choice is made.
Package defaultcache is the in-memory cache backend the framework forwards as its default, freelru: the one place the choice is made.
cache/fastcache
Package fastcache adapts github.com/VictoriaMetrics/fastcache to gst.Cache: a cache sized in bytes that stores serialized values, encoded through codec, so it is for plain data only (see the public cache package).
Package fastcache adapts github.com/VictoriaMetrics/fastcache to gst.Cache: a cache sized in bytes that stores serialized values, encoded through codec, so it is for plain data only (see the public cache package).
cache/freecache
Package freecache adapts github.com/coocood/freecache to gst.Cache: a cache sized in bytes that stores serialized values, encoded through codec, so it is for plain data only (see the public cache package).
Package freecache adapts github.com/coocood/freecache to gst.Cache: a cache sized in bytes that stores serialized values, encoded through codec, so it is for plain data only (see the public cache package).
cache/freelru
Package freelru is a sharded LRU cache that stores live values.
Package freelru is a sharded LRU cache that stores live values.
cache/lru
Package lru adapts github.com/hashicorp/golang-lru/v2 to gst.Cache: a cache of live values that evicts the least recently used entry past the per-type entry bound (see capacity).
Package lru adapts github.com/hashicorp/golang-lru/v2 to gst.Cache: a cache of live values that evicts the least recently used entry past the per-type entry bound (see capacity).
cache/otter
Package otter is a W-TinyLFU cache.
Package otter is a W-TinyLFU cache.
cache/registry
Package registry provides the shared per-type singleton store used by the in-memory cache backends.
Package registry provides the shared per-type singleton store used by the in-memory cache backends.
cache/ristretto
Package ristretto is a TinyLFU-admission cache.
Package ristretto is a TinyLFU-admission cache.
cache/smap
Package smap adapts sync.Map to gst.Cache: a map of live values with no expiration and no entry bound.
Package smap adapts sync.Map to gst.Cache: a map of live values with no expiration and no entry bound.
cache/tracing
Package tracing wraps cache backends that talk to remote systems with OpenTelemetry spans.
Package tracing wraps cache backends that talk to remote systems with OpenTelemetry spans.
clioutput
Package clioutput is how gg writes to the terminal: the few line shapes every command prints with, so that all of them read alike.
Package clioutput is how gg writes to the terminal: the few line shapes every command prints with, so that all of them read alike.
cmd/apidocgen command
Command apidocgen regenerates the apidoc registration files of the framework packages that ship struct doc comments to the OpenAPI generator.
Command apidocgen regenerates the apidoc registration files of the framework packages that ship struct doc comments to the OpenAPI generator.
cmd/selfcheck command
Command selfcheck holds the framework's own source to the rules golangci-lint cannot express, and prints each violation it finds.
Command selfcheck holds the framework's own source to the rules golangci-lint cannot express, and prints each violation it finds.
cmd/testcachefix command
Command testcachefix writes back the test results go keeps under one key only, so a cached test never links its binary again.
Command testcachefix writes back the test results go keeps under one key only, so a cached test never links its binary again.
controller
Package controller serves the actions of the registered routes on both transports: over HTTP through the handlers the router mounts (CreateHandler and its kind), over gRPC through the call functions the generated pb package runs (CreateCall and its kind).
Package controller serves the actions of the registered routes on both transports: over HTTP through the handlers the router mounts (CreateHandler and its kind), over gRPC through the call functions the generated pb package runs (CreateCall and its kind).
cqlpolicy
Package cqlpolicy builds the gocql policies the cassandra and scylla providers configure from settings both of them carry, so that a setting means the same in either: the retry policy downgrading_consistency names and the tries a reconnection policy makes.
Package cqlpolicy builds the gocql policies the cassandra and scylla providers configure from settings both of them carry, so that a setting means the same in either: the retry policy downgrading_consistency names and the tries a reconnection policy makes.
datatypes
Package datatypes holds the types a model declares the fields of a kind of its own with: a calendar day, a time of day, and a JSON document stored in one column, an array, an object or a value of any Go type.
Package datatypes holds the types a model declares the fields of a kind of its own with: a calendar day, a time of day, and a JSON document stored in one column, an array, an object or a value of any Go type.
dbruntime
Package dbruntime holds the database runtime below the public database package: what that package, the dialect packages and the framework's other users of the database share, and a project never sees.
Package dbruntime holds the database runtime below the public database package: what that package, the dialect packages and the framework's other users of the database share, and a project never sees.
dbruntime/dbnode
Package dbnode names the roles of the nodes a database handle writes to and reads from, and stamps the role that serves a statement on the statement's context, for the SQL log to report which node ran it.
Package dbnode names the roles of the nodes a database handle writes to and reads from, and stamps the role that serves a statement on the statement's context, for the SQL log to report which node ran it.
debug/gops
Package gops runs the gops agent, through which the gops command inspects the running process, when the configuration enables it (config.App's GopsEnabled): bootstrap starts it with the process and stops it as the process shuts down.
Package gops runs the gops agent, through which the gops command inspects the running process, when the configuration enables it (config.App's GopsEnabled): bootstrap starts it with the process and stops it as the process shuts down.
debug/pprof
Package debugpprof provides an HTTP server for pprof endpoints; the package name avoids conflicting with the standard library "pprof" packages.
Package debugpprof provides an HTTP server for pprof endpoints; the package name avoids conflicting with the standard library "pprof" packages.
debug/statsviz
Package statsviz serves the statsviz page, live charts of the Go runtime's metrics, on a listener of its own when the configuration enables it (config.App's StatsvizEnabled): bootstrap starts the server with the process and stops it as the process shuts down.
Package statsviz serves the statsviz page, live charts of the Go runtime's metrics, on a listener of its own when the configuration enables it (config.App's StatsvizEnabled): bootstrap starts the server with the process and stops it as the process shuts down.
dsl
Package dsl reads the designs the models of a project declare with the keywords of the public dsl package: Parse turns a model file into the Design of each of its models, Validate reports the declarations the generator cannot honor, and Design and Action are what gg gen, gg check and the generators consume.
Package dsl reads the designs the models of a project declare with the keywords of the public dsl package: Parse turns a model file into the Design of each of its models, Validate reports the declarations the generator cannot honor, and Design and Action are what gg gen, gg check and the generators consume.
errorstack
Package errorstack extracts stack traces embedded in errors, shared by tracing and logging so both report the same error origin stack format.
Package errorstack extracts stack traces embedded in errors, shared by tracing and logging so both report the same error origin stack format.
execctx
Package execctx carries the identity of the unit of work a context belongs to.
Package execctx carries the identity of the unit of work a context belongs to.
ggcheck
Package ggcheck holds the rules gg check holds a business project to.
Package ggcheck holds the rules gg check holds a business project to.
ggconfig
Package ggconfig loads the project-level gst configuration file (gst.yaml) that gg commands consume at build time.
Package ggconfig loads the project-level gst configuration file (gst.yaml) that gg commands consume at build time.
ggconst
Package ggconst holds the conventions the framework's tooling shares: the import paths of the framework packages, the names of the files, packages, fields and symbols gg generates and reads, and the patterns it recognizes them by.
Package ggconst holds the conventions the framework's tooling shares: the import paths of the framework packages, the names of the files, packages, fields and symbols gg generates and reads, and the patterns it recognizes them by.
gggen
Package gggen builds the code gg gen writes: the registration files, the service files and their test scaffolds, and, in its sub-packages, the column references, the TypeScript declarations and the protobuf definitions.
Package gggen builds the code gg gen writes: the registration files, the service files and their test scaffolds, and, in its sub-packages, the column references, the TypeScript declarations and the protobuf definitions.
gggen/columns
Package columns generates the typed column references of a project's models, so filters name columns through the compiler instead of through string literals.
Package columns generates the typed column references of a project's models, so filters name columns through the compiler instead of through string literals.
gggen/pb
Package pb generates the protobuf definitions of a gst project's gRPC services and the Go files serving them, from the schema pbschema derives of the models declaring GRPC(): the definitions printed as .proto files that mirror the model directory under pb/; beside each, a .gen.go with the type serving the service, the calls of its actions, the handlers of its rpcs and the conversions between the messages and the Go types (see handlerFile); and, in every package under pb/, a pb.gen.go registering its services on the listener (see registrationFiles).
Package pb generates the protobuf definitions of a gst project's gRPC services and the Go files serving them, from the schema pbschema derives of the models declaring GRPC(): the definitions printed as .proto files that mirror the model directory under pb/; beside each, a .gen.go with the type serving the service, the calls of its actions, the handlers of its rpcs and the conversions between the messages and the Go types (see handlerFile); and, in every package under pb/, a pb.gen.go registering its services on the listener (see registrationFiles).
gggen/ts
Package ts generates the TypeScript declarations of the Go types a gst project's API routes send and receive.
Package ts generates the TypeScript declarations of the Go types a gst project's API routes send and receive.
gghelper
Package gghelper holds what more than one of gg's packages needs about the project gg runs in: the module path its go.mod declares, the paths gg leaves out of it, the ones its Git ignore rules exclude and the ones the go command leaves out, programs and package listings run against its module, and the small file and path helpers the commands share.
Package gghelper holds what more than one of gg's packages needs about the project gg runs in: the module path its go.mod declares, the paths gg leaves out of it, the ones its Git ignore rules exclude and the ones the go command leaves out, programs and package listings run against its module, and the small file and path helpers the commands share.
gghelper/program/inspect command
The inspection program reports the project models' columns, what their index declarations violate on MySQL, the framework columns they rename, the query names they declare ambiguously and the ID accessors they lack for an ID of their own, as JSON written to the file its argument names: it is what gg gen and gg check build and run inside the project.
The inspection program reports the project models' columns, what their index declarations violate on MySQL, the framework columns they rename, the query names they declare ambiguously and the ID accessors they lack for an ID of their own, as JSON written to the file its argument names: it is what gg gen and gg check build and run inside the project.
gghelper/program/migrate command
The migration program migrates a project's database to its models, or dumps the schema its models describe: it is what gg migrate builds and runs inside the project.
The migration program migrates a project's database to its models, or dumps the schema its models describe: it is what gg migrate builds and runs inside the project.
ggmodule
Package ggmodule implements the behavior behind the gg module command family: discovering framework modules (list), registering a module import in the project's module/module.go (add, remove), and materializing a module into the project as project-owned source (copy).
Package ggmodule implements the behavior behind the gg module command family: discovering framework modules (list), registering a module import in the project's module/module.go (add, remove), and materializing a module into the project as project-owned source (copy).
ggnew
Package ggnew scaffolds the project gg new creates: the first version of every package the generated main.go imports, so the project compiles before its first generation.
Package ggnew scaffolds the project gg new creates: the first version of every package the generated main.go imports, so the project compiles before its first generation.
ggprune
Package ggprune works out what gg prune deletes from a project's service and pb directories, and deletes it: the service files of disabled actions, the unmanaged files of service directories no model owns, the files under pb/ gg gen would not write now (see PlanPBFiles), and the directories that leaves empty.
Package ggprune works out what gg prune deletes from a project's service and pb directories, and deletes it: the service files of disabled actions, the unmanaged files of service directories no model owns, the files under pb/ gg gen would not write now (see PlanPBFiles), and the directories that leaves empty.
goast
Package goast holds the go/ast helpers the framework's tooling shares: the questions asked of an expression, a statement or a declaration (IsQualified, AlwaysLeaves, FindStructTypeSpec), the edits of a file's imports and identifiers (InsertImportSpec, RenameIdent), the recognition of the framework's service base and of test functions (IsServiceBase, IsTestCaseFunc), and the header and line positions a generated file is printed with (GeneratedHeader, LineSet).
Package goast holds the go/ast helpers the framework's tooling shares: the questions asked of an expression, a statement or a declaration (IsQualified, AlwaysLeaves, FindStructTypeSpec), the edits of a file's imports and identifiers (InsertImportSpec, RenameIdent), the recognition of the framework's service base and of test functions (IsServiceBase, IsTestCaseFunc), and the header and line positions a generated file is printed with (GeneratedHeader, LineSet).
grpcserver
Package grpcserver serves the gRPC services of the models declaring GRPC(), on a listener of its own beside the HTTP one and on the same lifecycle: bootstrap starts it with the other listeners, drains it with the readiness probe and stops it side by side with the HTTP listener within the shutdown's window, its streams ending as the stop begins.
Package grpcserver serves the gRPC services of the models declaring GRPC(), on a listener of its own beside the HTTP one and on the same lifecycle: bootstrap starts it with the other listeners, drains it with the readiness probe and stops it side by side with the HTTP listener within the shutdown's window, its streams ending as the stop begins.
gst
Package gst defines the contracts between the framework and business projects: the Model, Service, Database, Selector, Cache, RBAC, and Logger interfaces, the query building blocks they exchange (Filter, Order, Cursor, Column, Term, Window), and the per-request ServiceContext.
Package gst defines the contracts between the framework and business projects: the Model, Service, Database, Selector, Cache, RBAC, and Logger interfaces, the query building blocks they exchange (Filter, Order, Cursor, Column, Term, Window), and the per-request ServiceContext.
hookoverride
Package hookoverride tells whether a type overrides a method beyond the framework base it embeds, reading the answer off the compiled binary.
Package hookoverride tells whether a type overrides a method beyond the framework base it embeds, reading the answer off the compiled binary.
httpwrapper
Package httpwrapper provides JSON round-trip wrappers around *http.Request and *http.Response.
Package httpwrapper provides JSON round-trip wrappers around *http.Request and *http.Response.
i18n
Package i18n holds the language a process answers its clients in and the translations of the sentences it answers them with.
Package i18n holds the language a process answers its clients in and the translations of the sentences it answers them with.
instance
Package instance answers one question — which process is this — for every part of the framework that tells replicas apart: the field every log entry carries, the tracing resource, the origin a replicated cache event names.
Package instance answers one question — which process is this — for every part of the framework that tells replicas apart: the field every log entry carries, the tracing resource, the origin a replicated cache event names.
jsonshape
Package jsonshape reads what the types of a gst project look like on the wire: the JSON encoding/json writes for them, which is the codec of the framework's response envelope and request binding.
Package jsonshape reads what the types of a gst project look like on the wire: the JSON encoding/json writes for them, which is the codec of the framework's response envelope and request binding.
jsonshape/fixture/a-b
Package ab lives in a directory whose name maps to the same TypeScript import name as the a_b fixture package.
Package ab lives in a directory whose name maps to the same TypeScript import name as the a_b fixture package.
jsonshape/fixture/a_b
Package ab lives in a directory whose name maps to the same TypeScript import name as the a-b fixture package.
Package ab lives in a directory whose name maps to the same TypeScript import name as the a-b fixture package.
jsonshape/fixture/gst
Package gst sits where its output file would be gst.ts, the name of the framework prelude.
Package gst sits where its output file would be gst.ts, the name of the framework prelude.
jsonshape/fixture/mode
Package mode declares an enum type another fixture package declares a further constant of.
Package mode declares an enum type another fixture package declares a further constant of.
jsonshape/fixture/model/record
Package record declares fixture types the sample fixture refers to from another package.
Package record declares fixture types the sample fixture refers to from another package.
jsonshape/fixture/model/sample
Package sample declares the fixture types the TypeScript generator tests start from.
Package sample declares the fixture types the TypeScript generator tests start from.
jsonshape/fixture/pkg/notifier
Package notifier declares a fixture type a model refers to from outside the model directory.
Package notifier declares a fixture type a model refers to from outside the model directory.
jsonshape/fixture/unsupported
Package unsupported declares fixture types whose JSON shape the TypeScript generator cannot describe.
Package unsupported declares fixture types whose JSON shape the TypeScript generator cannot describe.
lease
Package lease is the coordination primitive the framework's distributed capabilities stand on: a name that at most one healthy process among those sharing the primary database holds at a time.
Package lease is the coordination primitive the framework's distributed capabilities stand on: a name that at most one healthy process among those sharing the primary database holds at a time.
lifecycle
Package lifecycle is the registry of the framework components that have a lifetime of their own: clients of external systems, the scheduler, election loops, anything that owns a connection or a background goroutine — and, through the component package, a project's own long-running work.
Package lifecycle is the registry of the framework components that have a lifetime of their own: clients of external systems, the scheduler, election loops, anything that owns a connection or a background goroutine — and, through the component package, a project's own long-running work.
logfield
Package logfield declares log fields more than one stream of the framework writes, each as one constructor fixing the field's key and its type.
Package logfield declares log fields more than one stream of the framework writes, each as one constructor fixing the field's key and its type.
logger
Package logger holds the framework's log streams, one per component, and the loggers behind them: Init opens the streams the configuration names, and New builds a logger of its own for a component that has one.
Package logger holds the framework's log streams, one per component, and the loggers behind them: Init opens the streams the configuration names, and New builds a logger of its own for a component that has one.
metrics
Package prommetrics provides Prometheus metrics for the application; the name avoids conflicting with standard library or common "metrics" package names.
Package prommetrics provides Prometheus metrics for the application; the name avoids conflicting with standard library or common "metrics" package names.
middleware
Package middleware holds the middleware the framework mounts itself and the machinery that mounts the rest: the built-in chain ahead of every route, the registry Register and RegisterAuth fill, and the registries the router fills as routes register — route parameters and streaming routes — together with the circuit breaker built from configuration at startup.
Package middleware holds the middleware the framework mounts itself and the machinery that mounts the rest: the built-in chain ahead of every route, the registry Register and RegisterAuth fill, and the registries the router fills as routes register — route parameters and streaming routes — together with the circuit breaker built from configuration at startup.
model
Package model contains the internal model infrastructure behind the public model package.
Package model contains the internal model infrastructure behind the public model package.
modelinspect
Package modelinspect inspects a project's models by running them.
Package modelinspect inspects a project's models by running them.
modelschema
Package modelschema resolves what a model struct declares about its table: its database columns, the kind of value each stores and the URL query name each takes (see Column and ClassifyColumn), the columns the framework fixes the names of (see ValidateFrameworkColumns and DefaultCursorColumn), the secondary indexes it declares (see Index), its optimistic-locking column (see Version), the columns identifying one of its rows (see IdentifyingColumns), its associations (see AssociationFields) and the fields it declares read-only to clients (see IsReadOnly); and the fingerprint of everything a set of models declares about their tables (see Fingerprint).
Package modelschema resolves what a model struct declares about its table: its database columns, the kind of value each stores and the URL query name each takes (see Column and ClassifyColumn), the columns the framework fixes the names of (see ValidateFrameworkColumns and DefaultCursorColumn), the secondary indexes it declares (see Index), its optimistic-locking column (see Version), the columns identifying one of its rows (see IdentifyingColumns), its associations (see AssociationFields) and the fields it declares read-only to clients (see IsReadOnly); and the fingerprint of everything a set of models declares about their tables (see Fingerprint).
modelsource
Package modelsource reads the models of a project the way gg sees them: the model files under the model directory, each model with the design its DSL declares, its routes resolved through the resource hierarchy and the gst.yaml ignore rules, the service files its actions map to, and the doc comments the API document registers.
Package modelsource reads the models of a project the way gg sees them: the model files under the model directory, each model with the design its DSL declares, its routes resolved through the resource hierarchy and the gst.yaml ignore rules, the service files its actions map to, and the doc comments the API document registers.
openapigen
Package openapigen builds the OpenAPI document a running service serves.
Package openapigen builds the OpenAPI document a running service serves.
operation
Package operation begins the framework's own background operations — a round of a cron job, a tenure of a leader work — with the identity the rest of the operation is annotated with: a trace id, taken from a root span when tracing is on and generated otherwise, the way a request takes its own at the listener.
Package operation begins the framework's own background operations — a round of a cron job, a tenure of a leader work — with the identity the rest of the operation is annotated with: a trace id, taken from a root span when tracing is on and generated otherwise, the way a request takes its own at the listener.
otel
Package otel is the framework's OpenTelemetry tracing: it builds the tracer provider from the otel configuration and exports spans over OTLP, names the spans the framework owns, and records errors and recovered panics on them.
Package otel is the framework's OpenTelemetry tracing: it builds the tracer provider from the otel configuration and exports spans over OTLP, names the spans the framework owns, and records errors and recovered panics on them.
pbschema
Package pbschema derives the protobuf definitions of a gst project's gRPC services: for every model whose Design declares GRPC(), the messages its Go types encode to and the service exposing its actions, as the descriptors of the .proto files that mirror the model directory under pb/, held to the files already committed there (see Derive).
Package pbschema derives the protobuf definitions of a gst project's gRPC services: for every model whose Design declares GRPC(), the messages its Go types encode to and the service exposing its actions, as the descriptors of the .proto files that mirror the model directory under pb/, held to the files already committed there (see Derive).
requestctx
Package requestctx carries what names a request below its handler.
Package requestctx carries what names a request below its handler.
router
Package router builds the HTTP server a gst process serves: the engine carrying the framework's middleware chain and operational endpoints, the route groups registered routes attach to, the routes-ready hooks, and the server's start and shutdown.
Package router builds the HTTP server a gst process serves: the engine carrying the framework's middleware chain and operational endpoints, the route groups registered routes attach to, the routes-ready hooks, and the server's start and shutdown.
service
Package service holds what stands behind the public service package: the base every service embeds, with the hooks it defaults and the query helpers it lends, the registry of the services the controllers run, and the detection of the hooks a service overrides.
Package service holds what stands behind the public service package: the base every service embeds, with the hooks it defaults and the query helpers it lends, the registry of the services the controllers run, and the detection of the hooks a service overrides.
sse
Package sse implements Server-Sent Events (SSE) for the framework.
Package sse implements Server-Sent Events (SSE) for the framework.
streamctx
Package streamctx gives a stream a context that ends when the server it is served on begins to shut down.
Package streamctx gives a stream a context that ends when the server it is served on begins to shut down.
structdoc
Package structdoc parses Go source code and extracts the doc comments of exported struct declarations and their exported fields.
Package structdoc parses Go source code and extracts the doc comments of exported struct declarations and their exported fields.
tenant
Package tenant implements the row scoping the public tenant package forwards: the column type whose clauses Gorm applies to every statement of a schema carrying it, the struct a model embeds to carry that column, and the context a statement takes its tenant from.
Package tenant implements the row scoping the public tenant package forwards: the column type whose clauses Gorm applies to every statement of a schema carrying it, the struct a model embeds to carry that column, and the context a statement takes its tenant from.
testutil
Package testutil is the test support the public testutil package forwards to projects and the framework's own suites run on.
Package testutil is the test support the public testutil package forwards to projects and the framework's own suites run on.
testutil/golist
Package golist reads the packages of the module a test runs in through go list, for a test that reads the source of packages other than its own.
Package golist reads the packages of the module a test runs in through go list, for a test that reads the source of packages other than its own.
testutil/oteltest
Package oteltest turns real OpenTelemetry tracing on inside one framework test and reads back the spans it exports.
Package oteltest turns real OpenTelemetry tracing on inside one framework test and reads back the spans it exports.
testutil/providertest
Package providertest runs the tests of a provider package against the external services they need, with the framework brought up as far as its provider stage and no further.
Package providertest runs the tests of a provider package against the external services they need, with the framework brought up as far as its provider stage and no further.
testutil/swap
Package swap replaces process-wide values for the duration of one test.
Package swap replaces process-wide values for the duration of one test.
testutil/testcontainer
Package testcontainer prepares the services the framework's tests run against, in containers started through testcontainers-go.
Package testcontainer prepares the services the framework's tests run against, in containers started through testcontainers-go.
testutil/testlog
Package testlog keeps the framework's logs of a test process out of the test output and out of the package source tree, where log files changing with every run would make go's test cache rerun every test that reads or lists the tree.
Package testlog keeps the framework's logs of a test process out of the test output and out of the package source tree, where log files changing with every run would make go's test cache rerun every test that reads or lists the tree.
tlsconfig
Package tlsconfig builds the TLS configuration a client of the framework dials with, from the four settings every component configures it by: the certificate file, its key file, the CA file and whether to skip verification.
Package tlsconfig builds the TLS configuration a client of the framework dials with, from the four settings every component configures it by: the certificate file, its key file, the CA file and whether to skip verification.
tunnel
Package tunnel is the communication protocol between a server and its users, and between a server and its clients.
Package tunnel is the communication protocol between a server and its users, and between a server and its clients.
urlquery
Package urlquery translates URL query parameters into the arguments the database layer builds a query from.
Package urlquery translates URL query parameters into the arguments the database layer builds a query from.
websocket
Package websocket serves and dials the WebSocket connections a stream travels on over HTTP: every message one text frame holding a JSON document, the client's last one followed by a frame holding null.
Package websocket serves and dials the WebSocket connections a stream travels on over HTTP: every message one text frame holding a JSON document, the client's last one followed by a frame holding null.
Package leader runs, on one replica of a deployment at a time, the work a project registers: a loop that polls an external system, a relay that forwards an outbox, the one connection a protocol allows.
Package leader runs, on one replica of a deployment at a time, the work a project registers: a loop that polls an external system, a relay that forwards an outbox, the one connection a protocol allows.
Package lock runs a piece of work once at a time across a deployment: an administrator's "rebuild the report", a refresh of a credential every replica shares, anything that must not run twice at once and is done when it returns.
Package lock runs a piece of work once at a time across a deployment: an administrator's "rebuild the report", a refresh of a credential every replica shares, anything that must not run twice at once and is done when it returns.
Package logger is where the code of a project logs.
Package logger is where the code of a project logs.
Package middleware is the HTTP middleware a project mounts: Register adds middleware to every API route, RegisterAuth to the routes of the authenticated group, and the constructors here build the middleware the framework ships for either.
Package middleware is the HTTP middleware a project mounts: Register adds middleware to every API route, RegisterAuth to the routes of the authenticated group, and the constructors here build the middleware the framework ships for either.
ratelimiter
Package ratelimiter is a gin middleware limiting the rate of requests per key, with buckets kept in the process: a request over the limit is refused with 429.
Package ratelimiter is a gin middleware limiting the rate of requests per key, with buckets kept in the process: a request over the limit is refused with 429.
Package model is what a project declares its models with: the base types a model embeds, Base, AutoBase and Empty; Query and Cursor, which a model embeds to shape how its list is read; Index, which declares an index, and Version, which opts a model into optimistic locking; and Register, which registers a database-backed model for table setup.
Package model is what a project declares its models with: the base types a model embeds, Base, AutoBase and Empty; Query and Cursor, which a model embeds to shape how its list is read; Index, which declares an index, and Version, which opts a model into optimistic locking; and Register, which registers a database-backed model for table setup.
Package module provides a unified module registration system that automatically registers models, services, and HTTP routes for CRUD operations.
Package module provides a unified module registration system that automatically registers models, services, and HTTP routes for CRUD operations.
authz
Package authz is the RBAC authorization module: roles, role bindings, authorization rules and menus, with the routes that manage them.
Package authz is the RBAC authorization module: roles, role bindings, authorization rules and menus, with the routes that manage them.
column
Package column is the module that answers which distinct values the columns of a table hold, for the tables and columns a project registers, under the filters a request names among those columns.
Package column is the module that answers which distinct values the columns of a table hold, for the tables and columns a project registers, under the filters a request names among those columns.
email
Package email is the module of email verification, password reset by email and email change for IAM accounts.
Package email is the module of email verification, password reset by email and email change for IAM accounts.
helloworld
Package helloworld is the complete module example projects are pointed to: one module on an empty model with request and response types of its own, and one on a table-backed model with before and after hooks for each action.
Package helloworld is the complete module example projects are pointed to: one module on an empty model with request and response types of its own, and one on a table-backed model with before and after hooks for each action.
iam
Package iam is the identity and access module: users and their accounts, login, and sessions, with the routes that serve them.
Package iam is the identity and access module: users and their accounts, login, and sessions, with the routes that serve them.
logmgmt
Package logmgmt is the module of login logs and operation logs: the two models and the routes that read them.
Package logmgmt is the module of login logs and operation logs: the two models and the routes that read them.
mfa
Package mfa is the module of multi-factor authentication by TOTP: the routes an account binds, confirms and unbinds its authenticator with, the administrative routes over the authenticators of other accounts, and the second-factor gate of the login.
Package mfa is the module of multi-factor authentication by TOTP: the routes an account binds, confirms and unbinds its authenticator with, the administrative routes over the authenticators of other accounts, and the second-factor gate of the login.
version
Package versionmod provides the version API module; the name avoids conflicting with the standard library "runtime/version" package.
Package versionmod provides the version API module; the name avoids conflicting with the standard library "runtime/version" package.
Package otel traces a project's own work beside the spans the framework records for every request.
Package otel traces a project's own work beside the spans the framework records for every request.
provider
cassandra
Package cassandra is the Cassandra provider: Client returns the session the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
Package cassandra is the Cassandra provider: Client returns the session the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
clickhouse
Package clickhouse provides the native ClickHouse client for analytical workloads: high-throughput batch ingestion and queries that bypass the gorm dialect.
Package clickhouse provides the native ClickHouse client for analytical workloads: high-throughput batch ingestion and queries that bypass the gorm dialect.
elastic
Package elastic is the Elasticsearch provider: Client returns the client the bootstrap opened from the configuration, New opens another from a configuration of the caller's, and the helpers read, search and write documents, manage indexes and compose queries.
Package elastic is the Elasticsearch provider: Client returns the client the bootstrap opened from the configuration, New opens another from a configuration of the caller's, and the helpers read, search and write documents, manage indexes and compose queries.
etcd
Package etcd is the etcd provider: Client returns the client the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
Package etcd is the etcd provider: Client returns the client the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
influxdb
Package influxdb is the InfluxDB provider: Client returns the client the bootstrap opened from the configuration, the helpers query and write points through it, and New opens another from a configuration of the caller's.
Package influxdb is the InfluxDB provider: Client returns the client the bootstrap opened from the configuration, the helpers query and write points through it, and New opens another from a configuration of the caller's.
kafka
Package kafka is the Kafka provider: Client returns the default client the bootstrap opened from the configuration, meant for producing and for administration, Admin returns an admin client over it, and New opens a client of the caller's own, which is how a consumer gets one carrying its subscription.
Package kafka is the Kafka provider: Client returns the default client the bootstrap opened from the configuration, meant for producing and for administration, Admin returns an admin client over it, and New opens a client of the caller's own, which is how a consumer gets one carrying its subscription.
ldap
Package ldap is the LDAP provider: Client returns the connection the bootstrap opened from the configuration, the helpers search the directory, authenticate a user and manage users and groups through it, and New opens another from a configuration of the caller's.
Package ldap is the LDAP provider: Client returns the connection the bootstrap opened from the configuration, the helpers search the directory, authenticate a user and manage users and groups through it, and New opens another from a configuration of the caller's.
minio
Package minio is the MinIO provider: Client returns the client the bootstrap opened from the configuration, the helpers work on buckets and the objects in them, and New opens another from a configuration of the caller's.
Package minio is the MinIO provider: Client returns the client the bootstrap opened from the configuration, the helpers work on buckets and the objects in them, and New opens another from a configuration of the caller's.
mongo
Package mongo is the MongoDB provider: Client returns the client the bootstrap opened from the configuration, Database and Collection return handles of it, and New opens another from a configuration of the caller's.
Package mongo is the MongoDB provider: Client returns the client the bootstrap opened from the configuration, Database and Collection return handles of it, and New opens another from a configuration of the caller's.
mqtt
Package mqtt is the MQTT provider: Client returns the client the bootstrap opened from the configuration, Publish, Subscribe and Unsubscribe work on topics through it, and New opens another from a configuration of the caller's.
Package mqtt is the MQTT provider: Client returns the client the bootstrap opened from the configuration, Publish, Subscribe and Unsubscribe work on topics through it, and New opens another from a configuration of the caller's.
nats
Package nats is the NATS provider: Client returns the connection the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
Package nats is the NATS provider: Client returns the connection the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
rethinkdb
Package rethinkdb is the RethinkDB provider: Client returns the session the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
Package rethinkdb is the RethinkDB provider: Client returns the session the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
rocketmq
Package rocketmq is the RocketMQ provider: Producer and Admin return the producer and the admin client the bootstrap started from the configuration, and NewProducer, NewPushConsumer and NewAdmin build ones of the caller's own.
Package rocketmq is the RocketMQ provider: Producer and Admin return the producer and the admin client the bootstrap started from the configuration, and NewProducer, NewPushConsumer and NewAdmin build ones of the caller's own.
scylla
Package scylla is the ScyllaDB provider: Client returns the session the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
Package scylla is the ScyllaDB provider: Client returns the session the bootstrap opened from the configuration, and New opens another from a configuration of the caller's.
Package redis holds the process-wide Redis client, the typed cache backend built on it, and the helpers that share its keyspace.
Package redis holds the process-wide Redis client, the typed cache backend built on it, and the helpers that share its keyspace.
Package requestctx is the public entry to the request metadata that storage and service logs name a request by.
Package requestctx is the public entry to the request metadata that storage and service logs name a request by.
Package response exposes the response entry points that code outside the framework's controller path needs.
Package response exposes the response entry points that code outside the framework's controller path needs.
Package router registers a project's HTTP routes and the hooks that run once they are ready.
Package router registers a project's HTTP routes and the hooks that run once they are ready.
Package service exposes the public service extension points for application code: Base, the no-op service an application service embeds, Register, which registers a service type for a phase of a route, and the phases of an action a registration names, Create or List.
Package service exposes the public service extension points for application code: Base, the no-op service an application service embeds, Register, which registers a service type for a phase of a route, and the phases of an action a registration names, Create or List.
Package sse exposes the framework's Server-Sent Events support to application code.
Package sse exposes the framework's Server-Sent Events support to application code.
Package tenant scopes a model's rows to the tenant the caller acts in.
Package tenant scopes a model's rows to the tenant the caller acts in.
Package testutil is the test suite a gst project writes its tests against.
Package testutil is the test suite a gst project writes its tests against.
iamtest
Package iamtest logs a test in through the iam module, the way a client of the project does, and hands it the session: as the cookie the returned client carries on every request, and as the session id a gRPC call names with grpc.WithBearer.
Package iamtest logs a test in through the iam module, the way a client of the project does, and hands it the session: as the cookie the returned client carries on every request, and as the session id a gRPC call names with grpc.WithBearer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL