From 1369b3cf8b778cb8eedf40464d8837c0bbd111ad Mon Sep 17 00:00:00 2001 From: Gergo Dulai Date: Tue, 3 Feb 2026 22:03:08 +0100 Subject: [PATCH] Container start/stop impl --- node/node.go | 61 ++++++++++++++++++++++++++++++++++++------------ router/router.go | 10 ++------ 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/node/node.go b/node/node.go index 59aca7e..dba42df 100644 --- a/node/node.go +++ b/node/node.go @@ -25,44 +25,75 @@ func build(command BuildCommand) { } func HandleStart(w http.ResponseWriter, r *http.Request) { + requestId := r.Context().Value("requestId") -} - -func start() { - -} - -func HandleStop(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) var containerId string err := decoder.Decode(&containerId) if err != nil { - log.LogError("Failed to decode node/stop request body!") + log.LogError("Failed to decode node/start request body! (%s)\n%s", requestId, err) w.WriteHeader(http.StatusBadRequest) return } if containerId == "" { - log.LogError("Container id mus be specified for node/stop!") + log.LogError("Container id mus be specified for node/start! (%s)", requestId) w.WriteHeader(http.StatusBadRequest) return } - if stop(containerId) { + err = start(containerId) + if err == nil { + log.LogInfo("Successfully container start: %s (%s)", containerId, requestId) w.WriteHeader(http.StatusOK) } else { + log.LogInfo("Failed container start! (%s)\n%s", requestId, err) w.WriteHeader(http.StatusInternalServerError) } } -func stop(containerId string) bool { +func start(containerId string) error { + cli := openDockerClient() + _, err := cli.ContainerStart(context.Background(), containerId, client.ContainerStartOptions{}) + if err != nil { + return err + } + return nil +} + +func HandleStop(w http.ResponseWriter, r *http.Request) { + requestId := r.Context().Value("requestId") + + decoder := json.NewDecoder(r.Body) + var containerId string + err := decoder.Decode(&containerId) + if err != nil { + log.LogError("Failed to decode node/stop request body! (%s)", requestId) + w.WriteHeader(http.StatusBadRequest) + return + } + if containerId == "" { + log.LogError("Container id must be specified for node/stop! (%s)", requestId) + w.WriteHeader(http.StatusBadRequest) + return + } + + err = stop(containerId) + if err == nil { + log.LogInfo("Successfully container stop: %s (%s)", containerId, requestId) + w.WriteHeader(http.StatusOK) + } else { + log.LogInfo("Failed container stop! (%s)\n%s", requestId, err) + w.WriteHeader(http.StatusInternalServerError) + } +} + +func stop(containerId string) error { cli := openDockerClient() _, err := cli.ContainerStop(context.Background(), containerId, client.ContainerStopOptions{}) if err != nil { - log.LogError("Failed to stop container: %s\n%s", containerId, err) - return false + return err } - log.LogInfo("Successfully stopped container: %s", containerId) - return true + return nil } func HandleInfo(w http.ResponseWriter, r *http.Request) { diff --git a/router/router.go b/router/router.go index a2c900d..848281e 100644 --- a/router/router.go +++ b/router/router.go @@ -18,14 +18,8 @@ func SetupRouter() *mux.Router { r.HandleFunc("/health", health.Handle).Methods("GET", "OPTIONS") r.HandleFunc("/node/info", node.HandleInfo).Methods("GET", "OPTIONS") - - /* - r.HandleFunc("/album", handler.GetAlbums).Methods("GET", "OPTIONS") - r.HandleFunc("/album", handler.SaveAlbum).Methods("POST", "OPTIONS") - r.HandleFunc("/album/{albumId}", handler.DeleteAlbum).Methods("DELETE", "OPTIONS") - r.HandleFunc("/album/{albumId}", handler.GetAlbum).Methods("GET", "OPTIONS") - r.HandleFunc("/shareAlbum/{albumId}/{userId}", handler.ShareAlbum).Methods("POST", "OPTIONS") - */ + r.HandleFunc("/node/start", node.HandleStart).Methods("POST", "OPTIONS") + r.HandleFunc("/node/stop", node.HandleStop).Methods("POST", "OPTIONS") return r }