bouncer/internal/admin/definition_route.go

57 lines
1.4 KiB
Go
Raw Normal View History

2024-05-17 15:44:28 +02:00
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
}