feat: use wire

This commit is contained in:
Yuzuki616
2025-05-07 17:22:29 +09:00
parent 05a1e586ed
commit 9e18c06199
14 changed files with 418 additions and 222 deletions

21
boot/acme.go Normal file
View File

@@ -0,0 +1,21 @@
package boot
import (
"github.com/InazumaV/Ratte/acme"
"github.com/InazumaV/Ratte/conf"
log "github.com/sirupsen/logrus"
)
type AcmeGroup map[string]*acme.Acme
func initAcme(a []conf.ACME) AcmeGroup {
acmes := make(AcmeGroup, len(a))
for _, a := range a {
ac, err := acme.NewAcme(&a)
if err != nil {
log.WithError(err).Fatal("New acme failed")
}
acmes[a.Name] = ac
}
return acmes
}

50
boot/boot.go Normal file
View File

@@ -0,0 +1,50 @@
//go:build wireinject
package boot
import (
"github.com/InazumaV/Ratte/conf"
"github.com/google/wire"
)
var preSet = wire.NewSet(
wire.FieldsOf(new(*conf.Conf), "ACME", "Plugin"),
wire.FieldsOf(new(conf.Plugins), "Core", "Panel"),
initAcme,
initCores,
initPanels,
)
var mainSet = wire.NewSet(
wire.FieldsOf(new(*conf.Conf), "Node"),
initNode,
)
type Boot struct {
Acmes AcmeGroup
Cores CoreGroup
Panels PanelGroup
Node *NodeGroup
}
func (b *Boot) Start() error {
if err := b.Node.Start(); err != nil {
return err
}
return nil
}
func (b *Boot) Close() error {
b.Node.Close()
b.Cores.Close()
b.Panels.Close()
return nil
}
func InitBoot(c *conf.Conf) (*Boot, error) {
wire.Build(
preSet,
mainSet,
wire.Struct(new(Boot), "*"),
)
return nil, nil
}

40
boot/core.go Normal file
View File

@@ -0,0 +1,40 @@
package boot
import (
"fmt"
"github.com/InazumaV/Ratte-Interface/core"
"github.com/InazumaV/Ratte/conf"
"os/exec"
)
type CoreGroup map[string]*core.PluginClient
func (c CoreGroup) Get(name string) *core.PluginClient {
if co, ok := c[name]; ok {
return co
}
return nil
}
func (c CoreGroup) Close() {
for _, co := range c {
co.Close()
}
}
func initCores(cc []conf.CorePlugin) (CoreGroup, error) {
cores := make(CoreGroup, len(cc))
for _, co := range cc {
c, err := core.NewClient(nil, exec.Command(co.Path))
if err != nil {
return nil, fmt.Errorf("new core error: %w", err)
}
err = c.Start(co.DataPath, co.Config)
if err != nil {
return nil, fmt.Errorf("start core error: %w", err)
}
cores[co.Name] = c
break
}
return cores, nil
}

95
boot/node.go Normal file
View File

@@ -0,0 +1,95 @@
package boot
import (
"fmt"
"github.com/InazumaV/Ratte-Interface/core"
"github.com/InazumaV/Ratte-Interface/panel"
"github.com/InazumaV/Ratte/conf"
"github.com/InazumaV/Ratte/handler"
"github.com/InazumaV/Ratte/trigger"
log "github.com/sirupsen/logrus"
)
type NodeGroup struct {
t []*trigger.Trigger
h []*handler.Handler
}
func (n NodeGroup) Start() error {
for _, t := range n.t {
err := t.Start()
if err != nil {
return fmt.Errorf("start trigger error: %w", err)
}
}
return nil
}
func (n NodeGroup) Close() error {
for _, t := range n.t {
err := t.Close()
if err != nil {
log.WithError(err).Errorln("Close trigger error")
}
}
for _, h := range n.h {
err := h.Close()
if err != nil {
log.WithError(err).Errorln("Close handler error")
}
}
return nil
}
func initNode(
n []conf.Node,
acme AcmeGroup,
cores CoreGroup,
panels PanelGroup) (*NodeGroup, error) {
triggers := make([]*trigger.Trigger, 0, len(n))
handlers := make([]*handler.Handler, 0, len(n))
for _, nd := range n {
var co core.Core
if c, ok := cores[nd.Options.Core]; ok {
co = c
} else {
return nil, fmt.Errorf("unknown core name: %s", nd.Options.Core)
}
var pl panel.Panel
if p, ok := panels[nd.Options.Panel]; ok {
pl = p
} else {
return nil, fmt.Errorf("")
}
ac, e := acme[nd.Options.Acme]
if !e {
return nil, fmt.Errorf("unknown acme name: %s", nd.Options.Acme)
}
h := handler.New(co, pl, nd.Name, ac, log.WithFields(
map[string]interface{}{
"node": nd.Name,
"service": "handler",
},
), &nd.Options)
handlers = append(handlers, h)
tr, err := trigger.New(log.WithFields(
map[string]interface{}{
"node": nd.Name,
"service": "trigger",
},
), &nd.Trigger, h, pl, &nd.Remote)
if err != nil {
return nil, fmt.Errorf("new trigger error: %w", err)
}
triggers = append(triggers, tr)
err = tr.Start()
if err != nil {
return nil, fmt.Errorf("start trigger error: %w", err)
}
}
return &NodeGroup{
t: triggers,
h: handlers,
},
nil
}

40
boot/panel.go Normal file
View File

@@ -0,0 +1,40 @@
package boot
import (
"fmt"
"github.com/InazumaV/Ratte-Interface/panel"
"github.com/InazumaV/Ratte/conf"
log "github.com/sirupsen/logrus"
"os/exec"
)
type PanelGroup map[string]*panel.PluginClient
func (p *PanelGroup) Get(name string) *panel.PluginClient {
if pl, ok := (*p)[name]; ok {
return pl
}
return nil
}
func (p *PanelGroup) Close() error {
for _, pl := range *p {
err := pl.Close()
if err != nil {
log.WithError(err).Warn()
}
}
return nil
}
func initPanels(panelsP []conf.Plugin) (PanelGroup, error) {
panels := make(PanelGroup, len(panelsP))
for _, p := range panelsP {
pn, err := panel.NewClient(nil, exec.Command(p.Path))
if err != nil {
return nil, fmt.Errorf("new panel error: %w", err)
}
panels[p.Name] = pn
}
return panels, nil
}

72
boot/wire_gen.go Normal file
View File

@@ -0,0 +1,72 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate go run -mod=mod github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
package boot
import (
"github.com/InazumaV/Ratte/conf"
"github.com/google/wire"
)
// Injectors from boot.go:
func InitBoot(c *conf.Conf) (*Boot, error) {
v := c.Acme
acmeGroup := initAcme(v)
plugins := c.Plugin
v2 := plugins.Core
coreGroup, err := initCores(v2)
if err != nil {
return nil, err
}
v3 := plugins.Panel
panelGroup, err := initPanels(v3)
if err != nil {
return nil, err
}
v4 := c.Node
nodeGroup, err := initNode(v4, acmeGroup, coreGroup, panelGroup)
if err != nil {
return nil, err
}
boot := &Boot{
Acmes: acmeGroup,
Cores: coreGroup,
Panels: panelGroup,
Node: nodeGroup,
}
return boot, nil
}
// boot.go:
var preSet = wire.NewSet(wire.FieldsOf(new(*conf.Conf), "ACME", "Plugin"), wire.FieldsOf(new(conf.Plugins), "Core", "Panel"), initAcme,
initCores,
initPanels,
)
var mainSet = wire.NewSet(wire.FieldsOf(new(*conf.Conf), "Node"), initNode)
type Boot struct {
Acmes AcmeGroup
Cores CoreGroup
Panels PanelGroup
Node *NodeGroup
}
func (b *Boot) Start() error {
if err := b.Node.Start(); err != nil {
return err
}
return nil
}
func (b *Boot) Close() error {
b.Node.Close()
b.Cores.Close()
b.Panels.Close()
return nil
}