feat: Implemented (un)install,start/stop flow and fixed actions flow
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"git.dcntrlized.org/Dcntrlized/Maestro/lib/docker"
|
||||
"git.dcntrlized.org/Dcntrlized/Maestro/lib/logger"
|
||||
)
|
||||
|
||||
func RunAction(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -14,6 +15,8 @@ func RunAction(w http.ResponseWriter, r *http.Request) {
|
||||
actionName := r.PathValue("actionName")
|
||||
containerId := r.PathValue("containerId")
|
||||
|
||||
logger.Debug("Received request to run action %s on container id %s", actionName, containerId)
|
||||
|
||||
// Get IP address of the container using containerId
|
||||
dockerClient, err := docker.NewDockerClient()
|
||||
if err != nil {
|
||||
@@ -34,16 +37,36 @@ func RunAction(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Construct the URL for the action endpoint
|
||||
actionURL := fmt.Sprintf("http://%s:%d/%s", network.IPAddress, network.Ports[0].Host, actionName) // Assuming network.Ports[0] is the main port. Might change in the future
|
||||
actionURL := fmt.Sprintf("http://%s:%d/action/%s", network.IPAddress.Address, network.Ports[0].Host, actionName) // Assuming network.Ports[0] is the main port. Might change in the future
|
||||
|
||||
// Forward the request to the container's action endpoint
|
||||
resp, err := http.Get(actionURL)
|
||||
if err != nil {
|
||||
logger.Debug("Failed to forward action request %s to container %s: %w", actionName, containerId, err)
|
||||
http.Error(w, "Failed to forward request to container", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logger.Debug("Failed to read container error response body: %v", err)
|
||||
}
|
||||
|
||||
responseBodyString := string(bodyBytes)
|
||||
|
||||
if resp.StatusCode == 404 {
|
||||
logger.Info("The requested action does not exist")
|
||||
http.Error(w, "The requested action does not exist", http.StatusNotFound)
|
||||
}
|
||||
|
||||
logger.Info("Container responded with non-2xx code")
|
||||
logger.Debug("Container: %s | Called URL: %s | Status: %d | Response: %s",
|
||||
containerId, actionURL, resp.StatusCode, responseBodyString)
|
||||
http.Error(w, "Container responded with non-2xx code", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
// Copy the response from the container back to the original response writer
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
_, err = io.Copy(w, resp.Body)
|
||||
|
||||
Reference in New Issue
Block a user