diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-06-18 16:03:17 +0200 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-06-18 16:57:15 +0200 |
commit | 5643b52829587c6bc723233a14b2bdc3f52b3879 (patch) | |
tree | f4873d48996083d2d2bf7cee4411f074b3633b2a /src/web.go | |
parent | 7e08ae6ff7701632c8e3919b9dd3a7a13970835b (diff) | |
download | birdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.tar birdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.tar.zst birdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.zip |
Implement reboot and poweroff via Web Interface
Diffstat (limited to 'src/web.go')
-rw-r--r-- | src/web.go | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -6,6 +6,7 @@ import ( "fmt" "log" "net/http" + "os/exec" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" @@ -39,6 +40,8 @@ func (server *WebServer) run() { router.Get("/js/birdscan.js", jsHandler) router.Post("/api/state", stateHandler) router.Post("/api/single_picture", singlePictureHandler) + router.Post("/api/reboot", rebootHandler) + router.Post("/api/poweroff", poweroffHandler) log.Println("Binding to 'http://" + server.config.BindAddress + ":" + server.config.BindPort + "'") log.Fatal(http.ListenAndServe(server.config.BindAddress + ":" + server.config.BindPort, router)) @@ -66,3 +69,38 @@ func singlePictureHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, http.StatusText(http.StatusOK)) } +func rebootHandler(w http.ResponseWriter, r *http.Request) { + log.Println("starting reboot") + + if config.Flag.Debug { + log.Println("Skipping reboot because of debug mode") + } else { + cmd := exec.Command("/usr/bin/sudo", "/usr/bin/reboot") + err := cmd.Run() + if err != nil { + log.Print(err) + fmt.Fprintf(w, http.StatusText(http.StatusForbidden)) + return + } + } + + fmt.Fprintf(w, http.StatusText(http.StatusOK)) +} + +func poweroffHandler(w http.ResponseWriter, r *http.Request) { + log.Println("starting poweroff") + + if config.Flag.Debug { + log.Println("Skipping poweroff because of debug mode") + } else { + cmd := exec.Command("/usr/bin/sudo", "/usr/bin/poweroff") + err := cmd.Run() + if err != nil { + log.Print(err) + fmt.Fprintf(w, http.StatusText(http.StatusForbidden)) + return + } + } + + fmt.Fprintf(w, http.StatusText(http.StatusOK)) +} |