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.

referencesWEB服务开发路由管理路由管理-路由注册路由注册-分组路由.md

分组路由是业务项目中主要使用的路由注册方式。

分组路由

GoFrame 框架支持分组路由的注册方式,可以给分组路由指定一个 prefix 前缀(也可以直接给定 / 前缀,表示注册在根路由下),在该分组下的所有路由注册都将注册在该路由前缀下。 分组路由注册方式也是推荐的路由注册方式。

接口文档: https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp#RouterGroup

// 创建分组路由
func (s *Server) Group(prefix string, groups ...func(g *RouterGroup)) *RouterGroup

// 注册Method路由
func (g *RouterGroup) ALL(pattern string, object interface{}, params...interface{})
func (g *RouterGroup) GET(pattern string, object interface{}, params...interface{})
func (g *RouterGroup) PUT(pattern string, object interface{}, params...interface{})
func (g *RouterGroup) POST(pattern string, object interface{}, params...interface{})
func (g *RouterGroup) DELETE(pattern string, object interface{}, params...interface{})
...

// 中间件绑定
func (g *RouterGroup) Middleware(handlers ...HandlerFunc) *RouterGroup

// 批量注册
func (g *RouterGroup) Map(m map[string]interface{})
func (g *RouterGroup) ALLMap(m map[string]interface{})

简要介绍:

  1. Group 方法用于创建一个分组路由对象,并且支持在指定域名对象上创建。
  2. 以 HTTP Method 命名的方法用于绑定指定的 HTTP Method 路由;其中 ALL 方法用于注册所有的 HTTP Method 到指定的函数/对象/控制器上; REST 方法用于注册 RESTful 风格的路由,需给定一个执行对象或者控制器对象。
  3. Middleware 方法用于绑定一个或多个中间件到当前分组的路由上,具体详见中间件章节。

简单示例

package main

import (
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

func main() {
    s := g.Server()
    s.Group("/api", func(group *ghttp.RouterGroup) {
        group.ALL("/all", func(r *ghttp.Request) {
            r.Response.Write("all")
        })
        group.GET("/get", func(r *ghttp.Request) {
            r.Response.Write("get")
        })
        group.POST("/post", func(r *ghttp.Request) {
            r.Response.Write("post")
        })
    })
    s.SetPort(8199)
    s.Run()
}

执行后,终端打印出路由表如下:

SERVER | DOMAIN  | ADDRESS | METHOD |   ROUTE   |     HANDLER     | MIDDLEWARE
|---------|---------|---------|--------|-----------|-----------------|------------|
  default | default | :8199   | ALL    | /api/all  | main.main.func1 |
|---------|---------|---------|--------|-----------|-----------------|------------|
  default | default | :8199   | GET    | /api/get  | main.main.func2 |
|---------|---------|---------|--------|-----------|-----------------|------------|
  default | default | :8199   | POST   | /api/post | main.main.func3 |
|---------|---------|---------|--------|-----------|-----------------|------------|

其中, /api/get 仅允许 GET 方式访问, /api/post 仅允许 POST 方式访问, /api/all 允许所有的方式访问。

我们使用 curl 工具来测试一下:

  1. /api/get
$ curl http://127.0.0.1:8199/api/get
get
$ curl -X POST http://127.0.0.1:8199/api/get
Not Found
  1. /api/post
$ curl http://127.0.0.1:8199/api/post
Not Found
$ curl -X POST http://127.0.0.1:8199/api/post post
  1. /api/all
$ curl http://127.0.0.1:8199/api/all
all
$ curl -X POST http://127.0.0.1:8199/api/all
all
$ curl -X DELETE http://127.0.0.1:8199/api/all
all
$ curl -X OPTIONS http://127.0.0.1:8199/api/all
all

层级注册

:::tip GoFrame 框架的层级路由注册方式灵感来源于 PHP Laravel 框架。 ::: 推荐使用路由层级注册方式,注册的路由代码更清晰直观。 GoFrame 框架的分组路由注册支持更加直观优雅层级的注册方式,以便于开发者更方便地管理路由列表。路由层级注册方式也是推荐的路由注册方式。我们来看一个比较完整的示例,该示例中注册了使用到了中间件、 HOOK 以及不同 HTTP Method 绑定的路由注册:

package main

import (
    "fmt"
    "net/http"

    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

func MiddlewareAuth(r *ghttp.Request) {
    if r.Get("token").String() != "123456" {
        r.Response.WriteStatus(http.StatusForbidden)
        return
    }
    r.Middleware.Next()
}

func MiddlewareCORS(r *ghttp.Request) {
    r.Response.CORSDefault()
    r.Middleware.Next()
}

func MiddlewareLog(r *ghttp.Request) {
    r.Middleware.Next()
    fmt.Println(r.Response.Status, r.URL.Path)
}

func main() {
    s := g.Server()
    s.Use(MiddlewareLog)
    s.Group("/api.v2", func(group *ghttp.RouterGroup) {
        group.Middleware(MiddlewareAuth, MiddlewareCORS)
        group.GET("/test", func(r *ghttp.Request) {
            r.Response.Write("test")
        })
        group.Group("/order", func(group *ghttp.RouterGroup) {
            group.GET("/list", func(r *ghttp.Request) {
                r.Response.Write("list")
            })
            group.PUT("/update", func(r *ghttp.Request) {
                r.Response.Write("update")
            })
        })
        group.Group("/user", func(group *ghttp.RouterGroup) {
            group.GET("/info", func(r *ghttp.Request) {
                r.Response.Write("info")
            })
            group.POST("/edit", func(r *ghttp.Request) {
                r.Response.Write("edit")
            })
            group.DELETE("/drop", func(r *ghttp.Request) {
                r.Response.Write("drop")
            })
        })
        group.Group("/hook", func(group *ghttp.RouterGroup) {
            group.Hook("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
                r.Response.Write("hook any")
            })
            group.Hook("/:name", ghttp.HookBeforeServe, func(r *ghttp.Request) {
                r.Response.Write("hook name")
            })
        })
    })
    s.SetPort(8199)
    s.Run()
}

执行后,注册的路由列表如下:

SERVER | DOMAIN  | ADDRESS | METHOD |        ROUTE         |       HANDLER       |               MIDDLEWARE
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | ALL    | /*                   | main.MiddlewareLog  | GLOBAL MIDDLEWARE
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | ALL    | /api.v2/hook/*       | main.main.func1.4.1 | HOOK_BEFORE_SERVE
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | ALL    | /api.v2/hook/:name   | main.main.func1.4.2 | HOOK_BEFORE_SERVE
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | GET    | /api.v2/order/list   | main.main.func1.2.1 | main.MiddlewareAuth,main.MiddlewareCORS
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | PUT    | /api.v2/order/update | main.main.func1.2.2 | main.MiddlewareAuth,main.MiddlewareCORS
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | GET    | /api.v2/test         | main.main.func1.1   | main.MiddlewareAuth,main.MiddlewareCORS
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | DELETE | /api.v2/user/drop    | main.main.func1.3.3 | main.MiddlewareAuth,main.MiddlewareCORS
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | POST   | /api.v2/user/edit    | main.main.func1.3.2 | main.MiddlewareAuth,main.MiddlewareCORS
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|
  default | default | :8199   | GET    | /api.v2/user/info    | main.main.func1.3.1 | main.MiddlewareAuth,main.MiddlewareCORS
|---------|---------|---------|--------|----------------------|---------------------|-----------------------------------------|

批量注册

Map

通过 Map 方法可以实现批量的分组路由注册,但是如果是相同的 URI 不同的 HTTP Method 需要按照路由规范给定 HTTP Method。使用示例:

package main

import (
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

func UserGet(r *ghttp.Request) {
    r.Response.Write("get")
}

func UserDelete(r *ghttp.Request) {
    r.Response.Write("delete")
}

func main() {
    s := g.Server()
    s.Group("/api", func(group *ghttp.RouterGroup) {
        group.Map(g.Map{
            "GET:    /user": UserGet,
            "DELETE: /user": UserDelete,
        })
    })
    s.SetPort(8199)
    s.Run()
}

AllMap

也可以使用 ALLMap 方法实现批量的分组路由注册,通过该方法注册的路由将把路由函数/对象应用到所有的 HTTP Method。使用示例:

s := g.Server()
// 前台系统路由注册
s.Group("/", func(group *ghttp.RouterGroup) {
    group.Middleware(service.Middleware.Ctx)
    group.ALLMap(g.Map{
        "/":            api.Index,          // 首页
        "/login":       api.Login,          // 登录
        "/register":    api.Register,       // 注册
        "/category":    api.Category,       // 栏目
        "/topic":       api.Topic,          // 主题
        "/topic/:id":   api.Topic.Detail,   // 主题 - 详情
        "/ask":         api.Ask,            // 问答
        "/ask/:id":     api.Ask.Detail,     // 问答 - 详情
        "/article":     api.Article,        // 文章
        "/article/:id": api.Article.Detail, // 文章 - 详情
        "/reply":       api.Reply,          // 回复
        "/search":      api.Search,         // 搜索
        "/captcha":     api.Captcha,        // 验证码
        "/user/:id":    api.User.Index,     // 用户 - 主页
    })
    // 权限控制路由
    group.Group("/", func(group *ghttp.RouterGroup) {
        group.Middleware(service.Middleware.Auth)
        group.ALLMap(g.Map{
            "/user":     api.User,     // 用户
            "/content":  api.Content,  // 内容
            "/interact": api.Interact, // 交互
            "/file":     api.File,     // 文件
        })
    })
})

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.