Documentation
¶
Index ¶
- Variables
- func IsPackageAllowed(pkg string) bool
- func ValidateSource(source string) error
- type APIHandler
- type ComponentInfo
- type ComponentRegistry
- func (r *ComponentRegistry) Count() int
- func (r *ComponentRegistry) Get(id string) (*DynamicComponent, bool)
- func (r *ComponentRegistry) List() []ComponentInfo
- func (r *ComponentRegistry) ListNames() []string
- func (r *ComponentRegistry) Register(id string, component *DynamicComponent) error
- func (r *ComponentRegistry) Unregister(id string) error
- type ComponentStatus
- type DynamicComponent
- func (dc *DynamicComponent) Execute(ctx context.Context, params map[string]interface{}) (map[string]interface{}, error)
- func (dc *DynamicComponent) Info() ComponentInfo
- func (dc *DynamicComponent) Init(services map[string]interface{}) error
- func (dc *DynamicComponent) LoadFromSource(source string) error
- func (dc *DynamicComponent) Name() string
- func (dc *DynamicComponent) Source() string
- func (dc *DynamicComponent) Start(ctx context.Context) error
- func (dc *DynamicComponent) Stop(ctx context.Context) error
- type InterpreterPool
- type Loader
- type ModuleAdapter
- func (a *ModuleAdapter) Execute(ctx context.Context, params map[string]interface{}) (map[string]interface{}, error)
- func (a *ModuleAdapter) Init(app modular.Application) error
- func (a *ModuleAdapter) Name() string
- func (a *ModuleAdapter) ProvidesServices() []modular.ServiceProvider
- func (a *ModuleAdapter) RequiresServices() []modular.ServiceDependency
- func (a *ModuleAdapter) SetProvides(services []string)
- func (a *ModuleAdapter) SetRequires(services []string)
- type Option
- type Watcher
- type WatcherOption
Constants ¶
This section is empty.
Variables ¶
var AllowedPackages = map[string]bool{ "fmt": true, "strings": true, "strconv": true, "encoding/json": true, "encoding/xml": true, "encoding/csv": true, "encoding/base64": true, "context": true, "time": true, "math": true, "math/rand": true, "sort": true, "sync": true, "sync/atomic": true, "errors": true, "io": true, "bytes": true, "bufio": true, "unicode": true, "unicode/utf8": true, "regexp": true, "path": true, "net/url": true, "net/http": true, "log": true, "maps": true, "slices": true, "crypto/sha256": true, "crypto/hmac": true, "crypto/md5": true, "hash": true, "html": true, "html/template": true, "text/template": true, }
AllowedPackages defines the standard library packages that dynamically loaded components are permitted to import. Packages not in this list will be rejected during source validation.
var BlockedPackages = map[string]bool{ "os/exec": true, "syscall": true, "unsafe": true, "plugin": true, "runtime/debug": true, "reflect": true, "os": true, "net": true, "crypto/tls": true, "debug/elf": true, "debug/macho": true, "debug/pe": true, "debug/plan9obj": true, }
BlockedPackages defines packages that are explicitly forbidden for security reasons.
Functions ¶
func IsPackageAllowed ¶
IsPackageAllowed checks if a given import path is permitted in dynamic components.
func ValidateSource ¶
ValidateSource performs a basic syntax check and verifies that only allowed packages are imported.
Types ¶
type APIHandler ¶
type APIHandler struct {
// contains filtered or unexported fields
}
APIHandler exposes HTTP endpoints for managing dynamic components.
func NewAPIHandler ¶
func NewAPIHandler(loader *Loader, registry *ComponentRegistry) *APIHandler
NewAPIHandler creates a new API handler.
func (*APIHandler) RegisterRoutes ¶
func (h *APIHandler) RegisterRoutes(mux *http.ServeMux)
RegisterRoutes registers the dynamic component API routes on the given mux.
type ComponentInfo ¶
type ComponentInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Source string `json:"source,omitempty"`
Status ComponentStatus `json:"status"`
LoadedAt time.Time `json:"loaded_at"`
Error string `json:"error,omitempty"`
}
ComponentInfo holds metadata about a loaded dynamic component.
type ComponentRegistry ¶
type ComponentRegistry struct {
// contains filtered or unexported fields
}
ComponentRegistry tracks all dynamically loaded components. It is safe for concurrent access.
func NewComponentRegistry ¶
func NewComponentRegistry() *ComponentRegistry
NewComponentRegistry creates an empty registry.
func (*ComponentRegistry) Count ¶
func (r *ComponentRegistry) Count() int
Count returns the number of registered components.
func (*ComponentRegistry) Get ¶
func (r *ComponentRegistry) Get(id string) (*DynamicComponent, bool)
Get retrieves a component by ID.
func (*ComponentRegistry) List ¶
func (r *ComponentRegistry) List() []ComponentInfo
List returns info for all registered components.
func (*ComponentRegistry) ListNames ¶
func (r *ComponentRegistry) ListNames() []string
ListNames returns the IDs of all registered components.
func (*ComponentRegistry) Register ¶
func (r *ComponentRegistry) Register(id string, component *DynamicComponent) error
Register adds or replaces a component in the registry.
func (*ComponentRegistry) Unregister ¶
func (r *ComponentRegistry) Unregister(id string) error
Unregister removes a component from the registry.
type ComponentStatus ¶
type ComponentStatus string
ComponentStatus describes the lifecycle state of a dynamic component.
const ( StatusUnloaded ComponentStatus = "unloaded" StatusLoaded ComponentStatus = "loaded" StatusInitialized ComponentStatus = "initialized" StatusRunning ComponentStatus = "running" StatusStopped ComponentStatus = "stopped" StatusError ComponentStatus = "error" )
type DynamicComponent ¶
type DynamicComponent struct {
// contains filtered or unexported fields
}
DynamicComponent wraps Yaegi-interpreted Go code as a workflow component that satisfies the modular.Module interface.
func NewDynamicComponent ¶
func NewDynamicComponent(id string, pool *InterpreterPool) *DynamicComponent
NewDynamicComponent creates a new unloaded dynamic component.
func (*DynamicComponent) Execute ¶
func (dc *DynamicComponent) Execute(ctx context.Context, params map[string]interface{}) (map[string]interface{}, error)
Execute runs the interpreted Execute function.
func (*DynamicComponent) Info ¶
func (dc *DynamicComponent) Info() ComponentInfo
Info returns the current component metadata.
func (*DynamicComponent) Init ¶
func (dc *DynamicComponent) Init(services map[string]interface{}) error
Init satisfies modular.Module. It delegates to the interpreted Init function.
func (*DynamicComponent) LoadFromSource ¶
func (dc *DynamicComponent) LoadFromSource(source string) error
LoadFromSource compiles and loads Go source code into the component.
func (*DynamicComponent) Name ¶
func (dc *DynamicComponent) Name() string
Name returns the component name. If interpreted code provides a Name() function, that value is used; otherwise the component ID is returned.
func (*DynamicComponent) Source ¶
func (dc *DynamicComponent) Source() string
Source returns the loaded source code.
type InterpreterPool ¶
type InterpreterPool struct {
// contains filtered or unexported fields
}
InterpreterPool manages a pool of Yaegi interpreters.
func NewInterpreterPool ¶
func NewInterpreterPool(opts ...Option) *InterpreterPool
NewInterpreterPool creates a new pool with optional configuration.
func (*InterpreterPool) NewInterpreter ¶
func (p *InterpreterPool) NewInterpreter() (*interp.Interpreter, error)
NewInterpreter creates a sandboxed Yaegi interpreter with only the allowed standard library symbols loaded.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader handles loading dynamic components from various sources.
func NewLoader ¶
func NewLoader(pool *InterpreterPool, registry *ComponentRegistry) *Loader
NewLoader creates a Loader backed by the given pool and registry.
func (*Loader) LoadFromDirectory ¶
func (l *Loader) LoadFromDirectory(dir string) ([]*DynamicComponent, error)
LoadFromDirectory scans a directory for .go files and loads each one.
func (*Loader) LoadFromFile ¶
func (l *Loader) LoadFromFile(id, path string) (*DynamicComponent, error)
LoadFromFile reads a .go file and loads it as a component. The component ID is derived from the filename (without extension) unless the caller provides an explicit id.
func (*Loader) LoadFromString ¶
func (l *Loader) LoadFromString(id, source string) (*DynamicComponent, error)
LoadFromString validates, compiles, and registers a component from source.
type ModuleAdapter ¶
type ModuleAdapter struct {
// contains filtered or unexported fields
}
ModuleAdapter wraps a DynamicComponent as a modular.Module so it can participate in the modular dependency system.
func NewModuleAdapter ¶
func NewModuleAdapter(component *DynamicComponent) *ModuleAdapter
NewModuleAdapter creates a new ModuleAdapter wrapping the given component.
func (*ModuleAdapter) Execute ¶
func (a *ModuleAdapter) Execute(ctx context.Context, params map[string]interface{}) (map[string]interface{}, error)
Execute delegates execution to the underlying component.
func (*ModuleAdapter) Init ¶
func (a *ModuleAdapter) Init(app modular.Application) error
Init initializes the adapter by collecting required services and passing them to the underlying component, then registering provided services.
func (*ModuleAdapter) ProvidesServices ¶
func (a *ModuleAdapter) ProvidesServices() []modular.ServiceProvider
ProvidesServices returns the services provided by this adapter.
func (*ModuleAdapter) RequiresServices ¶
func (a *ModuleAdapter) RequiresServices() []modular.ServiceDependency
RequiresServices returns the services required by this adapter.
func (*ModuleAdapter) SetProvides ¶
func (a *ModuleAdapter) SetProvides(services []string)
SetProvides sets the list of service names this adapter provides.
func (*ModuleAdapter) SetRequires ¶
func (a *ModuleAdapter) SetRequires(services []string)
SetRequires sets the list of service names this adapter requires.
type Option ¶
type Option func(*InterpreterPool)
Option configures an InterpreterPool.
func WithAllowedPackages ¶
WithAllowedPackages overrides the default allowed packages list.
type Watcher ¶
type Watcher struct {
// contains filtered or unexported fields
}
Watcher monitors a directory for .go file changes and hot-reloads components.
func NewWatcher ¶
func NewWatcher(loader *Loader, dir string, opts ...WatcherOption) *Watcher
NewWatcher creates a file system watcher that automatically reloads components when .go files in the watched directory change.
type WatcherOption ¶
type WatcherOption func(*Watcher)
WatcherOption configures a Watcher.
func WithDebounce ¶
func WithDebounce(d time.Duration) WatcherOption
WithDebounce sets the debounce duration for file change events.
func WithLogger ¶
func WithLogger(l *log.Logger) WatcherOption
WithLogger sets the logger for the watcher.