bouncer/internal/setup/registry.go

81 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-06-23 19:08:35 +02:00
package setup
import (
"context"
"forge.cadoles.com/cadoles/bouncer/internal/config"
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
2023-06-24 01:53:56 +02:00
"forge.cadoles.com/cadoles/bouncer/internal/schema"
2023-06-23 19:08:35 +02:00
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
)
2023-06-24 01:53:56 +02:00
type layerEntry struct {
setup LayerSetupFunc
rawOptionsSchema []byte
}
2023-06-23 19:08:35 +02:00
type Registry struct {
2023-06-24 01:53:56 +02:00
layers map[store.LayerType]layerEntry
2023-06-23 19:08:35 +02:00
}
2023-06-24 01:53:56 +02:00
type LayerSetupFunc func(*config.Config) (director.Layer, error)
func (r *Registry) RegisterLayer(layerType store.LayerType, layerSetup LayerSetupFunc, rawOptionsSchema []byte) {
r.layers[layerType] = layerEntry{
setup: layerSetup,
rawOptionsSchema: rawOptionsSchema,
}
}
func (r *Registry) GetLayerOptionsSchema(layerType store.LayerType) (*schema.Schema, error) {
layerEntry, exists := r.layers[layerType]
if !exists {
return nil, errors.WithStack(ErrNotFound)
}
schema, err := schema.Parse(layerEntry.rawOptionsSchema)
if err != nil {
return nil, errors.WithStack(err)
}
2023-06-23 19:08:35 +02:00
2023-06-24 01:53:56 +02:00
return schema, nil
2023-06-23 19:08:35 +02:00
}
2023-06-24 01:53:56 +02:00
func (r *Registry) GetLayers(ctx context.Context, conf *config.Config) ([]director.Layer, error) {
2023-06-23 19:08:35 +02:00
layers := make([]director.Layer, 0, len(r.layers))
2023-06-24 01:53:56 +02:00
for layerType, layerEntry := range r.layers {
layer, err := layerEntry.setup(conf)
2023-06-23 19:08:35 +02:00
if err != nil {
return nil, errors.Wrapf(err, "could not create layer '%s'", layerType)
}
layers = append(layers, layer)
}
return layers, nil
}
func (r *Registry) LayerTypeExists(layerType store.LayerType) bool {
_, exists := r.layers[layerType]
return exists
}
func (r *Registry) GetLayerTypes() []store.LayerType {
layerTypes := make([]store.LayerType, 0, len(r.layers))
for layerType := range r.layers {
layerTypes = append(layerTypes, layerType)
}
return layerTypes
}
func NewRegistry() *Registry {
return &Registry{
2023-06-24 01:53:56 +02:00
layers: make(map[store.LayerType]layerEntry),
2023-06-23 19:08:35 +02:00
}
}