package admin import ( "net/http" "forge.cadoles.com/cadoles/bouncer/internal/store" "github.com/go-chi/chi/v5" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/api" "gitlab.com/wpetit/goweb/logger" ) type QueryLayerResponse struct { Layers []*store.LayerHeader `json:"layers"` } func (s *Server) queryLayer(w http.ResponseWriter, r *http.Request) { limit, ok := getIntQueryParam(w, r, "limit", 10) if !ok { return } offset, ok := getIntQueryParam(w, r, "offset", 0) if !ok { return } options := []store.QueryLayerOptionFunc{ store.WithLayerQueryLimit(int(limit)), store.WithLayerQueryOffset(int(offset)), } name := r.URL.Query().Get("name") if name != "" { options = append(options, store.WithLayerQueryName(store.LayerName(name))) } ctx := r.Context() layers, err := s.layerRepository.QueryLayers( ctx, options..., ) if err != nil { logger.Error(ctx, "could not list layers", logger.E(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } api.DataResponse(w, http.StatusOK, QueryLayerResponse{ Layers: layers, }) } func validateLayerName(v string) (store.LayerName, error) { name, err := store.ValidateName(v) if err != nil { return "", errors.WithStack(err) } return store.LayerName(name), nil } type GetLayerResponse struct { Layer *store.Layer `json:"layer"` } func (s *Server) getLayer(w http.ResponseWriter, r *http.Request) { proxyName, ok := getProxyName(w, r) if !ok { return } layerName, ok := getLayerName(w, r) if !ok { return } ctx := r.Context() layer, err := s.layerRepository.GetLayer(ctx, proxyName, layerName) if err != nil { if errors.Is(err, store.ErrNotFound) { api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil) return } logger.Error(ctx, "could not get layer", logger.E(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } api.DataResponse(w, http.StatusOK, GetLayerResponse{ Layer: layer, }) } type DeleteLayerResponse struct { LayerName store.LayerName `json:"layerName"` } func (s *Server) deleteLayer(w http.ResponseWriter, r *http.Request) { proxyName, ok := getProxyName(w, r) if !ok { return } layerName, ok := getLayerName(w, r) if !ok { return } ctx := r.Context() if err := s.layerRepository.DeleteLayer(ctx, proxyName, layerName); err != nil { if errors.Is(err, store.ErrNotFound) { api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil) return } logger.Error(ctx, "could not delete layer", logger.E(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } api.DataResponse(w, http.StatusOK, DeleteLayerResponse{ LayerName: layerName, }) } type CreateLayerRequest struct { Name string `json:"name" validate:"required"` Type string `json:"type" validate:"required"` Proxy string `json:"proxy" validate:"required"` Options map[string]any `json:"options"` } type CreateLayerResponse struct { Layer *store.Layer `json:"layer"` } func (s *Server) createLayer(w http.ResponseWriter, r *http.Request) { ctx := r.Context() createLayerReq := &CreateLayerRequest{} if ok := api.Bind(w, r, createLayerReq); !ok { return } layerName, err := store.ValidateName(createLayerReq.Name) if err != nil { logger.Error(r.Context(), "could not parse 'name' parameter", logger.E(errors.WithStack(err))) api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil) return } proxyName, err := store.ValidateName(createLayerReq.Proxy) if err != nil { logger.Error(r.Context(), "could not parse 'proxy' parameter", logger.E(errors.WithStack(err))) api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil) return } layer, err := s.layerRepository.CreateLayer(ctx, store.ProxyName(proxyName), store.LayerName(layerName), store.LayerType(createLayerReq.Type), createLayerReq.Options) if err != nil { if errors.Is(err, store.ErrAlreadyExist) { api.ErrorResponse(w, http.StatusConflict, ErrCodeAlreadyExist, nil) return } logger.Error(ctx, "could not create layer", logger.E(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } api.DataResponse(w, http.StatusOK, struct { Layer *store.Layer `json:"layer"` }{ Layer: layer, }) } type UpdateLayerRequest struct{} func (s *Server) updateLayer(w http.ResponseWriter, r *http.Request) { } func getLayerName(w http.ResponseWriter, r *http.Request) (store.LayerName, bool) { rawLayerName := chi.URLParam(r, "layerName") name, err := store.ValidateName(rawLayerName) if err != nil { logger.Error(r.Context(), "could not parse layer name", logger.E(errors.WithStack(err))) api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil) return "", false } return store.LayerName(name), true }