reactor: detach plugin config

This commit is contained in:
Yuzuki616
2025-04-28 10:43:29 +09:00
parent 28d5f1dd00
commit 97f7b261d9
5 changed files with 76 additions and 52 deletions

View File

@@ -50,14 +50,14 @@ func runHandle(_ *cobra.Command, _ []string) {
log.WithField("path", config).Info("Loaded.") log.WithField("path", config).Info("Loaded.")
log.Info("Init core plugin...") log.Info("Init core plugin...")
err = startCores(c.Core) err = startCores(c.Plugin.Core, c.Core)
if err != nil { if err != nil {
log.WithError(err).Fatal("Init core plugin failed") log.WithError(err).Fatal("Init core plugin failed")
} }
log.Info("Done.") log.Info("Done.")
log.Info("Init panel plugin...") log.Info("Init panel plugin...")
err = startPanel(c.Panel) err = startPanel(c.Plugin.Panel)
if err != nil { if err != nil {
log.WithError(err).Fatal("Init panel plugin failed") log.WithError(err).Fatal("Init panel plugin failed")
} }
@@ -106,11 +106,11 @@ func runHandle(_ *cobra.Command, _ []string) {
if err != nil { if err != nil {
log.WithError(err).Fatal("Close panel failed") log.WithError(err).Fatal("Close panel failed")
} }
err = startCores(c.Core) err = startCores(c.Plugin.Core, c.Core)
if err != nil { if err != nil {
log.WithError(err).Fatal("Start core failed") log.WithError(err).Fatal("Start core failed")
} }
err = startPanel(c.Panel) err = startPanel(c.Plugin.Panel)
if err != nil { if err != nil {
log.WithError(err).Fatal("Start panel failed") log.WithError(err).Fatal("Start panel failed")
} }
@@ -146,19 +146,25 @@ func runHandle(_ *cobra.Command, _ []string) {
log.Info("Done.") log.Info("Done.")
} }
func startCores(coresP []conf.Core) error { func startCores(coresP []conf.Plugin, cc []conf.Core) error {
// new cores // new cores
cores = make(map[string]*core.PluginClient, len(coresP)) cores = make(map[string]*core.PluginClient, len(coresP))
for _, co := range coresP { for _, ccV := range cc {
c, err := core.NewClient(nil, exec.Command(co.Path)) for _, co := range coresP {
if err != nil { if co.Name != ccV.Type {
return fmt.Errorf("new core error: %w", err) continue
}
c, err := core.NewClient(nil, exec.Command(co.Path))
if err != nil {
return fmt.Errorf("new core error: %w", err)
}
err = c.Start(ccV.DataPath, ccV.Config)
if err != nil {
return fmt.Errorf("start core error: %w", err)
}
cores[co.Name] = c
break
} }
err = c.Start(co.DataPath, co.Config)
if err != nil {
return fmt.Errorf("start core error: %w", err)
}
cores[co.Name] = c
} }
return nil return nil
} }
@@ -173,7 +179,7 @@ func closeCore() error {
return nil return nil
} }
func startPanel(panelsP []conf.Panel) error { func startPanel(panelsP []conf.Plugin) error {
panels = make(map[string]*panel.PluginClient, len(panelsP)) panels = make(map[string]*panel.PluginClient, len(panelsP))
for _, p := range panelsP { for _, p := range panelsP {
pn, err := panel.NewClient(nil, exec.Command(p.Path)) pn, err := panel.NewClient(nil, exec.Command(p.Path))

View File

@@ -21,9 +21,9 @@ type Conf struct {
// config fields // config fields
Log Log `json:"Log,omitempty"` Log Log `json:"Log,omitempty"`
Watcher Watcher `json:"Watcher,omitempty"` Watcher Watcher `json:"Watcher,omitempty"`
Plugin Plugins `json:"Plugin,omitempty"` // Only accept from file path
Core []Core `json:"Core,omitempty"` Core []Core `json:"Core,omitempty"`
Acme []ACME `json:"Acme,omitempty"` Acme []ACME `json:"Acme,omitempty"`
Panel []Panel `json:"Panel,omitempty"`
Node []Node `json:"Node,omitempty"` Node []Node `json:"Node,omitempty"`
} }
@@ -34,11 +34,11 @@ func New(path string) *Conf {
WatchLocalConfig: true, WatchLocalConfig: true,
WatchRemoteConfig: true, WatchRemoteConfig: true,
}, },
Log: newLog(), Log: newLog(),
Core: make([]Core, 0), Core: make([]Core, 0),
Acme: make([]ACME, 0), Acme: make([]ACME, 0),
Panel: make([]Panel, 0), Plugin: Plugins{},
Node: make([]Node, 0), Node: make([]Node, 0),
} }
} }

View File

@@ -1,25 +0,0 @@
package conf
import (
"fmt"
"github.com/goccy/go-json"
)
type Core struct {
Name string `json:"Name,omitempty"`
Path string `json:"Path,omitempty"`
DataPath string `json:"DataPath,omitempty"`
Config json.RawMessage `json:"Config,omitempty"`
}
type _core Core
func (c *Core) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, (*_core)(c))
if err != nil {
return fmt.Errorf("failed to unmarshal core: %v", err)
}
if len(c.Config) == 0 {
c.Config = data
}
return nil
}

View File

@@ -1,6 +0,0 @@
package conf
type Panel struct {
Name string `json:"name"`
Path string `json:"path"`
}

49
conf/plugin.go Normal file
View File

@@ -0,0 +1,49 @@
package conf
import (
"fmt"
"github.com/goccy/go-json"
)
type Core struct {
Type string `json:"type"`
DataPath string `json:"DataPath,omitempty"`
Config json.RawMessage `json:"Config,omitempty"`
}
type _core Core
func (c *Core) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, (*_core)(c))
if err != nil {
return fmt.Errorf("failed to unmarshal core: %v", err)
}
if len(c.Config) == 0 {
c.Config = data
}
return nil
}
type Plugins struct {
Core []Plugin `json:"core"`
Panel []Plugin `json:"panel"`
}
type Plugin struct {
Name string `json:"Name,omitempty"`
Path string `json:"Path,omitempty"`
}
type _plugins Plugins
func (p *Plugins) UnmarshalJSON(data []byte) error {
var path string
err := json.Unmarshal(data, path)
if err != nil {
return err
}
err = json.Unmarshal(data, (*_plugins)(p))
if err != nil {
return fmt.Errorf("failed to unmarshal plugin: %v", err)
}
return nil
}