added more clear output

This commit is contained in:
Stefan Friese 2025-01-20 15:32:13 +00:00
parent 3e4722e3ca
commit 555d235c3d
4 changed files with 38 additions and 6 deletions

6
.env
View File

@ -1,7 +1,7 @@
#!/usr/bin/env sh
export DB_HOST="172.17.0.2"
export DB_HOST="127.0.0.1"
export DB_PORT=3306
export DB_USERNAME="root"
export DB_PASSWORD="root"
export DB_USERNAME="mysql"
export DB_PASSWORD="mysql"
export DB_NAME="gomatic"

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.env
gontrol
bin/*
build/

View File

@ -165,10 +165,24 @@ func Server() (*http.Server) {
},
}
corsMiddleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") // Allow the WebUI origin
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, HX-Current-URL, HX-Request, HX-Target, HX-Trigger, HX-Trigger-Name")
if r.Method == "OPTIONS" {
// Handle preflight requests
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
webSocketMux := http.NewServeMux()
webSocketMux.Handle("/data", webSocketHandler)
webSocketMux.Handle("/executeCommand", executeCommand)
webSocketMux.Handle("/agentNames", getAgentNames)
webSocketMux.Handle("/executeCommand", corsMiddleware(http.HandlerFunc(executeCommand)))
webSocketMux.Handle("/agentNames", corsMiddleware(http.HandlerFunc(getAgentNames)))
websocketServer := &http.Server{
Addr: ":5555",
Handler: webSocketMux,

View File

@ -31,6 +31,23 @@
});
})
.catch(error => console.error('Error fetching agent names:', error));
const socket = new WebSocket("ws://localhost:5555/data");
socket.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.type === 'response') {
const output = document.getElementById('commandOutput');
output.textContent = message.payload;
}
};
socket.onerror = (error) => {
console.error("Websocket error:", error);
};
socket.onclose = () => {
console.warn('Websocket connection closed.');
};
});
</script>
</head>
@ -45,7 +62,7 @@
<!-- Agent Commands -->
<div id="agentCommands">
<h3>Command Execution</h3>
<form hx-post="/executeCommand" hx-target="#commandOutput" hx-swap="innerHTML">
<form hx-post="http://localhost:5555/executeCommand" hx-target="#commandOutput" hx-encoding="application/x-www-form-urlencoded" hx-swap="innerHTML">
<div class="mb-3">
<label for="agentName" class="form-label">Agent Name</label>
<select class="form-select" id="agentName" name="agentName" required>