diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.go | 3 | ||||
-rw-r--r-- | src/web.go | 38 |
2 files changed, 38 insertions, 3 deletions
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)) +} |