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 Server 关闭了静态文件服务的功能,如果开发者配置了 静态文件目录,那么静态文件服务将会自动开启。

静态文件服务涉及到的常用配置方法如下:

// 设置http server参数 - ServerRoot
func (s *Server) SetServerRoot(root string)

// 添加静态文件搜索目录,必须给定目录的绝对路径
func (s *Server) AddSearchPath(path string)

// 设置http server参数 - IndexFiles,默认展示文件,如:index.html, index.htm
func (s *Server) SetIndexFiles(index []string)

// 是否允许展示访问目录的文件列表
func (s *Server) SetIndexFolder(enabled bool)

// 添加URI与静态目录的映射
func (s *Server) AddStaticPath(prefix string, path string)

// 静态文件服务总开关:是否开启/关闭静态文件服务
func (s *Server) SetFileServerEnabled(enabled bool)

// 设置URI重写规则
func (s *Server) SetRewrite(uri string, rewrite string)

// 设置URI重写规则(批量)
func (s *Server) SetRewriteMap(rewrites map[string]string)

简要介绍:

  1. IndexFiles 为当访问目录时默认检索的文件名称列表(按照slice先后顺序进行检索),当检索的文件存在时则返回文件内容,否则展示目录列表( SetIndexFolder 为 true 时),默认的 IndexFiles 为: index.html, index.htm;
  2. SetIndexFolder 为设置是否在用户访问文件目录,且没有在目录下检索到 IndexFiles 时,则展示目录下的文件列表,默认为关闭;
  3. SetServerRoot 为设置默认提供服务的静态文件目录,该目录会被自动添加到 SearchPath 中的第一个搜索路径;
  4. AddSearchPath 为添加静态文件检索目录,可以有多个,按照文件目录添加的先后顺序执行优先级检索;
  5. AddStaticPath 为添加 URI 与目录路径的映射关系,可以自定义静态文件目录的访问URI规则;
  6. SetRewrite/ SetRewriteMap 为重写规则设置(类似于 nginx 的 rewrite),严格上来讲不仅仅是静态文件服务,当然也支持动态的路由注册的 rewrite; :::tip 设置静态文件服务的目录路径时,可以使用绝对路径,也可以使用相对路径,例如设置当前运行目录提供静态文件服务可以使用 SetServerRoot(".")。

开发者可以设置多个文件目录来提供静态文件服务,并且可以设置目录及URI的优先级,但是一旦通过 SetFileServerEnabled 关闭了静态服务,所有静态文件/目录的访问都将失效。 :::

示例1, 基本使用

package main

import "github.com/gogf/gf/v2/frame/g"

// 静态文件服务器基本使用
func main() {
    s := g.Server()
    s.SetIndexFolder(true)
    s.SetServerRoot("/Users/john/Temp")
    s.AddSearchPath("/Users/john/Documents")
    s.SetPort(8199)
    s.Run()
}

示例2,静态目录映射

package main

import "github.com/gogf/gf/v2/frame/g"

// 静态文件服务器,支持自定义静态目录映射
func main() {
    s := g.Server()
    s.SetIndexFolder(true)
    s.SetServerRoot("/Users/john/Temp")
    s.AddSearchPath("/Users/john/Documents")
    s.AddStaticPath("/my-doc", "/Users/john/Documents")
    s.SetPort(8199)
    s.Run()
}

示例3,静态目录映射,优先级控制

静态目录映射的优先级按照绑定的 URI 精准度进行控制,绑定的URI越精准(深度优先匹配),那么优先级越高。

package main

import "github.com/gogf/gf/v2/frame/g"

// 静态文件服务器,支持自定义静态目录映射
func main() {
    s := g.Server()
    s.SetIndexFolder(true)
    s.SetServerRoot("/Users/john/Temp")
    s.AddSearchPath("/Users/john/Documents")
    s.AddStaticPath("/my-doc", "/Users/john/Documents")
    s.AddStaticPath("/my-doc/test", "/Users/john/Temp")
    s.SetPort(8199)
    s.Run()
}

其中,访问 /my-doc/test 的优先级会比 /my-doc 高,因此假如 /Users/john/Documents 目录下存在 test 目录(与自定义的 /my-doc/test 冲突),将会无法被访问到。

示例4, URI 重写

GoFrame 框架的静态文件服务支持将任意的 URI 重写,替换为制定的 URI,使用 SetRewrite/SetRewriteMap 方法。

示例,在 /Users/john/Temp 目录下只有两个文件 test1.html 及 test2.html。

package main

import "github.com/gogf/gf/v2/frame/g"

func main() {
    s := g.Server()
    s.SetServerRoot("/Users/john/Temp")
    s.SetRewrite("/test.html", "/test1.html")
    s.SetRewriteMap(g.MapStrStr{
        "/my-test1": "/test1.html",
        "/my-test2": "/test2.html",
    })
    s.SetPort(8199)
    s.Run()
}

执行后,

  1. 当我们访问 /test.html,其实最终被重写到了 test1.html,返回的是该文件内容;
  2. 当我们访问 /my-test1,其实最终被重写到了 test1.html,返回的是该文件内容;
  3. 当我们访问 /my-test2,其实最终被重写到了 test2.html,返回的是该文件内容;

示例5,跨域

package main

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

func beforeServeHook(r *ghttp.Request) {
    glog.Debugf(r.GetCtx(), "beforeServeHook [is file:%v] URI:%s", r.IsFileRequest(), r.RequestURI)
    r.Response.CORSDefault()
}

// 利用hook注入跨域配置
func main() {
    s := g.Server()
    s.BindHookHandler("/*", ghttp.HookBeforeServe, beforeServeHook)
    s.SetServerRoot(".")
    s.SetFileServerEnabled(true)
    s.SetAddr(":8080")
    s.Run()
}

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.