All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references核心组件配置管理配置管理-配置监听回调.md

基本介绍

从v2.10.0版本开始,gcfg组件新增WatcherAdapter接口,WatcherAdapter是gcfg包中的一个接口,用于实现配置文件变化监听功能。当配置文件被修改、创建、删除或权限发生变化时,注册的观察者会收到通知。

GoFrame的配置监听功能为应用程序提供了动态响应配置变化的能力,使系统能够实时调整行为而无需重启。通过灵活的监听器机制和丰富的context信息,开发者可以轻松构建自适应的应用程序。

接口定义

https://github.com/gogf/gf/blob/master/os/gcfg/gcfg_adaper.go

// WatcherAdapter is the interface for configuration watcher.
type WatcherAdapter interface {
	// AddWatcher adds a watcher function for specified `pattern` and `resource`.
	AddWatcher(name string, fn func(ctx context.Context))
	// RemoveWatcher removes the watcher function for specified `pattern` and `resource`.
	RemoveWatcher(name string)
	// GetWatcherNames returns all watcher names.
	GetWatcherNames() []string
}

适配器适配情况:

适配器名称 是否支持监听回调
AdapterFile 支持
AdapterContent 支持
polaris 支持
nacos 支持
apollo 支持
consul 支持
kubecm 支持

核心概念

  1. WatcherAdapter 接口:定义了添加、移除和获取观察者的方法
  2. WatcherRegistry:观察者注册管理器,提供了统一的观察者管理实现
  3. context上下文传递:通过 context 传递配置变更的详细信息

主要功能

  1. 文件监控:自动监控配置文件的变化(写入、创建、删除、重命名、权限更改)
  2. 手动触发:支持手动触发观察者回调(如 Set 操作)
  3. 并发安全:所有操作都是并发安全的
  4. panic 防护:每个观察者的执行都在独立的 goroutine 中,并有 panic 恢复机制

使用方法

让我通过示例代码展示如何使用 WatcherAdapter 功能:

1. 文件适配器的观察者使用

// 创建配置适配器
c, err := gcfg.NewAdapterFile("config.toml")
if err != nil {
    panic(err)
}

// 添加观察者
c.AddWatcher("my-watcher", func(ctx context.Context) {
    // 获取文件操作上下文信息
    fileCtx := gcfg.GetAdapterFileCtx(ctx)
    
    fmt.Printf("操作类型: %s\n", fileCtx.GetOperation())
    fmt.Printf("文件名: %s\n", fileCtx.GetFileName())
    fmt.Printf("文件路径: %s\n", fileCtx.GetFilePath())
    
    // 根据不同操作类型处理
    switch fileCtx.GetOperation() {
    case gcfg.OperationWrite:
        fmt.Println("配置文件被修改了")
    case gcfg.OperationSet:
        fmt.Printf("配置项 %s 被设置为 %v\n", fileCtx.GetKey(), fileCtx.GetValue())
    }
})

// 修改配置会触发观察者
c.Set("key", "new-value")

2. 内容适配器的观察者使用

// 创建内容适配器
content := `{"key": "value"}`
adapter, err := gcfg.NewAdapterContent(content)
if err != nil {
    panic(err)
}

// 添加观察者
adapter.AddWatcher("content-watcher", func(ctx context.Context) {
    contentCtx := gcfg.GetAdapterContentCtx(ctx)
    fmt.Printf("内容操作: %s\n", contentCtx.GetOperation())
})

// 设置新内容会触发观察者
adapter.SetContent(`{"key": "new-value"}`)

3. 与 gcfg.Config 实例一起使用

// 创建配置实例
config := g.Cfg()

// 添加观察者到配置实例的适配器
if adapter, ok := config.GetAdapter().(gcfg.WatcherAdapter); ok {
    adapter.AddWatcher("config-watcher", func(ctx context.Context) {
        fmt.Println("配置发生了变化")
    })
}

观察者上下文信息

根据不同适配器类型,可以通过相应的上下文获取不同的信息,具体可以参考不同类型适配器的xxxAdapterCtx实现。

例如nacos可以通过adapterCtx := nacos.GetAdapterCtx(ctx)获取*NacosAdapterCtx

位于: contrib/config/nacos/nacos_adapter_ctx.go

上下文键常量:

  • ContextKeyNamespace: 命名空间
  • ContextKeyGroup: 组
  • ContextKeyDataId: 数据ID
  • gcfg.ContextKeyOperation: 操作类型(继承自gcfg)
  • gcfg.ContextKeyContent: 设置的内容(继承自gcfg)

方法:

  • WithOperation(): 设置操作类型
  • WithNamespace(): 设置命名空间
  • WithGroup(): 设置组
  • WithDataId(): 设置数据ID
  • WithContent(): 设置内容
  • GetNamespace(): 获取命名空间
  • GetGroup(): 获取组
  • GetDataId(): 获取数据ID
  • GetContent(): 获取内容
  • GetOperation(): 获取操作类型

实际应用场景

  1. 动态配置更新:当配置文件被修改时,应用程序可以自动重新加载配置
  2. 日志记录:记录配置变更历史
  3. 通知机制:配置变更时通知其他组件
  4. 缓存清理:配置变更时清理相关缓存

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.