bootstrap

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 18 Imported by: ? modules, 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.

Documentation

Overview

Package bootstrap 把域服务 main() 的固定装配收进框架。

域服务的启动顺序是死的:加载配置 → 初始化遥测 → 建连接池 → 建事件总线 → 装配模块 → 运行 → 反序关停。这段仪式每个域仓库一字不差,却最容易被改坏, 且改坏了框架无从知情——少一个 tel.Shutdown 就丢 span,pool.Close 提前一步 就在关停期查询失败,忘了 Migrator 就静默不跑迁移。

收进这里之后它位于 module cache(0444 只读),改不动也不必读。 用户仓库的 main 只声明"装哪些模块",见 Options。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main(o Options)

Main 是域服务 main() 的全部内容:解析 flag、装配、运行; 失败时打印到 stderr 并以退出码 1 结束。

需要自定义 flag 或注入 ctx 时改用 Run。

func Run

func Run(ctx context.Context, o Options, r RunOptions) error

Run 执行装配与运行,直到收到关停信号或运行期错误; r.MigrateOnly 为 true 时应用完迁移即返回。

Types

type Base

type Base struct {
	Addr     string   `koanf:"addr"`
	Env      string   `koanf:"env" validate:"omitempty,oneof=dev staging prod"`
	Database Database `koanf:"database"`
	Log      Log      `koanf:"log"`
	Debug    Debug    `koanf:"debug"`
}

Base 是每个域服务共有的配置项。需要额外配置项时,用 Deps.Config 再 config.Load 一次自己的结构——同一份文件与环境变量,不必重复声明这些。

type Database

type Database struct {
	// URL 形如 postgres://user:pass@host:5432/db?sslmode=disable。
	// 留空时默认拒绝启动;本地零依赖试跑必须显式传 -minimal。
	// 生产部署必须配置。
	URL string `koanf:"url"`
}

Database 是数据库配置。

type Debug

type Debug struct {
	// Pprof 为 true 时挂 /debug/pprof/*(语义见 appkit.Pprof)。
	Pprof bool `koanf:"pprof"`
}

Debug 是排障端点配置。全部默认关闭:pprof 暴露进程内部信息 (goroutine 栈、堆采样可能含敏感数据),开启前先确认服务端口 不暴露公网或网络策略已挡好。 放配置而不是代码开关:线上排障改 configmap 重启即生效,不必发版。

type Deps

type Deps struct {
	// Log 是已接入 trace 的服务日志器。
	Log *slog.Logger
	// Base 是已加载并填过默认值的框架配置。
	Base Base
	// Pool 是数据库连接池;最小模式下为 nil。
	Pool *pgxpool.Pool
	// Bus 是事件总线;最小模式下为 nil。
	Bus EventBus
	// Config 复现框架加载配置时的入参。需要额外配置项时:
	//   cfg, err := config.Load[myConfig](d.Config)
	Config config.Options
}

Deps 是框架已备好的依赖,传给 Options 的各回调。

func (Deps) IsMinimal

func (d Deps) IsMinimal() bool

IsMinimal 报告当前是否为显式最小模式。

type EventBus

type EventBus interface {
	outbox.Bus
	appkit.Subscriber
}

EventBus 同时是 relay 的投递出口(outbox.Bus)与消费者的订阅入口 (appkit.Subscriber)。DirectBus 是进程内实现;拆分部署换 NATS/Kafka 适配器,实现这两个接口即可,业务代码不变。

type Log

type Log struct {
	Level  string `koanf:"level" validate:"omitempty,oneof=debug info warn error"`
	Format string `koanf:"format" validate:"omitempty,oneof=json text"`
}

Log 是日志配置。

type Options

type Options struct {
	// Service 是服务名:遥测 service.name 与环境变量前缀的来源。必填。
	Service string
	// EnvPrefix 覆盖环境变量前缀,默认 strings.ToUpper(Service)。
	EnvPrefix string
	// DefaultAddr 覆盖默认监听地址,默认 ":8080"。
	DefaultAddr string
	// Modules 声明本进程可装配的模块全集(实际启用集由 -target 决定)。
	// 完整模式下 Deps.Pool 与 Deps.Bus 非 nil。必填。
	Modules func(Deps) ([]appkit.Module, error)
	// Minimal 声明显式 -minimal 模式装配的模块(通常挂个 ping 证明服务能起)。
	// 仅 env=dev 可启用;Deps.Pool 与 Deps.Bus 为 nil。提供此回调不会授权
	// database.url 缺失时自动降级,运行者仍必须显式传 -minimal。
	Minimal func(Deps) ([]appkit.Module, error)
	// AppOptions 追加 appkit.Option,典型用途是 appkit.Remote 绑定外域契约。
	AppOptions func(Deps) []appkit.Option
	// NewBus 覆盖默认事件总线(默认 outbox.NewDirectBus())。
	NewBus func() EventBus
	// AllowDirectBusForSplit 明确允许 target != all 时仍使用默认 DirectBus。
	// 这只适用于调用方确认事件不会跨进程的特殊部署;默认拒绝,避免生产者侧
	// 无本地订阅者却误把拆分部署当成可工作的跨进程事件总线。
	AllowDirectBusForSplit bool
	// PoolOptions 追加连接池配置,随 bootstrap 建的生产池生效:
	// pgtx.WithAfterConnect 装 per-connection 钩子(otelpgx tracer、
	// 会话级 GUC),pgtx.WithMaxConns 调容量。留空即框架默认。
	// 此前域要给池加钩子只能整个自建池绕开 bootstrap——迁移/outbox/
	// 幂等装配全部重写一遍,正是这条例外最不该存在的地方。
	PoolOptions []pgtx.PoolOption
	// AuthnPublicKey 是访问令牌的 Ed25519 验签公钥(组合根只持公钥,
	// 私钥在鉴权提供方)。security.mode=user_facing 时必填,并在根链
	// Base 之后挂 authn.Middleware:
	// 验签结果(appkit.Actor)注入 ctx,reg.Require / appkit.Check 据此
	// 判定。disabled 模式不得同时提供;内部服务身份由独立配置负责。
	AuthnPublicKey ed25519.PublicKey
	// AuthnIssuer 是期望的令牌 iss(提供方按分区签发,如 "rbac-demo")。
	// user_facing 模式必填,缺了启动报错——没有 iss 约束的验签
	// 会接受任何持私钥者签的令牌。
	AuthnIssuer string
}

Options 声明一个域服务的装配。除 Service 与 Modules 外均可留空。

type RunOptions

type RunOptions struct {
	// Target 是本地实例化的模块集:"all" 或逗号分隔的模块名。
	Target string
	// ConfigFile 是配置文件路径,可缺省(此时只读环境变量)。
	ConfigFile string
	// MigrateOnly 为 true 时只应用迁移然后退出,不监听端口、不跑后台任务。
	MigrateOnly bool
	// Minimal 显式启用无数据库最小模式,仅允许 env=dev。
	Minimal bool
}

RunOptions 是运行期参数,对应 Main 解析出的命令行 flag。

Jump to

Keyboard shortcuts

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