优化结构

This commit is contained in:
Yvan
2026-08-21 00:01:48 +08:00
parent e65cf0b1d0
commit 746e33e027
344 changed files with 1021 additions and 578 deletions
+102
View File
@@ -0,0 +1,102 @@
// Package module defines the small contracts shared by application modules.
// A module contributes its own schema, seed data, admin surface and runtime
// hooks without depending on the built-in system implementation.
package module
import (
"github.com/gin-gonic/gin"
"kra/pkg/database/migration"
"kra/pkg/task"
)
type Menu struct {
Name, Path, ParentName, Component, Title, Icon string
Sort int
}
type API struct {
Path, Method, Group, Description string
}
type Surface struct {
Menus []Menu
APIs []API
}
type TimedTask struct {
Name, Description, Spec, MethodName string
WithSeconds, Enabled bool
}
type Definition struct {
Name string
Migrations []migration.Step
Surface Surface
TimedTasks []TimedTask
Tasks []task.Method
}
type Catalog struct {
Definitions []Definition
}
func (c Catalog) MigrationSteps() []migration.Step {
var steps []migration.Step
for _, item := range c.Definitions {
steps = append(steps, item.Migrations...)
}
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 {
tasks = append(tasks, item.TimedTasks...)
}
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 {
registrar.RegisterRoutes(public, private, engine)
}
}
+43
View File
@@ -0,0 +1,43 @@
package module
import (
"context"
"encoding/json"
"testing"
"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" {
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)
}
}