105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
|
package director
|
||
|
|
||
|
import (
|
||
|
"forge.cadoles.com/Cadoles/go-proxy"
|
||
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||
|
)
|
||
|
|
||
|
type Layer interface {
|
||
|
LayerType() store.LayerType
|
||
|
}
|
||
|
|
||
|
type MiddlewareLayer interface {
|
||
|
Layer
|
||
|
Middleware(layer *store.Layer) proxy.Middleware
|
||
|
}
|
||
|
|
||
|
type RequestTransformerLayer interface {
|
||
|
Layer
|
||
|
RequestTransformer(layer *store.Layer) proxy.RequestTransformer
|
||
|
}
|
||
|
|
||
|
type ResponseTransformerLayer interface {
|
||
|
Layer
|
||
|
ResponseTransformer(layer *store.Layer) proxy.ResponseTransformer
|
||
|
}
|
||
|
|
||
|
type LayerRegistry struct {
|
||
|
index map[store.LayerType]Layer
|
||
|
}
|
||
|
|
||
|
func (r *LayerRegistry) GetLayer(layerType store.LayerType) (Layer, bool) {
|
||
|
layer, exists := r.index[layerType]
|
||
|
if !exists {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
return layer, true
|
||
|
}
|
||
|
|
||
|
func (r *LayerRegistry) getLayerAsAny(layerType store.LayerType) (any, bool) {
|
||
|
return r.GetLayer(layerType)
|
||
|
}
|
||
|
|
||
|
func (r *LayerRegistry) GetMiddleware(layerType store.LayerType, name store.LayerName) (MiddlewareLayer, bool) {
|
||
|
layer, exists := r.getLayerAsAny(layerType)
|
||
|
if !exists {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
middleware, ok := layer.(MiddlewareLayer)
|
||
|
if !ok {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
return middleware, true
|
||
|
}
|
||
|
|
||
|
func (r *LayerRegistry) GetResponseTransformer(layerType store.LayerType) (ResponseTransformerLayer, bool) {
|
||
|
layer, exists := r.getLayerAsAny(layerType)
|
||
|
if !exists {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
transformer, ok := layer.(ResponseTransformerLayer)
|
||
|
if !ok {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
return transformer, true
|
||
|
}
|
||
|
|
||
|
func (r *LayerRegistry) GetRequestTransformer(layerType store.LayerType) (RequestTransformerLayer, bool) {
|
||
|
layer, exists := r.getLayerAsAny(layerType)
|
||
|
if !exists {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
transformer, ok := layer.(RequestTransformerLayer)
|
||
|
if !ok {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
return transformer, true
|
||
|
}
|
||
|
|
||
|
func (r *LayerRegistry) Load(layers ...Layer) {
|
||
|
index := make(map[store.LayerType]Layer)
|
||
|
|
||
|
for _, l := range layers {
|
||
|
layerType := l.LayerType()
|
||
|
index[layerType] = l
|
||
|
}
|
||
|
|
||
|
r.index = index
|
||
|
}
|
||
|
|
||
|
func NewLayerRegistry(layers ...Layer) *LayerRegistry {
|
||
|
registry := &LayerRegistry{
|
||
|
index: make(map[store.LayerType]Layer),
|
||
|
}
|
||
|
registry.Load(layers...)
|
||
|
|
||
|
return registry
|
||
|
}
|