mirror of
https://github.com/amir20/dozzle.git
synced 2025-12-21 21:33:18 +01:00
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/amir20/dozzle/internal/auth"
|
|
"github.com/amir20/dozzle/internal/container"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (h *handler) containerActions(w http.ResponseWriter, r *http.Request) {
|
|
action := chi.URLParam(r, "action")
|
|
id := chi.URLParam(r, "id")
|
|
|
|
userLabels := h.config.Labels
|
|
if h.config.Authorization.Provider != NONE {
|
|
user := auth.UserFromContext(r.Context())
|
|
if user.ContainerLabels.Exists() {
|
|
userLabels = user.ContainerLabels
|
|
}
|
|
}
|
|
|
|
containerService, err := h.hostService.FindContainer(hostKey(r), id, userLabels)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error while trying to find container")
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
parsedAction, err := container.ParseContainerAction(action)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error while trying to parse action")
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := containerService.Action(r.Context(), parsedAction); err != nil {
|
|
log.Error().Err(err).Msg("error while trying to perform container action")
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
log.Info().Str("action", action).Str("container", containerService.Container.Name).Msg("container action performed")
|
|
http.Error(w, "", http.StatusNoContent)
|
|
}
|