From 5643b52829587c6bc723233a14b2bdc3f52b3879 Mon Sep 17 00:00:00 2001 From: xengineering Date: Fri, 18 Jun 2021 16:03:17 +0200 Subject: Implement reboot and poweroff via Web Interface --- Makefile | 1 + data/html/index.html | 2 ++ data/js/birdscan.js | 12 ++++++++++++ src/main.go | 3 --- src/web.go | 38 ++++++++++++++++++++++++++++++++++++++ sudo/birdscan | 1 + 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 sudo/birdscan diff --git a/Makefile b/Makefile index 550624a..98f694c 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ install: all install -Dm 644 data/html/index.html $(DESTDIR)$(PREFIX)/share/birdscan/html/index.html install -Dm 644 data/css/birdscan.css $(DESTDIR)$(PREFIX)/share/birdscan/css/birdscan.css install -Dm 644 data/js/birdscan.js $(DESTDIR)$(PREFIX)/share/birdscan/js/birdscan.js + install -Dm 644 sudo/birdscan $(DESTDIR)/etc/sudoers.d/50_birdscan debug: all make -C src debug diff --git a/data/html/index.html b/data/html/index.html index 268aad9..002f50a 100644 --- a/data/html/index.html +++ b/data/html/index.html @@ -27,6 +27,8 @@

A software to take beautiful pictures of birds with a Raspberry Pi Camera.

+ + diff --git a/data/js/birdscan.js b/data/js/birdscan.js index d3a46c3..87ec3e7 100644 --- a/data/js/birdscan.js +++ b/data/js/birdscan.js @@ -8,6 +8,18 @@ function singlePicture() { xhttp.send(); } +function poweroff() { + const xhttp = new XMLHttpRequest(); + xhttp.open("POST", "./api/poweroff", true); + xhttp.send(); +} + +function reboot() { + const xhttp = new XMLHttpRequest(); + xhttp.open("POST", "./api/reboot", true); + xhttp.send(); +} + function updateState() { if (!updateStatePending) { updateStatePending = true; diff --git a/src/main.go b/src/main.go index e61dfb5..3218df9 100644 --- a/src/main.go +++ b/src/main.go @@ -19,9 +19,6 @@ func main() { // parse flags and read config config = GetRuntimeConfig() - // print startup message - log.Println("Starting birdscan") - // create camera camera = NewCamera() diff --git a/src/web.go b/src/web.go index ab15aba..f7c7b49 100644 --- a/src/web.go +++ b/src/web.go @@ -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)) +} diff --git a/sudo/birdscan b/sudo/birdscan new file mode 100644 index 0000000..033cd18 --- /dev/null +++ b/sudo/birdscan @@ -0,0 +1 @@ +birdscan ALL= NOPASSWD: /usr/bin/poweroff,/usr/bin/reboot -- cgit v1.2.3-70-g09d2