2023-04-24 20:52:12 +02:00
|
|
|
package director
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-09-27 10:09:25 +02:00
|
|
|
"net/http"
|
2024-04-12 16:41:11 +02:00
|
|
|
"net/url"
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
|
|
|
"github.com/pkg/errors"
|
2024-09-27 10:09:25 +02:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2023-04-24 20:52:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey string
|
|
|
|
|
|
|
|
const (
|
2024-04-12 16:41:11 +02:00
|
|
|
contextKeyProxy contextKey = "proxy"
|
|
|
|
contextKeyLayers contextKey = "layers"
|
|
|
|
contextKeyOriginalURL contextKey = "originalURL"
|
2024-09-27 10:09:25 +02:00
|
|
|
contextKeyHandleError contextKey = "handleError"
|
2023-04-24 20:52:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errContextKeyNotFound = errors.New("context key not found")
|
|
|
|
errUnexpectedContextValue = errors.New("unexpected context value")
|
|
|
|
)
|
|
|
|
|
2024-04-12 16:41:11 +02:00
|
|
|
func withOriginalURL(ctx context.Context, url *url.URL) context.Context {
|
|
|
|
return context.WithValue(ctx, contextKeyOriginalURL, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func OriginalURL(ctx context.Context) (*url.URL, error) {
|
|
|
|
url, err := ctxValue[*url.URL](ctx, contextKeyOriginalURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url, nil
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
func withLayers(ctx context.Context, layers []*store.Layer) context.Context {
|
|
|
|
return context.WithValue(ctx, contextKeyLayers, layers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ctxLayers(ctx context.Context) ([]*store.Layer, error) {
|
|
|
|
layers, err := ctxValue[[]*store.Layer](ctx, contextKeyLayers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return layers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ctxValue[T any](ctx context.Context, key contextKey) (T, error) {
|
|
|
|
raw := ctx.Value(key)
|
|
|
|
if raw == nil {
|
|
|
|
return *new(T), errors.WithStack(errContextKeyNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := raw.(T)
|
|
|
|
if !ok {
|
|
|
|
return *new(T), errors.WithStack(errUnexpectedContextValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
2024-09-27 10:09:25 +02:00
|
|
|
|
|
|
|
type HandleErrorFunc func(w http.ResponseWriter, r *http.Request, status int, err error)
|
|
|
|
|
|
|
|
func withHandleError(ctx context.Context, fn HandleErrorFunc) context.Context {
|
|
|
|
return context.WithValue(ctx, contextKeyHandleError, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, status int, err error) {
|
|
|
|
err = errors.WithStack(err)
|
|
|
|
|
|
|
|
fn, ok := ctx.Value(contextKeyHandleError).(HandleErrorFunc)
|
|
|
|
if !ok {
|
|
|
|
logger.Error(ctx, err.Error(), logger.CapturedE(err))
|
|
|
|
http.Error(w, http.StatusText(status), status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(w, r, status, err)
|
|
|
|
}
|