优化结构

This commit is contained in:
Yvan
2026-08-27 16:20:37 +08:00
parent 001e39da50
commit 653414a1da
54 changed files with 1257 additions and 3450 deletions
-46
View File
@@ -6,7 +6,6 @@ package module
import (
"github.com/gin-gonic/gin"
"kra/pkg/database/migration"
"kra/pkg/task"
)
type Menu struct {
@@ -33,7 +32,6 @@ type Definition struct {
Migrations []migration.Step
Surface Surface
TimedTasks []TimedTask
Tasks []task.Method
}
type Catalog struct {
@@ -48,15 +46,6 @@ func (c Catalog) MigrationSteps() []migration.Step {
return steps
}
func (c Catalog) Surface() Surface {
var surface Surface
for _, item := range c.Definitions {
surface.Menus = append(surface.Menus, item.Surface.Menus...)
surface.APIs = append(surface.APIs, item.Surface.APIs...)
}
return surface
}
func (c Catalog) DefaultTimedTasks() []TimedTask {
var tasks []TimedTask
for _, item := range c.Definitions {
@@ -65,41 +54,6 @@ func (c Catalog) DefaultTimedTasks() []TimedTask {
return tasks
}
func (c Catalog) TaskMethods() []task.Method {
var methods []task.Method
for _, item := range c.Definitions {
methods = append(methods, item.Tasks...)
}
return methods
}
type RouteRegistrar interface {
RegisterRoutes(public, private *gin.RouterGroup, engine *gin.Engine)
}
type Runtime struct {
routes []RouteRegistrar
}
func NewRuntime(routes ...RouteRegistrar) *Runtime {
return &Runtime{routes: append([]RouteRegistrar(nil), routes...)}
}
func (r *Runtime) Add(routes ...RouteRegistrar) {
if r == nil {
return
}
r.routes = append(r.routes, routes...)
}
func (r *Runtime) RegisterRoutes(public, private *gin.RouterGroup, engine *gin.Engine) {
if r == nil {
return
}
for _, registrar := range r.routes {
if registrar == nil {
continue
}
registrar.RegisterRoutes(public, private, engine)
}
}
+1 -26
View File
@@ -1,59 +1,34 @@
package module
import (
"context"
"encoding/json"
"testing"
"github.com/gin-gonic/gin"
"kra/pkg/database/migration"
"kra/pkg/task"
)
func TestCatalogCollectsContributions(t *testing.T) {
run := func(context.Context, json.RawMessage) error { return nil }
catalog := Catalog{Definitions: []Definition{
{
Name: "system",
Migrations: []migration.Step{{ID: "system_schema"}},
Surface: Surface{Menus: []Menu{{Name: "users"}}, APIs: []API{{Path: "/users"}}},
TimedTasks: []TimedTask{{Name: "system.cleanup"}},
Tasks: []task.Method{{Name: "system.cleanup", Run: run}},
},
{
Name: "orders",
Migrations: []migration.Step{{ID: "orders_schema"}},
Surface: Surface{Menus: []Menu{{Name: "orders"}}, APIs: []API{{Path: "/orders"}}},
TimedTasks: []TimedTask{{Name: "orders.expire"}},
Tasks: []task.Method{{Name: "orders.expire", Run: run}},
},
}}
if got := catalog.MigrationSteps(); len(got) != 2 || got[0].ID != "system_schema" || got[1].ID != "orders_schema" {
t.Fatalf("unexpected migrations: %#v", got)
}
if got := catalog.Surface(); len(got.Menus) != 2 || len(got.APIs) != 2 || got.Menus[1].Name != "orders" {
if got := catalog.Definitions[1].Surface; len(got.Menus) != 1 || len(got.APIs) != 1 || got.Menus[0].Name != "orders" {
t.Fatalf("unexpected surface: %#v", got)
}
if got := catalog.DefaultTimedTasks(); len(got) != 2 || got[1].Name != "orders.expire" {
t.Fatalf("unexpected default tasks: %#v", got)
}
if got := catalog.TaskMethods(); len(got) != 2 || got[1].Name != "orders.expire" {
t.Fatalf("unexpected task methods: %#v", got)
}
}
type routeRegistrarStub struct{ called bool }
func (stub *routeRegistrarStub) RegisterRoutes(*gin.RouterGroup, *gin.RouterGroup, *gin.Engine) {
stub.called = true
}
func TestRuntimeSkipsNilRouteRegistrars(t *testing.T) {
stub := &routeRegistrarStub{}
runtime := NewRuntime(nil, stub)
runtime.RegisterRoutes(nil, nil, nil)
if !stub.called {
t.Fatal("non-nil route registrar was not called")
}
}