feat: implement action proxy route

This commit is contained in:
2026-07-31 20:30:47 +01:00
parent a40dbe0f6e
commit eb01569f7d
2 changed files with 49 additions and 5 deletions
+48 -4
View File
@@ -1,10 +1,54 @@
package actions
import "net/http"
import (
"context"
"fmt"
"io"
"net/http"
"git.dcntrlized.org/Dcntrlized/Maestro/lib/docker"
)
func RunAction(w http.ResponseWriter, r *http.Request) {
// Stub, to implement, receives an action via frontend code and redirects it to the correct container, which will execute it and return the result to the frontend
// Request comes in format {"container_id": string, "action": { "method": "GET" | "POST" | "PUT" | "DELETE", "action_name": string }, "payload": object}
// Runs an action on the container specified by the actionName and containerId path parameters.
actionName := r.PathValue("actionName")
containerId := r.PathValue("containerId")
http.Error(w, "Action not implemented", http.StatusNotImplemented)
// Get IP address of the container using containerId
dockerClient, err := docker.NewDockerClient()
if err != nil {
http.Error(w, "Failed to create Docker client", http.StatusInternalServerError)
return
}
inspectData, err := dockerClient.ContainerInspect(context.Background(), containerId)
if err != nil {
http.Error(w, "Failed to inspect container", http.StatusInternalServerError)
return
}
network, _, err := docker.ExtractNetworkInfo(context.Background(), dockerClient, inspectData)
if err != nil {
http.Error(w, "Failed to extract network info", http.StatusInternalServerError)
return
}
// 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
// Forward the request to the container's action endpoint
resp, err := http.Get(actionURL)
if err != nil {
http.Error(w, "Failed to forward request to container", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Copy the response from the container back to the original response writer
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
if err != nil {
http.Error(w, "Failed to read response from container", http.StatusInternalServerError)
return
}
}