From 650138aae6a2d6ae26f57a22056bdf0ea5fa8c77 Mon Sep 17 00:00:00 2001 From: Petri Hienonen Date: Sun, 28 Sep 2025 10:45:49 +0300 Subject: Initial commit --- main.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 main.go (limited to 'main.go') diff --git a/main.go b/main.go new file mode 100644 index 0000000..046ad91 --- /dev/null +++ b/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "encoding/json" + "html/template" + "log" + "net/http" + "os/exec" +) + +// Templates +var tpl = template.Must(template.ParseFiles("index.html")) + +// Handlers +func indexHandler(w http.ResponseWriter, r *http.Request) { + tpl.Execute(w, nil) +} + +// Run `networkctl status --json=short` +func statusHandler(w http.ResponseWriter, r *http.Request) { + cmd := exec.Command("networkctl", "status", "--json=short") + out, err := cmd.Output() + if err != nil { + http.Error(w, err.Error(), 500) + return + } + w.Header().Set("Content-Type", "application/json") + w.Write(out) +} + +// Run `journalctl -u systemd-networkd.service --no-pager -n 200` +func logsHandler(w http.ResponseWriter, r *http.Request) { + cmd := exec.Command("journalctl", "-u", "systemd-networkd.service", "--no-pager", "-n", "200") + out, err := cmd.Output() + if err != nil { + http.Error(w, err.Error(), 500) + return + } + w.Header().Set("Content-Type", "text/plain") + w.Write(out) +} + +// Reload systemd-networkd +func reloadHandler(w http.ResponseWriter, r *http.Request) { + cmd := exec.Command("systemctl", "restart", "systemd-networkd") + if err := cmd.Run(); err != nil { + http.Error(w, err.Error(), 500) + return + } + json.NewEncoder(w).Encode(map[string]string{"status": "restarted"}) +} + +// Reboot the system +func rebootHandler(w http.ResponseWriter, r *http.Request) { + cmd := exec.Command("systemctl", "reboot") + if err := cmd.Start(); err != nil { + http.Error(w, err.Error(), 500) + return + } + json.NewEncoder(w).Encode(map[string]string{"status": "rebooting"}) +} + +func main() { + http.HandleFunc("/", indexHandler) + http.HandleFunc("/api/status", statusHandler) + http.HandleFunc("/api/logs", logsHandler) + http.HandleFunc("/api/reload", reloadHandler) + http.HandleFunc("/api/reboot", rebootHandler) + + log.Println("Listening on :8080") + log.Fatal(http.ListenAndServe(":8080", nil)) +} -- cgit v1.2.3-70-g09d2