1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-21 21:33:18 +01:00
Files
dozzle/internal/web/actions.go

53 lines
1.4 KiB
Go

package web
import (
"context"
"net/http"
"time"
"github.com/amir20/dozzle/internal/docker"
"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")
validIdMap, err := h.validContainerIDsForHost(r, hostKey(r))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, ok := validIdMap[id]; !ok {
http.Error(w, "container not found", http.StatusUnauthorized)
return
}
containerService, err := h.multiHostService.FindContainer(hostKey(r), id)
if err != nil {
log.Error().Err(err).Msg("error while trying to find container")
http.Error(w, err.Error(), http.StatusNotFound)
return
}
parsedAction, err := docker.ParseContainerAction(action)
if err != nil {
log.Error().Err(err).Msg("error while trying to parse action")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := containerService.Action(ctx, 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)
}