后端重构
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
{{- if .IsAdd }}
|
||||
// ==================== Wire Provider 注入说明 ====================
|
||||
// 请在 {{.Package}} 包的 enter.go 文件中的 ProviderSet 添加以下内容:
|
||||
// New{{.StructName}}Router,
|
||||
//
|
||||
// 同时在 RouterGroup 结构体中添加:
|
||||
// {{.StructName}}Router
|
||||
//
|
||||
// 并在 var 块中添加 handler 引用:
|
||||
// {{.Abbreviation}}Handler = handler.{{.StructName}}Handler{}
|
||||
{{- else }}
|
||||
package {{ .Package }}
|
||||
|
||||
import "github.com/google/wire"
|
||||
|
||||
// ProviderSet is {{.Package}} router providers.
|
||||
// 包含 {{.Description}} 相关的路由配置
|
||||
// 使用 Wire 进行依赖注入
|
||||
var ProviderSet = wire.NewSet(New{{.StructName}}Router)
|
||||
|
||||
// RouterGroup {{.Package}} 路由组
|
||||
// 包含该包下所有路由的集合
|
||||
type RouterGroup struct {
|
||||
{{.StructName}}Router
|
||||
}
|
||||
{{- end }}
|
||||
@@ -0,0 +1,75 @@
|
||||
package {{.Package}}
|
||||
|
||||
import (
|
||||
{{- if not .OnlyTemplate}}
|
||||
"{{.Module}}/internal/server/middleware"
|
||||
{{- else}}
|
||||
// "{{.Module}}/internal/server/middleware"
|
||||
{{- end}}
|
||||
"{{.Module}}/internal/server/handler/{{.Package}}"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// {{.StructName}}Router {{.Description}}路由
|
||||
// 负责注册 {{.Description}} 相关的 HTTP 路由
|
||||
type {{.StructName}}Router struct {
|
||||
handler *{{.Package}}.{{.StructName}}Handler
|
||||
}
|
||||
|
||||
// New{{.StructName}}Router 创建{{.Description}}路由
|
||||
// @param handler {{.Description}}处理器
|
||||
func New{{.StructName}}Router(handler *{{.Package}}.{{.StructName}}Handler) *{{.StructName}}Router {
|
||||
return &{{.StructName}}Router{handler: handler}
|
||||
}
|
||||
|
||||
// Init{{.StructName}}Router 初始化 {{.Description}} 路由信息
|
||||
// @param Router 需要认证的路由组
|
||||
// @param PublicRouter 公开路由组(不需要认证)
|
||||
func (r *{{.StructName}}Router) Init{{.StructName}}Router(Router *gin.RouterGroup, PublicRouter *gin.RouterGroup) {
|
||||
{{- if not .OnlyTemplate}}
|
||||
// 需要认证 + 操作记录的路由组(用于写操作:创建、更新、删除)
|
||||
{{.Abbreviation}}Router := Router.Group("{{.Abbreviation}}").Use(middleware.OperationRecord())
|
||||
// 需要认证但不记录操作的路由组(用于读操作:查询、列表)
|
||||
{{.Abbreviation}}RouterWithoutRecord := Router.Group("{{.Abbreviation}}")
|
||||
{{- else}}
|
||||
// {{.Abbreviation}}Router := Router.Group("{{.Abbreviation}}").Use(middleware.OperationRecord())
|
||||
// {{.Abbreviation}}RouterWithoutRecord := Router.Group("{{.Abbreviation}}")
|
||||
{{- end}}
|
||||
// 公开路由组(不需要认证)
|
||||
{{.Abbreviation}}RouterWithoutAuth := PublicRouter.Group("{{.Abbreviation}}")
|
||||
|
||||
{{- if not .OnlyTemplate}}
|
||||
// ==================== 需要认证 + 操作记录的路由 ====================
|
||||
// 这些路由会记录操作日志,用于审计和追踪
|
||||
{
|
||||
{{.Abbreviation}}Router.POST("create{{.StructName}}", r.handler.Create{{.StructName}}) // 新建{{.Description}}
|
||||
{{.Abbreviation}}Router.DELETE("delete{{.StructName}}", r.handler.Delete{{.StructName}}) // 删除{{.Description}}
|
||||
{{.Abbreviation}}Router.DELETE("delete{{.StructName}}ByIds", r.handler.Delete{{.StructName}}ByIds) // 批量删除{{.Description}}
|
||||
{{.Abbreviation}}Router.PUT("update{{.StructName}}", r.handler.Update{{.StructName}}) // 更新{{.Description}}
|
||||
}
|
||||
|
||||
// ==================== 需要认证但不记录操作的路由 ====================
|
||||
// 这些路由是只读操作,不需要记录操作日志
|
||||
{
|
||||
{{.Abbreviation}}RouterWithoutRecord.GET("find{{.StructName}}", r.handler.Find{{.StructName}}) // 根据ID获取{{.Description}}
|
||||
{{.Abbreviation}}RouterWithoutRecord.GET("get{{.StructName}}List", r.handler.Get{{.StructName}}List) // 获取{{.Description}}列表
|
||||
}
|
||||
|
||||
// ==================== 公开路由(不需要认证) ====================
|
||||
// 这些路由可以被任何人访问,不需要登录
|
||||
{
|
||||
{{- if .HasDataSource}}
|
||||
{{.Abbreviation}}RouterWithoutAuth.GET("get{{.StructName}}DataSource", r.handler.Get{{.StructName}}DataSource) // 获取{{.Description}}数据源
|
||||
{{- end}}
|
||||
{{.Abbreviation}}RouterWithoutAuth.GET("get{{.StructName}}Public", r.handler.Get{{.StructName}}Public) // {{.Description}}开放接口
|
||||
}
|
||||
{{- else}}
|
||||
// ==================== 公开路由(不需要认证) ====================
|
||||
{
|
||||
{{- if .HasDataSource}}
|
||||
{{.Abbreviation}}RouterWithoutAuth.GET("get{{.StructName}}DataSource", r.handler.Get{{.StructName}}DataSource) // 获取{{.Description}}数据源
|
||||
{{- end}}
|
||||
{{.Abbreviation}}RouterWithoutAuth.GET("get{{.StructName}}Public", r.handler.Get{{.StructName}}Public) // {{.Description}}开放接口
|
||||
}
|
||||
{{- end}}
|
||||
}
|
||||
Reference in New Issue
Block a user