summaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorPetri Hienonen <petri.hienonen@gmail.com>2025-09-28 10:45:49 +0300
committerPetri Hienonen <petri.hienonen@gmail.com>2025-09-28 10:45:49 +0300
commit650138aae6a2d6ae26f57a22056bdf0ea5fa8c77 (patch)
tree6b90cc177702c24b399b078b0f0014fc4e3af614 /main.go
downloadnetwork-650138aae6a2d6ae26f57a22056bdf0ea5fa8c77.tar.zst
Initial commit
Diffstat (limited to 'main.go')
-rw-r--r--main.go72
1 files changed, 72 insertions, 0 deletions
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))
+}