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 | |
parent | 7e08ae6ff7701632c8e3919b9dd3a7a13970835b (diff) | |
download | birdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.tar birdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.tar.zst birdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.zip |
Implement reboot and poweroff via Web Interface
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | data/html/index.html | 2 | ||||
-rw-r--r-- | data/js/birdscan.js | 12 | ||||
-rw-r--r-- | src/main.go | 3 | ||||
-rw-r--r-- | src/web.go | 38 | ||||
-rw-r--r-- | sudo/birdscan | 1 |
6 files changed, 54 insertions, 3 deletions
@@ -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 @@ <p>A software to take beautiful pictures of birds with a Raspberry Pi Camera.</p> <p id="state"></p> <button onclick="singlePicture()">Take single picture</button> + <button onclick="reboot()">reboot</button> + <button onclick="poweroff()">poweroff</button> </main> <script src="./js/birdscan.js"></script> 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() @@ -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 |