86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
// 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 (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
"kra/pkg/database/migration"
|
|
)
|
|
|
|
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
|
|
Prerequisites func(*gorm.DB) error
|
|
ValidateSchema func(*gorm.DB) error
|
|
}
|
|
|
|
type Catalog struct {
|
|
Definitions []Definition
|
|
}
|
|
|
|
func (c Catalog) CheckPrerequisites(db *gorm.DB) error {
|
|
for _, item := range c.Definitions {
|
|
if item.Prerequisites != nil {
|
|
if err := item.Prerequisites(db); err != nil {
|
|
return fmt.Errorf("%s prerequisites: %w", item.Name, err)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c Catalog) CheckSchema(db *gorm.DB) error {
|
|
for _, item := range c.Definitions {
|
|
if item.ValidateSchema != nil {
|
|
if err := item.ValidateSchema(db); err != nil {
|
|
return fmt.Errorf("%s: %w", item.Name, err)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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) DefaultTimedTasks() []TimedTask {
|
|
var tasks []TimedTask
|
|
for _, item := range c.Definitions {
|
|
tasks = append(tasks, item.TimedTasks...)
|
|
}
|
|
return tasks
|
|
}
|
|
|
|
type RouteRegistrar interface {
|
|
RegisterRoutes(public, private *gin.RouterGroup, engine *gin.Engine)
|
|
}
|