summaryrefslogtreecommitdiff
path: root/src/web.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/web.go')
-rw-r--r--src/web.go38
1 files changed, 38 insertions, 0 deletions
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))
+}