feat: add layer definition api
This commit is contained in:
56
internal/admin/definition_route.go
Normal file
56
internal/admin/definition_route.go
Normal file
@ -0,0 +1,56 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/setup"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
)
|
||||
|
||||
type QueryLayerDefinitionResponse struct {
|
||||
Definitions []store.LayerDefinition `json:"definitions"`
|
||||
}
|
||||
|
||||
func (s *Server) queryLayerDefinition(w http.ResponseWriter, r *http.Request) {
|
||||
typesFilter, ok := getStringableSliceValues(w, r, "types", nil, transformLayerType)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
existingTypes := setup.GetLayerTypes()
|
||||
|
||||
slices.Sort(existingTypes)
|
||||
|
||||
definitions := make([]store.LayerDefinition, 0, len(existingTypes))
|
||||
|
||||
for _, layerType := range existingTypes {
|
||||
if len(typesFilter) != 0 && !slices.Contains(typesFilter, layerType) {
|
||||
continue
|
||||
}
|
||||
|
||||
schema, err := setup.GetLayerOptionsRawSchema(layerType)
|
||||
if err != nil {
|
||||
logAndCaptureError(r.Context(), "could not retrieve layer options schema", errors.WithStack(err))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
definitions = append(definitions, store.LayerDefinition{
|
||||
Type: layerType,
|
||||
Options: json.RawMessage(schema),
|
||||
})
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, QueryLayerDefinitionResponse{
|
||||
Definitions: definitions,
|
||||
})
|
||||
}
|
||||
|
||||
func transformLayerType(v string) (store.LayerType, error) {
|
||||
return store.LayerType(v), nil
|
||||
}
|
@ -285,7 +285,7 @@ func getStringSliceValues(w http.ResponseWriter, r *http.Request, param string,
|
||||
return defaultValue, true
|
||||
}
|
||||
|
||||
func getStringableSliceValues[T ~string](w http.ResponseWriter, r *http.Request, param string, defaultValue []T, validate func(string) (T, error)) ([]T, bool) {
|
||||
func getStringableSliceValues[T ~string](w http.ResponseWriter, r *http.Request, param string, defaultValue []T, transform func(string) (T, error)) ([]T, bool) {
|
||||
rawValue := r.URL.Query().Get(param)
|
||||
|
||||
if rawValue != "" {
|
||||
@ -293,7 +293,7 @@ func getStringableSliceValues[T ~string](w http.ResponseWriter, r *http.Request,
|
||||
values := make([]T, 0, len(rawValues))
|
||||
|
||||
for _, rv := range rawValues {
|
||||
v, err := validate(rv)
|
||||
v, err := transform(rv)
|
||||
if err != nil {
|
||||
logAndCaptureError(r.Context(), "could not parse ids slice param", errors.WithStack(err))
|
||||
api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil)
|
||||
|
@ -161,6 +161,10 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
|
||||
jwt.NewAuthenticator(s.publicKeys, string(s.serverConfig.Auth.Issuer), jwt.DefaultAcceptableSkew),
|
||||
))
|
||||
|
||||
r.Route("/definitions", func(r chi.Router) {
|
||||
r.With(assertReadAccess).Get("/layers", s.queryLayerDefinition)
|
||||
})
|
||||
|
||||
r.Route("/proxies", func(r chi.Router) {
|
||||
r.With(assertReadAccess).Get("/", s.queryProxy)
|
||||
r.With(assertWriteAccess).Post("/", s.createProxy)
|
||||
|
Reference in New Issue
Block a user