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
|
||||
}
|
Reference in New Issue
Block a user