Added a status command

This commit is contained in:
2026-06-05 17:21:40 -05:00
parent 70483037c9
commit d29d51cede
2 changed files with 76 additions and 1 deletions

View File

@@ -19,6 +19,13 @@ type DaemonServer struct {
procManager *ProcessManager
}
type InstanceStatusResponse struct {
Name string `json:"name"`
Version string `json:"version"`
Port int `json:"port"`
Status string `json:"status"`
}
func StartDaemon(cfg *AppConfig) error {
ds := &DaemonServer{
cfg: cfg,
@@ -30,6 +37,7 @@ func StartDaemon(cfg *AppConfig) error {
mux.HandleFunc("/instances/start", ds.handleStart)
mux.HandleFunc("/instances/stop", ds.handleStop)
mux.HandleFunc("/instances/command", ds.handleCommand)
mux.HandleFunc("/instances/list", ds.handleList)
server := &http.Server{
Addr: cfg.Daemon.ListenAddress,
@@ -72,7 +80,7 @@ func (ds *DaemonServer) handleCreate(w http.ResponseWriter, r *http.Request) {
options := VsServerConfigOptions{
Version: version,
ServerName: name,
Port: 42424, // Pull from custom parameters later if desired
Port: 42424,
MaxClients: 10,
}
@@ -176,3 +184,33 @@ func (ds *DaemonServer) handleCommand(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Command delivered successfully to %s", name)
}
func (ds *DaemonServer) handleList(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
ds.procManager.RLock()
defer ds.procManager.RUnlock()
var responseList []InstanceStatusResponse
for name, options := range ds.cfg.Instances {
status := "STOPPED"
if _, running := ds.procManager.ActiveInstances[name]; running {
status = "RUNNING"
}
responseList = append(responseList, InstanceStatusResponse{
Name: name,
Version: options.Version,
Port: options.Port,
Status: status,
})
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(responseList)
}