feat: implement headless server mode with API endpoints for metrics and process management

This commit is contained in:
Jonathan Atta
2026-03-11 17:51:12 +01:00
parent e7c8e66889
commit 29e8f9b887
3 changed files with 261 additions and 0 deletions

69
main.go
View File

@@ -2,6 +2,10 @@ package main
import (
"embed"
"fmt"
"os"
"strconv"
"strings"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
@@ -12,6 +16,18 @@ import (
var assets embed.FS
func main() {
noGUI, port, help := parseSysmonArgs()
if help {
printHelp()
os.Exit(0)
}
if noGUI {
runServer(port)
return
}
// Create an instance of the app structure
app := NewApp()
@@ -34,3 +50,56 @@ func main() {
println("Error:", err.Error())
}
}
// parseSysmonArgs scans os.Args manually so we don't call flag.Parse(),
// which would conflict with flags that Wails injects in dev mode (e.g. -assetdir).
func parseSysmonArgs() (noGUI bool, port int, help bool) {
port = 9731
args := os.Args[1:]
for i := 0; i < len(args); i++ {
arg := args[i]
switch arg {
case "--no-gui":
noGUI = true
case "--help", "-h":
help = true
case "--port":
if i+1 < len(args) {
i++
if p, err := strconv.Atoi(args[i]); err == nil && p > 0 && p <= 65535 {
port = p
}
}
default:
if strings.HasPrefix(arg, "--port=") {
if p, err := strconv.Atoi(strings.TrimPrefix(arg, "--port=")); err == nil && p > 0 && p <= 65535 {
port = p
}
}
}
}
return
}
func printHelp() {
fmt.Print(`sysmon — system monitor
Usage:
sysmon Launch with graphical interface (default)
sysmon --no-gui Run as headless HTTP API server (port 9731)
sysmon --no-gui --port N Run server on port N
Options:
--no-gui Start without GUI, expose HTTP API instead
--port <number> HTTP server port (default: 9731, requires --no-gui)
--help, -h Show this help
Server endpoints (--no-gui mode):
GET /api/metrics Latest metrics snapshot (JSON)
GET /api/metrics/stream Server-Sent Events stream (1 s interval)
GET /api/process/<pid> Process detail (JSON)
POST /api/process/<pid>/kill Kill process (?force=true → SIGKILL)
GET /api/man/<name> Manual page for a command
`)
}