package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type Config struct {
Server ServerConfig `yaml:"server"`
Plugins map[string]PluginConfig `yaml:"plugins"`
ExternalPlugins ExternalPluginsConfig `yaml:"external_plugins"`
}
type ServerConfig struct {
Address string `yaml:"address"`
LogLevel string `yaml:"log_level"`
}
type ExternalPluginsConfig struct {
Enabled bool `yaml:"enabled"`
PluginDir string `yaml:"plugin_dir"`
AutoLoad bool `yaml:"auto_load"`
PluginPaths []string `yaml:"plugin_paths"`
WASIMountPath string `yaml:"wasi_mount_path"`
WASM WASMPluginConfig `yaml:"wasm"`
}
type WASMPluginConfig struct {
InstancePoolSize int `yaml:"instance_pool_size"`
InstanceMaxLifetime int `yaml:"instance_max_lifetime"`
InstanceMaxRequests int `yaml:"instance_max_requests"`
HealthCheckInterval int `yaml:"health_check_interval"`
EnablePoolStatistics bool `yaml:"enable_pool_statistics"`
}
type PluginConfig struct {
Enabled bool `yaml:"enabled"`
Path string `yaml:"path"`
Config map[string]interface{} `yaml:"config"`
Instances []PluginInstance `yaml:"-"`
}
type PluginInstance struct {
Name string `yaml:"name"`
Enabled bool `yaml:"enabled"`
Path string `yaml:"path"`
Config map[string]interface{} `yaml:"config"`
}
func (p *PluginConfig) UnmarshalYAML(node *yaml.Node) error {
var instances []PluginInstance
if err := node.Decode(&instances); err == nil && len(instances) > 0 {
p.Instances = instances
return nil
}
type pluginConfigAlias PluginConfig
aux := (*pluginConfigAlias)(p)
return node.Decode(aux)
}
func LoadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
return &cfg, nil
}
func (c *Config) GetPluginConfig(pluginName string) (PluginConfig, bool) {
cfg, ok := c.Plugins[pluginName]
return cfg, ok
}
func (c *Config) GetWASMConfig() WASMPluginConfig {
cfg := c.ExternalPlugins.WASM
if cfg.InstancePoolSize <= 0 {
cfg.InstancePoolSize = 10
}
if cfg.InstanceMaxLifetime < 0 {
cfg.InstanceMaxLifetime = 0
}
if cfg.InstanceMaxRequests < 0 {
cfg.InstanceMaxRequests = 0
}
if cfg.HealthCheckInterval < 0 {
cfg.HealthCheckInterval = 0
}
return cfg
}