2024-05-28 16:45:15 +02:00
|
|
|
package director
|
|
|
|
|
|
|
|
import (
|
2024-09-27 10:09:25 +02:00
|
|
|
"net/http"
|
2024-05-28 16:45:15 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/cache"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/cache/memory"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/cache/ttl"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
2024-09-27 10:09:25 +02:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2024-05-28 16:45:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
2024-09-27 10:09:25 +02:00
|
|
|
Layers []Layer
|
|
|
|
ProxyCache cache.Cache[string, []*store.Proxy]
|
|
|
|
LayerCache cache.Cache[string, []*store.Layer]
|
|
|
|
HandleError HandleErrorFunc
|
2024-05-28 16:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OptionFunc func(opts *Options)
|
|
|
|
|
|
|
|
func NewOptions(funcs ...OptionFunc) *Options {
|
|
|
|
opts := &Options{
|
|
|
|
Layers: make([]Layer, 0),
|
|
|
|
ProxyCache: ttl.NewCache(
|
|
|
|
memory.NewCache[string, []*store.Proxy](),
|
|
|
|
memory.NewCache[string, time.Time](),
|
|
|
|
30*time.Second,
|
|
|
|
),
|
|
|
|
LayerCache: ttl.NewCache(
|
|
|
|
memory.NewCache[string, []*store.Layer](),
|
|
|
|
memory.NewCache[string, time.Time](),
|
|
|
|
30*time.Second,
|
|
|
|
),
|
2024-09-27 10:09:25 +02:00
|
|
|
HandleError: func(w http.ResponseWriter, r *http.Request, status int, err error) {
|
|
|
|
logger.Error(r.Context(), err.Error(), logger.CapturedE(err))
|
|
|
|
http.Error(w, http.StatusText(status), status)
|
|
|
|
},
|
2024-05-28 16:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range funcs {
|
|
|
|
fn(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLayers(layers ...Layer) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Layers = layers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithProxyCache(cache cache.Cache[string, []*store.Proxy]) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.ProxyCache = cache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLayerCache(cache cache.Cache[string, []*store.Layer]) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.LayerCache = cache
|
|
|
|
}
|
|
|
|
}
|
2024-09-27 10:09:25 +02:00
|
|
|
|
|
|
|
func WithHandleErrorFunc(fn HandleErrorFunc) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.HandleError = fn
|
|
|
|
}
|
|
|
|
}
|