From 31296f5b0f01a481419f4bdbd40ef7b733bfadb5 Mon Sep 17 00:00:00 2001 From: William P Date: Sun, 27 Jul 2025 07:54:18 -0400 Subject: [PATCH] add poweroff and reboot functions --- client/power/power.go | 9 +++++++++ client/power/poweroff.go | 30 ++++++++++++++++++++++++++++++ client/power/reboot.go | 30 ++++++++++++++++++++++++++++++ client/routes/routes.go | 11 +++++++---- 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 client/power/power.go create mode 100644 client/power/poweroff.go create mode 100644 client/power/reboot.go diff --git a/client/power/power.go b/client/power/power.go new file mode 100644 index 0000000..0bf1b9c --- /dev/null +++ b/client/power/power.go @@ -0,0 +1,9 @@ +package power + +const APIVersion string = "power/v1alpha1" + +type PowerActionResponse struct { + APIVersion string `json:"apiVersion"` + Action string `json:"action"` + Success bool `json:"success"` +} diff --git a/client/power/poweroff.go b/client/power/poweroff.go new file mode 100644 index 0000000..c7efe03 --- /dev/null +++ b/client/power/poweroff.go @@ -0,0 +1,30 @@ +package power + +import ( + "net/http" + "os/exec" + + "git.dubyatp.xyz/orphanage/client/httputil" +) + +func PowerOffResponse(success bool) PowerActionResponse { + resp := PowerActionResponse{ + APIVersion: APIVersion, + Action: "poweroff", + Success: success, + } + return resp +} + +func PowerOff(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + cmd := exec.Command("systemctl", "poweroff") + err := cmd.Run() + + if err != nil { + httputil.WriteJSON(w, http.StatusOK, PowerOffResponse(false)) + } else { + httputil.WriteJSON(w, http.StatusOK, PowerOffResponse(true)) + } + }) +} diff --git a/client/power/reboot.go b/client/power/reboot.go new file mode 100644 index 0000000..998aef6 --- /dev/null +++ b/client/power/reboot.go @@ -0,0 +1,30 @@ +package power + +import ( + "net/http" + "os/exec" + + "git.dubyatp.xyz/orphanage/client/httputil" +) + +func RebootResponse(success bool) PowerActionResponse { + resp := PowerActionResponse{ + APIVersion: APIVersion, + Action: "reboot", + Success: success, + } + return resp +} + +func Reboot(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + cmd := exec.Command("systemctl", "reboot") + err := cmd.Run() + + if err != nil { + httputil.WriteJSON(w, http.StatusOK, RebootResponse(false)) + } else { + httputil.WriteJSON(w, http.StatusOK, RebootResponse(true)) + } + }) +} diff --git a/client/routes/routes.go b/client/routes/routes.go index 9423d3d..b000cda 100644 --- a/client/routes/routes.go +++ b/client/routes/routes.go @@ -5,14 +5,17 @@ import ( "git.dubyatp.xyz/orphanage/client/facts" "git.dubyatp.xyz/orphanage/client/httputil" + "git.dubyatp.xyz/orphanage/client/power" "git.dubyatp.xyz/orphanage/client/testfunc" ) func AddRoutes( mux *http.ServeMux, ) { - mux.Handle("/", http.NotFoundHandler()) - mux.Handle("/helloworld", httputil.HelloWorld(nil)) - mux.Handle("/testjson", testfunc.HelloWorldJSON(nil)) - mux.Handle("/facts", facts.GetFacts(nil)) + mux.Handle("GET /", http.NotFoundHandler()) + mux.Handle("GET /helloworld", httputil.HelloWorld(nil)) + mux.Handle("GET /testjson", testfunc.HelloWorldJSON(nil)) + mux.Handle("GET /facts", facts.GetFacts(nil)) + mux.Handle("POST /power/reboot", power.Reboot(nil)) + mux.Handle("POST /power/poweroff", power.PowerOff(nil)) }