Node endpoints with container handling #1

Merged
gdulai merged 3 commits from dev into main 2026-02-04 22:14:12 +00:00
2 changed files with 48 additions and 23 deletions
Showing only changes of commit 1369b3cf8b - Show all commits

View File

@@ -25,44 +25,75 @@ func build(command BuildCommand) {
} }
func HandleStart(w http.ResponseWriter, r *http.Request) { 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) decoder := json.NewDecoder(r.Body)
var containerId string var containerId string
err := decoder.Decode(&containerId) err := decoder.Decode(&containerId)
if err != nil { 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) w.WriteHeader(http.StatusBadRequest)
return return
} }
if containerId == "" { 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) w.WriteHeader(http.StatusBadRequest)
return return
} }
if stop(containerId) { err = start(containerId)
if err == nil {
log.LogInfo("Successfully container start: %s (%s)", containerId, requestId)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} else { } else {
log.LogInfo("Failed container start! (%s)\n%s", requestId, err)
w.WriteHeader(http.StatusInternalServerError) 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() cli := openDockerClient()
_, err := cli.ContainerStop(context.Background(), containerId, client.ContainerStopOptions{}) _, err := cli.ContainerStop(context.Background(), containerId, client.ContainerStopOptions{})
if err != nil { if err != nil {
log.LogError("Failed to stop container: %s\n%s", containerId, err) return err
return false
} }
log.LogInfo("Successfully stopped container: %s", containerId) return nil
return true
} }
func HandleInfo(w http.ResponseWriter, r *http.Request) { 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("/health", health.Handle).Methods("GET", "OPTIONS")
r.HandleFunc("/node/info", node.HandleInfo).Methods("GET", "OPTIONS") r.HandleFunc("/node/info", node.HandleInfo).Methods("GET", "OPTIONS")
r.HandleFunc("/node/start", node.HandleStart).Methods("POST", "OPTIONS")
/* r.HandleFunc("/node/stop", node.HandleStop).Methods("POST", "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")
*/
return r return r
} }