diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..04ed523 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +notes/ \ No newline at end of file diff --git a/README.md b/README.md index 1689d33..aba9f30 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,18 @@ ## Skeleton -* Stick with HL2 and HL2DM +* Stick with HL2DM * Set up routing * GS Lifecycle Management and Statistics * Concurrency - manage simultaneously events and schedule tasks. -* File Handling - configs, logs, backups and export config server -* TODO: Add auth and db stuff later. For persistent configurations. \ No newline at end of file +* File Handling - configs, logs, backups and export server configs. +* TODO: Add auth and db stuff later. For persistent configurations. + + +## Status + +* running srcds_run directly for the moment. +* file permissions are not being handled. +* ideal systemd integration? +* min add screen/tmux. +* routing : index -> /hl2 -> ok/err response to start & stop diff --git a/main.go b/main.go index a1418ea..6065b55 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,45 @@ import ( "html/template" "log" "net/http" + "os/exec" + "syscall" ) +var serverCmd *exec.Cmd + +func startServer(w http.ResponseWriter, r *http.Request) { + if serverCmd != nil { + fmt.Fprintln(w, "Server is already running.") + return + } + + serverCmd = exec.Command("/opt/hl2dm/Steam/hl2dm/srcds_run", "-game", "hl2mp", "+map", "dm_lockdown", "+maxplayers", "16") + serverCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} // Allow stopping the entire process group + + if err := serverCmd.Start(); err != nil { + log.Println("Error starting server:", err) + http.Error(w, "Failed to start server", http.StatusInternalServerError) + return + } + + fmt.Fprintln(w, "Server started with PID", serverCmd.Process.Pid) +} + +func stopServer(w http.ResponseWriter, r *http.Request) { + if serverCmd == nil { + fmt.Fprintln(w, "Server is not running.") + return + } + + if err := syscall.Kill(-serverCmd.Process.Pid, syscall.SIGKILL); err != nil { + log.Println("Error stopping server:", err) + http.Error(w, "Failed to stop server", http.StatusInternalServerError) + return + } + + serverCmd = nil + fmt.Fprintln(w, "Server stopped.") +} func main() { http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) @@ -14,6 +51,9 @@ func main() { http.HandleFunc("/", homeHandler) http.HandleFunc("/hl2", hl2Handler) http.HandleFunc("/quake", quakeHandler) + http.HandleFunc("/start", startServer) + http.HandleFunc("/stop", stopServer) + // http.HandleFunc("/restart", restartServer) // Start the server fmt.Println("Server running on http://localhost:8080") diff --git a/templates/hl2.xhtml b/templates/hl2.xhtml index f291c05..c01e076 100644 --- a/templates/hl2.xhtml +++ b/templates/hl2.xhtml @@ -12,6 +12,15 @@

Multiplayer Server

Manage or join the multiplayer servers here.

+
+ +
+
+ +
+
+ +

Coop