Container start/stop impl

This commit is contained in:
2026-02-03 22:03:08 +01:00
parent b8761de020
commit 1369b3cf8b
2 changed files with 48 additions and 23 deletions

View File

@@ -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) {

View File

@@ -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
}