Add Server Start Stop

Adds start and stop server to the /hl2 deathmatch server
runs executable directly.

Signed-off-by: Pratyush Desai <pratyush.desai@liberta.casa>
This commit is contained in:
Pratyush Desai 2024-11-03 03:52:21 +05:30
parent d3193b92dc
commit b02e8ee84e
Signed by: pratyush
GPG Key ID: DBA5BB7505946FAD
4 changed files with 62 additions and 3 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
notes/

View File

@ -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
* 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

40
main.go
View File

@ -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")

View File

@ -12,6 +12,15 @@
<div class="section">
<h2>Multiplayer Server</h2>
<p>Manage or join the multiplayer servers here.</p>
<form action="/start" method="post">
<button type="submit">Start Server</button>
</form>
<form action="/stop" method="post">
<button type="submit">Stop Server</button>
</form>
<form action="/restart" method="post">
<button type="submit">Restart Server</button>
</form>
</div>
<div class="section">
<h2>Coop</h2>