summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2021-06-18 16:03:17 +0200
committerxengineering <mail2xengineering@protonmail.com>2021-06-18 16:57:15 +0200
commit5643b52829587c6bc723233a14b2bdc3f52b3879 (patch)
treef4873d48996083d2d2bf7cee4411f074b3633b2a
parent7e08ae6ff7701632c8e3919b9dd3a7a13970835b (diff)
downloadbirdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.tar
birdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.tar.zst
birdscan-5643b52829587c6bc723233a14b2bdc3f52b3879.zip
Implement reboot and poweroff via Web Interface
-rw-r--r--Makefile1
-rw-r--r--data/html/index.html2
-rw-r--r--data/js/birdscan.js12
-rw-r--r--src/main.go3
-rw-r--r--src/web.go38
-rw-r--r--sudo/birdscan1
6 files changed, 54 insertions, 3 deletions
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 @@
<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()
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