Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 3 additions & 60 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"context"
"database/sql"
"encoding/json"
"flag"

Check failure on line 7 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / Build

other declaration of errors

Check failure on line 7 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / Test (Go 1.26)

other declaration of errors
"fmt"
"log"

Check failure on line 9 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / Build

"errors" imported and not used

Check failure on line 9 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / Build

errors redeclared in this block

Check failure on line 9 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / Test (Go 1.26)

"errors" imported and not used

Check failure on line 9 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / Test (Go 1.26)

errors redeclared in this block
"log/slog"
"net/http"
"os"
Expand Down Expand Up @@ -37,6 +37,7 @@
_ "github.com/GoCodeAlone/workflow/plugin/docmanager"
pluginexternal "github.com/GoCodeAlone/workflow/plugin/external"
_ "github.com/GoCodeAlone/workflow/plugin/storebrowser"
pluginadmin "github.com/GoCodeAlone/workflow/plugins/admin"
pluginai "github.com/GoCodeAlone/workflow/plugins/ai"
pluginapi "github.com/GoCodeAlone/workflow/plugins/api"
pluginauth "github.com/GoCodeAlone/workflow/plugins/auth"
Expand Down Expand Up @@ -115,6 +116,7 @@
pluginintegration.New(),
pluginai.New(),
pluginplatform.New(),
pluginadmin.New().WithUIDir(*adminUIDir),
}
for _, p := range plugins {
if err := engine.LoadPlugin(p); err != nil {
Expand Down Expand Up @@ -305,13 +307,7 @@
logger: logger,
}

// Merge admin config into primary config — admin UI is always enabled.
// The admin config provides all management endpoints (auth, API, schema,
// AI, dynamic components) via the engine's own modules and routes.
if err := mergeAdminConfig(logger, cfg); err != nil {
return nil, fmt.Errorf("failed to set up admin: %w", err)
}

// Admin config is merged by the admin plugin's wiring hook during buildEngine.
engine, loader, registry, err := buildEngine(cfg, logger)
if err != nil {
return nil, fmt.Errorf("failed to build engine: %w", err)
Expand Down Expand Up @@ -345,59 +341,6 @@
return app, nil
}

// mergeAdminConfig loads the embedded admin config and merges admin
// modules/routes into the primary config. If --admin-ui-dir (or ADMIN_UI_DIR
// env var) is set the static.fileserver root is updated to that path,
// allowing the admin UI to be deployed and updated independently of the binary.
// If the config already contains admin modules (e.g., the user passed the
// admin config directly), the merge is skipped to avoid duplicates — but
// the UI root is still injected so the static fileserver works.
func mergeAdminConfig(logger *slog.Logger, cfg *config.WorkflowConfig) error {
// Resolve the UI root: flag > ADMIN_UI_DIR env > leave as configured in config.yaml
uiDir := *adminUIDir

// Check if the config already contains admin modules
for _, m := range cfg.Modules {
if m.Name == "admin-server" {
logger.Info("Config already contains admin modules, skipping merge")
if uiDir != "" {
injectUIRoot(cfg, uiDir)
logger.Info("Admin UI root overridden", "uiDir", uiDir)
}
return nil
}
}

adminCfg, err := admin.LoadConfig()
if err != nil {
return err
}

if uiDir != "" {
injectUIRoot(adminCfg, uiDir)
logger.Info("Admin UI root overridden", "uiDir", uiDir)
}

// Merge admin modules and routes into primary config
admin.MergeInto(cfg, adminCfg)

logger.Info("Admin UI enabled")
return nil
}

// injectUIRoot updates every static.fileserver module config in cfg to serve
// from the given root directory.
func injectUIRoot(cfg *config.WorkflowConfig, uiRoot string) {
for i := range cfg.Modules {
if cfg.Modules[i].Type == "static.fileserver" {
if cfg.Modules[i].Config == nil {
cfg.Modules[i].Config = make(map[string]any)
}
cfg.Modules[i].Config["root"] = uiRoot
}
}
}

// initManagementHandlers creates all management service handlers and stores
// them on the serverApp struct. These handlers are created once and persist
// across engine reloads. Only the service registrations need to be refreshed.
Expand Down
238 changes: 238 additions & 0 deletions plugins/admin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
// Package admin provides an EnginePlugin that serves the admin dashboard UI
// and loads admin config routes. It encapsulates admin concerns — static file
// serving, config merging, and service delegate wiring — as a self-contained
// plugin rather than hard-wired logic in cmd/server/main.go.
package admin

import (
"fmt"
"log/slog"

"github.com/CrisisTextLine/modular"
"github.com/GoCodeAlone/workflow/admin"
"github.com/GoCodeAlone/workflow/capability"
"github.com/GoCodeAlone/workflow/config"
"github.com/GoCodeAlone/workflow/module"
"github.com/GoCodeAlone/workflow/plugin"
"github.com/GoCodeAlone/workflow/schema"
)

// Plugin provides admin-specific module types and wiring hooks:
// - admin.dashboard — serves the admin UI static files via a static.fileserver
// - admin.config_loader — loads admin/config.yaml and merges routes into the engine
type Plugin struct {
plugin.BaseEnginePlugin

// UIDir overrides the static file root for the admin dashboard.
// Empty string means use the default from admin/config.yaml.
UIDir string

// Logger for wiring hook diagnostics.
Logger *slog.Logger
}

// New creates a new admin plugin.
func New() *Plugin {
return &Plugin{
BaseEnginePlugin: plugin.BaseEnginePlugin{
BaseNativePlugin: plugin.BaseNativePlugin{
PluginName: "admin",
PluginVersion: "1.0.0",
PluginDescription: "Admin dashboard UI and config-driven admin routes",
},
Manifest: plugin.PluginManifest{
Name: "admin",
Version: "1.0.0",
Author: "GoCodeAlone",
Description: "Admin dashboard UI and config-driven admin routes",
Tier: plugin.TierCore,
ModuleTypes: []string{
"admin.dashboard",
"admin.config_loader",
},
WiringHooks: []string{
"admin-config-merge",
},
Capabilities: []plugin.CapabilityDecl{
{Name: "admin-ui", Role: "provider", Priority: 10},
{Name: "admin-config", Role: "provider", Priority: 10},
},
},
},
}
}

// WithUIDir sets the static file root for the admin dashboard.
func (p *Plugin) WithUIDir(dir string) *Plugin {
p.UIDir = dir
return p
}

// WithLogger sets the logger for wiring hook diagnostics.
func (p *Plugin) WithLogger(logger *slog.Logger) *Plugin {
p.Logger = logger
return p
}

// Capabilities returns the capability contracts this plugin defines.
func (p *Plugin) Capabilities() []capability.Contract {
return []capability.Contract{
{
Name: "admin-ui",
Description: "Serves the admin dashboard UI as static files with SPA fallback",
},
{
Name: "admin-config",
Description: "Loads and merges admin config routes into the workflow engine",
},
}
}

// ModuleFactories returns factories for admin module types.
func (p *Plugin) ModuleFactories() map[string]plugin.ModuleFactory {
return map[string]plugin.ModuleFactory{
"admin.dashboard": func(name string, cfg map[string]any) modular.Module {
root := ""
if r, ok := cfg["root"].(string); ok {
root = r
}
root = config.ResolvePathInConfig(cfg, root)
if p.UIDir != "" {
root = p.UIDir
}
prefix := "/"
if pfx, ok := cfg["prefix"].(string); ok {
prefix = pfx
}
// SPA fallback is enabled by default for the admin dashboard UI.
spaFallback := true
if sf, ok := cfg["spaFallback"].(bool); ok {
spaFallback = sf
}
var opts []module.StaticFileServerOption
if spaFallback {
opts = append(opts, module.WithSPAFallback())
}
if cma, ok := cfg["cacheMaxAge"].(int); ok {
opts = append(opts, module.WithCacheMaxAge(cma))
} else if cma, ok := cfg["cacheMaxAge"].(float64); ok {
opts = append(opts, module.WithCacheMaxAge(int(cma)))
}
sfs := module.NewStaticFileServer(name, root, prefix, opts...)
if routerName, ok := cfg["router"].(string); ok && routerName != "" {
sfs.SetRouterName(routerName)
}
return sfs
},
"admin.config_loader": func(name string, _ map[string]any) modular.Module {
return newConfigLoaderModule(name)
},
}
}

// ModuleSchemas returns UI schema definitions for admin module types.
func (p *Plugin) ModuleSchemas() []*schema.ModuleSchema {
return []*schema.ModuleSchema{
{
Type: "admin.dashboard",
Label: "Admin Dashboard",
Category: "admin",
Description: "Serves the admin UI static files with SPA fallback",
Inputs: []schema.ServiceIODef{{Name: "http_request", Type: "http.Request", Description: "HTTP request for admin UI"}},
Outputs: []schema.ServiceIODef{{Name: "http_response", Type: "http.Response", Description: "Static file or SPA fallback"}},
ConfigFields: []schema.ConfigFieldDef{
{Key: "root", Label: "UI Root Directory", Type: schema.FieldTypeString, Description: "Path to admin UI static assets directory", Placeholder: "ui/dist"},
},
},
{
Type: "admin.config_loader",
Label: "Admin Config Loader",
Category: "admin",
Description: "Loads the embedded admin config and merges routes into the engine",
Inputs: []schema.ServiceIODef{},
Outputs: []schema.ServiceIODef{{Name: "config", Type: "WorkflowConfig", Description: "Merged admin configuration"}},
},
}
}

// WiringHooks returns post-init wiring functions that merge admin config
// into the running engine.
func (p *Plugin) WiringHooks() []plugin.WiringHook {
return []plugin.WiringHook{
{
Name: "admin-config-merge",
Priority: 100, // run early so admin routes are available
Hook: func(_ modular.Application, cfg *config.WorkflowConfig) error {
return p.mergeAdminConfig(cfg)
},
},
}
}

// mergeAdminConfig loads the embedded admin config and merges it into the
// primary config. If UIDir is set, the static fileserver root is overridden.
func (p *Plugin) mergeAdminConfig(cfg *config.WorkflowConfig) error {
logger := p.Logger
if logger == nil {
logger = slog.Default()
}

// Skip merge if admin modules are already present
for _, m := range cfg.Modules {
if m.Name == "admin-server" {
logger.Info("Config already contains admin modules, skipping merge")
if p.UIDir != "" {
injectUIRoot(cfg, p.UIDir)
logger.Info("Admin UI root overridden", "uiDir", p.UIDir)
}
return nil
}
}

adminCfg, err := admin.LoadConfig()
if err != nil {
return fmt.Errorf("admin plugin: load config: %w", err)
}

if p.UIDir != "" {
injectUIRoot(adminCfg, p.UIDir)
logger.Info("Admin UI root overridden", "uiDir", p.UIDir)
}

admin.MergeInto(cfg, adminCfg)
logger.Info("Admin UI enabled via admin plugin")
return nil
}

// injectUIRoot updates every static.fileserver and admin.dashboard module
// config in cfg to serve from the given root directory.
func injectUIRoot(cfg *config.WorkflowConfig, uiRoot string) {
for i := range cfg.Modules {
if cfg.Modules[i].Type == "static.fileserver" || cfg.Modules[i].Type == "admin.dashboard" {
if cfg.Modules[i].Config == nil {
cfg.Modules[i].Config = make(map[string]any)
}
cfg.Modules[i].Config["root"] = uiRoot
}
}
}

// configLoaderModule is a minimal modular.Module that represents the admin
// config loading concern. It is used as a dependency anchor — other modules
// can depend on it to ensure admin config is loaded first.
type configLoaderModule struct {
name string
}

func newConfigLoaderModule(name string) *configLoaderModule {
return &configLoaderModule{name: name}
}

func (m *configLoaderModule) Name() string { return m.name }
func (m *configLoaderModule) Dependencies() []string { return nil }
func (m *configLoaderModule) ProvidesServices() []modular.ServiceProvider { return nil }
func (m *configLoaderModule) RequiresServices() []modular.ServiceDependency { return nil }
func (m *configLoaderModule) RegisterConfig(_ modular.Application) error { return nil }
func (m *configLoaderModule) Init(_ modular.Application) error { return nil }
func (m *configLoaderModule) Start(_ modular.Application) error { return nil }
func (m *configLoaderModule) Stop(_ modular.Application) error { return nil }
Loading
Loading