mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-02 06:48:57 -07:00
(wip) rework configuration to support multiple frontends
This commit is contained in:
+26
-53
@@ -1,92 +1,65 @@
|
||||
// Package middleware implements the TrackerLogic interface by executing
|
||||
// Package middleware implements the Logic interface by executing
|
||||
// a series of middleware hooks.
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/sot-tech/mochi/pkg/conf"
|
||||
"github.com/sot-tech/mochi/pkg/log"
|
||||
"github.com/sot-tech/mochi/storage"
|
||||
)
|
||||
|
||||
var (
|
||||
driversM sync.RWMutex
|
||||
drivers = make(map[string]Builder)
|
||||
|
||||
// ErrBuilderDoesNotExist is the error returned by NewMiddleware when a
|
||||
// middleware driver with that name does not exist.
|
||||
ErrBuilderDoesNotExist = errors.New("middleware builder with that name does not exist")
|
||||
logger = log.NewLogger("middleware")
|
||||
buildersMU sync.RWMutex
|
||||
builders = make(map[string]Builder)
|
||||
)
|
||||
|
||||
// Builder is the interface used to initialize a new type of middleware.
|
||||
//
|
||||
// The `options` parameter is map of parameters that should be unmarshalled into
|
||||
// the hook's custom configuration.
|
||||
type Builder func(options conf.MapConfig, storage storage.PeerStorage) (Hook, error)
|
||||
// Builder is the function used to initialize a new Hook
|
||||
// with provided configuration.
|
||||
type Builder func(conf.MapConfig, storage.PeerStorage) (Hook, error)
|
||||
|
||||
// RegisterBuilder makes a Builder available by the provided name.
|
||||
//
|
||||
// If called twice with the same name, the name is blank, or if the provided
|
||||
// Builder is nil, this function panics.
|
||||
func RegisterBuilder(name string, d Builder) {
|
||||
func RegisterBuilder(name string, b Builder) {
|
||||
if name == "" {
|
||||
panic("middleware: could not register a Builder with an empty name")
|
||||
panic("middleware: could not register Builder with an empty name")
|
||||
}
|
||||
if d == nil {
|
||||
if b == nil {
|
||||
panic("middleware: could not register a nil Builder")
|
||||
}
|
||||
|
||||
driversM.Lock()
|
||||
defer driversM.Unlock()
|
||||
buildersMU.Lock()
|
||||
defer buildersMU.Unlock()
|
||||
|
||||
if _, dup := drivers[name]; dup {
|
||||
if _, dup := builders[name]; dup {
|
||||
panic("middleware: RegisterBuilder called twice for " + name)
|
||||
}
|
||||
|
||||
drivers[name] = d
|
||||
}
|
||||
|
||||
// NewHook attempts to initialize a new middleware instance from the
|
||||
// list of registered Builders.
|
||||
//
|
||||
// If a driver does not exist, returns ErrBuilderDoesNotExist.
|
||||
func NewHook(name string, options conf.MapConfig, storage storage.PeerStorage) (Hook, error) {
|
||||
driversM.RLock()
|
||||
defer driversM.RUnlock()
|
||||
|
||||
var newHook Builder
|
||||
newHook, ok := drivers[name]
|
||||
if !ok {
|
||||
return nil, ErrBuilderDoesNotExist
|
||||
}
|
||||
|
||||
return newHook(options, storage)
|
||||
}
|
||||
|
||||
// Config is the generic configuration format used for all registered Hooks.
|
||||
type Config struct {
|
||||
Name string
|
||||
Options conf.MapConfig
|
||||
builders[name] = b
|
||||
}
|
||||
|
||||
// NewHooks is a utility function for initializing Hooks in bulk.
|
||||
// each element of configs must contain pairs `name` - string and `options` - map[string]any
|
||||
func NewHooks(configs []conf.MapConfig, storage storage.PeerStorage) (hooks []Hook, err error) {
|
||||
for _, cfg := range configs {
|
||||
var c Config
|
||||
|
||||
if err = cfg.Unmarshal(&c); err != nil {
|
||||
func NewHooks(configs []conf.NamedMapConfig, storage storage.PeerStorage) (hooks []Hook, err error) {
|
||||
buildersMU.RLock()
|
||||
defer buildersMU.RUnlock()
|
||||
for _, c := range configs {
|
||||
logger.Debug().Object("hook", c).Msg("starting hook")
|
||||
newHook, ok := builders[c.Name]
|
||||
if !ok {
|
||||
err = fmt.Errorf("hook with name '%s' does not exists", c.Name)
|
||||
break
|
||||
}
|
||||
|
||||
var h Hook
|
||||
h, err = NewHook(c.Name, c.Options, storage)
|
||||
if err != nil {
|
||||
if h, err = newHook(c.Config, storage); err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
hooks = append(hooks, h)
|
||||
logger.Info().Str("name", c.Name).Msg("hook started")
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user