bouncer/internal/setup/registry.go

90 lines
2.1 KiB
Go
Raw 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,
}
}
2024-05-17 15:44:28 +02:00
func (r *Registry) GetLayerOptionsRawSchema(layerType store.LayerType) ([]byte, error) {
2023-06-24 01:53:56 +02:00
layerEntry, exists := r.layers[layerType]
if !exists {
return nil, errors.WithStack(ErrNotFound)
}
2024-05-17 15:44:28 +02:00
return layerEntry.rawOptionsSchema, nil
}
func (r *Registry) GetLayerOptionsSchema(layerType store.LayerType) (*schema.Schema, error) {
rawSchema, err := r.GetLayerOptionsRawSchema(layerType)
if err != nil {
return nil, errors.WithStack(err)
}
schema, err := schema.Parse(rawSchema)
2023-06-24 01:53:56 +02:00
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
}
}