Made the create command require a port number

This commit is contained in:
2026-06-05 23:38:42 -05:00
parent 53b9f30d50
commit 9f4e27869b
3 changed files with 95 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
@@ -41,6 +42,7 @@ func StartDaemon(cfg *AppConfig, configPath string) error {
mux.HandleFunc("/instances/stop", ds.handleStop)
mux.HandleFunc("/instances/command", ds.handleCommand)
mux.HandleFunc("/instances/list", ds.handleList)
mux.HandleFunc("/instances/logs", ds.handleGetLogs)
corsWrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
@@ -143,8 +145,15 @@ func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
version := r.URL.Query().Get("version")
if name == "" || version == "" {
http.Error(w, "Missing name or version parameters", http.StatusBadRequest)
port := r.URL.Query().Get("port")
if name == "" || version == "" || port == "" {
http.Error(w, "Missing name, version, or port parameters", http.StatusBadRequest)
return
}
converted_port, err := strconv.Atoi(port)
if err != nil {
http.Error(w, "Could not convert provided port to a valid port number", http.StatusBadRequest)
return
}
@@ -156,11 +165,11 @@ func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
options := VsServerConfigOptions{
Version: version,
ServerName: name,
Port: 42424,
Port: converted_port,
MaxClients: 10,
}
err := DownloadAndExtractServer(version, ds.cfg.Storage.InstallDir)
err = DownloadAndExtractServer(version, ds.cfg.Storage.InstallDir)
if err != nil {
http.Error(w, fmt.Sprintf("Installation failed: %v", err), http.StatusInternalServerError)
return
@@ -303,3 +312,33 @@ func (ds *DaemonServer) handleList(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(responseList)
}
func (ds *DaemonServer) handleGetLogs(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing name parameter", http.StatusBadRequest)
return
}
ds.procManager.RLock()
buf, exists := ds.procManager.LogBuffers[name]
ds.procManager.RUnlock()
if !exists || buf == nil {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("[]"))
return
}
logLines := buf.GetSnapshot()
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(logLines); err != nil {
http.Error(w, fmt.Sprintf("Failed encoding log matrix: %v", err), http.StatusInternalServerError)
}
}