app

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFromSchema added in v2.1.0

func DefaultFromSchema(s openapi.Schema, depth int) any

DefaultFromSchema 按 schema 递归生成默认值(带深度上限).

类型缺省值规则: string -> "", 整数 -> 0, 浮点 -> 0, bool -> false, 对象 -> {字段: 默认值}, 数组 -> [], map -> {}. schema 自身已带 Default(字段上的 default tag 已设置过)时优先采用.

func ExampleFromSchema added in v2.1.0

func ExampleFromSchema(s openapi.Schema, depth int) any

ExampleFromSchema 按 schema 递归生成示例值(带深度上限).

schema 自身已带 Example(即字段上的 example tag 已设置过)时优先采用, 否则按类型给占位示例: string -> "string", 整数 -> 1, 浮点 -> 1.5, bool -> true.

func RegisterAutoRegistrar

func RegisterAutoRegistrar(reg AutoRegistrar)

Types

type AutoRegistrar

type AutoRegistrar func(*Engine) error

type BindSource

type BindSource string
const (
	BindAuto   BindSource = "auto"
	BindQuery  BindSource = "query"
	BindPath   BindSource = "path"
	BindHeader BindSource = "header"
	BindBody   BindSource = "body"
)

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

func New

func New(configPath string) (*Engine, error)

New 创建引擎并加载配置.

configPath 为 YAML 配置文件路径, 传空串时使用 config 包缺省的 config/config.yaml. 配置按 app/config.go 里 EngineConfig 的 inject tag 注入, 文件中缺失的段保留缺省值.

func (*Engine) Build

func (e *Engine) Build() error

func (*Engine) Container

func (e *Engine) Container() inject.Injector

func (*Engine) EmbedProject

func (e *Engine) EmbedProject(data []byte)

EmbedProject sets pre-generated AST metadata (the contents of .astp.json). When set, Build() uses this data instead of parsing Go source files at runtime.

Typical usage with go:generate + go:embed:

//go:generate go run github.com/linxlib/fw/v2/astp/cmd/astp -exported .
//go:embed .astp.json
var astpData []byte

func main() {
    e, _ := app.New("")
    e.EmbedProject(astpData)
    // ...
}

func (*Engine) EnableConfigReload added in v2.1.0

func (e *Engine) EnableConfigReload(interval time.Duration)

EnableConfigReload 开启配置文件热重载(缺省关闭, 需显式开启).

interval 为轮询间隔, <=0 时按 1s 处理. 开启后按顶层 section 增量重载: 只有内容 发生变化的 section 对应的内存配置被重写, config 包同时在标准输出打印 "config: reload detected, changed sections: [...]" 提示本次变化. 路由表与监听地址在 Build/ListenAndServe 时已确定, 重载不会重建它们, 因此应在 New 之后、ListenAndServe 之前调用.

func (*Engine) ListenAndServe

func (e *Engine) ListenAndServe() error

func (*Engine) RegisterController

func (e *Engine) RegisterController(controller any)

func (*Engine) RegisterMiddleware

func (e *Engine) RegisterMiddleware(mw middleware.Middleware)

func (*Engine) SetResponse

func (e *Engine) SetResponse(formatter response.Formatter, encoder response.Encoder)

func (*Engine) Use

func (e *Engine) Use(mw middleware.Middleware)

type EngineConfig added in v2.1.0

type EngineConfig struct {
	Server      ServerConfig              `yaml:"server"      inject:"server"`
	Log         LogConfig                 `yaml:"log"         inject:"log"`
	OpenAPI     OpenAPIConfig             `yaml:"openapi"     inject:"openapi"`
	Recovery    RecoveryConfig            `yaml:"recovery"    inject:"recovery"`
	ProjectDir  string                    `yaml:"project_dir" inject:"project_dir"`
	Middlewares map[string]config.Section `yaml:"middlewares" inject:"middlewares"`
}

EngineConfig 是 fw 引擎的配置根, 通过 config.Config.LoadByTags 按 inject tag 注入.

每个字段同时带 yaml tag(配置文件里的段名)与 inject tag(配置 key), 段缺失时保留 default tag 给出的缺省值, 因此配置文件可以只写关心的部分.

func DefaultEngineConfig added in v2.1.0

func DefaultEngineConfig() EngineConfig

DefaultEngineConfig 返回引擎配置的缺省值(等价于配置文件全部缺省时的结果).

type LogConfig added in v2.1.0

type LogConfig struct {
	Level          string `yaml:"level"            inject:"level"             default:"\"info\""`
	Output         string `yaml:"output"           inject:"output"            default:"\"console\""`
	FilePath       string `yaml:"file_path"        inject:"file_path"         default:"\"\""`
	EnableFile     bool   `yaml:"enable_file"      inject:"enable_file"       default:"false"`
	RequestEnabled bool   `yaml:"request_enabled"  inject:"request_enabled"   default:"true"`
}

LogConfig 日志配置.

type OpenAPIConfig added in v2.1.0

type OpenAPIConfig struct {
	Enabled bool   `yaml:"enabled" inject:"enabled" default:"true"`
	Output  string `yaml:"output"  inject:"output"  default:"\"openapi.json\""`
	Title   string `yaml:"title"   inject:"title"   default:"\"fw API\""`
	Version string `yaml:"version" inject:"version" default:"\"1.0.0\""`
}

OpenAPIConfig OpenAPI 文档生成配置.

type ParamHint

type ParamHint struct {
	Name   string
	Source BindSource
}

type RecoveryConfig added in v2.1.0

type RecoveryConfig struct {
	Enabled           bool `yaml:"enabled"               inject:"enabled"                default:"true"`
	ReturnStackToBody bool `yaml:"return_stack_to_body"  inject:"return_stack_to_body"   default:"false"`
}

RecoveryConfig panic 恢复配置.

type ServerConfig added in v2.1.0

type ServerConfig struct {
	Host string `yaml:"host" inject:"host" default:"\"0.0.0.0\""`
	Port int    `yaml:"port" inject:"port" default:"8080"`
}

ServerConfig 监听地址配置.

Jump to

Keyboard shortcuts

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