mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-27 17:48:11 -07:00
(tested) refactor code
* add sentinel master parameter into driver config * replace yaml double deserialization with `mapstructure` in initializers * replace struct initializers with registered functions * add torrent approval MD and a sanitize rest MDs
This commit is contained in:
+18
-33
@@ -3,12 +3,11 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/sot-tech/mochi/frontend/http"
|
||||
"github.com/sot-tech/mochi/frontend/udp"
|
||||
"github.com/sot-tech/mochi/middleware"
|
||||
"github.com/sot-tech/mochi/pkg/conf"
|
||||
|
||||
// Imports to register middleware drivers.
|
||||
_ "github.com/sot-tech/mochi/middleware/clientapproval"
|
||||
@@ -21,38 +20,24 @@ import (
|
||||
_ "github.com/sot-tech/mochi/storage/redis"
|
||||
)
|
||||
|
||||
type storageConfig struct {
|
||||
Name string `yaml:"name"`
|
||||
Config any `yaml:"config"`
|
||||
}
|
||||
|
||||
// Config represents the configuration used for executing Conf.
|
||||
type Config struct {
|
||||
middleware.ResponseConfig `yaml:",inline"`
|
||||
MetricsAddr string `yaml:"metrics_addr"`
|
||||
HTTPConfig http.Config `yaml:"http"`
|
||||
UDPConfig udp.Config `yaml:"udp"`
|
||||
Storage storageConfig `yaml:"storage"`
|
||||
PreHooks []middleware.Config `yaml:"prehooks"`
|
||||
PostHooks []middleware.Config `yaml:"posthooks"`
|
||||
}
|
||||
|
||||
// PreHookNames returns only the names of the configured middleware.
|
||||
func (cfg Config) PreHookNames() (names []string) {
|
||||
for _, hook := range cfg.PreHooks {
|
||||
names = append(names, hook.Name)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// PostHookNames returns only the names of the configured middleware.
|
||||
func (cfg Config) PostHookNames() (names []string) {
|
||||
for _, hook := range cfg.PostHooks {
|
||||
names = append(names, hook.Name)
|
||||
}
|
||||
|
||||
return
|
||||
// TODO(jzelinskie): Evaluate whether we would like to make
|
||||
// AnnounceInterval and MinAnnounceInterval optional.
|
||||
// We can make Conf extensible enough that you can program a new response
|
||||
// generator at the cost of making it possible for users to create config that
|
||||
// won't compose a functional tracker.
|
||||
AnnounceInterval time.Duration `yaml:"announce_interval"`
|
||||
MinAnnounceInterval time.Duration `yaml:"min_announce_interval"`
|
||||
MetricsAddr string `yaml:"metrics_addr"`
|
||||
HTTPConfig conf.MapConfig `yaml:"http"`
|
||||
UDPConfig conf.MapConfig `yaml:"udp"`
|
||||
Storage struct {
|
||||
Name string `yaml:"name"`
|
||||
Config conf.MapConfig `yaml:"config"`
|
||||
} `yaml:"storage"`
|
||||
PreHooks []conf.MapConfig `yaml:"prehooks"`
|
||||
PostHooks []conf.MapConfig `yaml:"posthooks"`
|
||||
}
|
||||
|
||||
// ConfigFile represents a namespaced YAML configation file.
|
||||
|
||||
+12
-13
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/sot-tech/mochi/frontend/http"
|
||||
"github.com/sot-tech/mochi/frontend/udp"
|
||||
"github.com/sot-tech/mochi/middleware"
|
||||
"github.com/sot-tech/mochi/pkg/conf"
|
||||
"github.com/sot-tech/mochi/pkg/log"
|
||||
"github.com/sot-tech/mochi/pkg/metrics"
|
||||
_ "github.com/sot-tech/mochi/pkg/randseed"
|
||||
@@ -79,28 +80,26 @@ func (r *Run) Start(ps storage.Storage) error {
|
||||
return fmt.Errorf("failed to validate hook config: %w", err)
|
||||
}
|
||||
|
||||
log.Info("starting tracker logic", log.Fields{
|
||||
"prehooks": cfg.PreHookNames(),
|
||||
"posthooks": cfg.PostHookNames(),
|
||||
})
|
||||
r.logic = middleware.NewLogic(cfg.ResponseConfig, r.storage, preHooks, postHooks)
|
||||
r.logic = middleware.NewLogic(cfg.AnnounceInterval, cfg.MinAnnounceInterval, r.storage, preHooks, postHooks)
|
||||
|
||||
if cfg.HTTPConfig.Addr != "" {
|
||||
if len(cfg.HTTPConfig) > 0 {
|
||||
log.Info("starting HTTP frontend", cfg.HTTPConfig)
|
||||
httpfe, err := http.NewFrontend(r.logic, cfg.HTTPConfig)
|
||||
if err != nil {
|
||||
httpFE, err := http.NewFrontend(r.logic, cfg.HTTPConfig)
|
||||
if err == nil {
|
||||
r.sg.Add(httpFE)
|
||||
} else if !errors.Is(err, conf.ErrNilConfigMap) {
|
||||
return err
|
||||
}
|
||||
r.sg.Add(httpfe)
|
||||
}
|
||||
|
||||
if cfg.UDPConfig.Addr != "" {
|
||||
if len(cfg.UDPConfig) > 0 {
|
||||
log.Info("starting UDP frontend", cfg.UDPConfig)
|
||||
udpfe, err := udp.NewFrontend(r.logic, cfg.UDPConfig)
|
||||
if err != nil {
|
||||
udpFE, err := udp.NewFrontend(r.logic, cfg.UDPConfig)
|
||||
if err == nil {
|
||||
r.sg.Add(udpFE)
|
||||
} else if !errors.Is(err, conf.ErrNilConfigMap) {
|
||||
return err
|
||||
}
|
||||
r.sg.Add(udpfe)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user