From 6df5b35600e6aae6e676e2ba29eeaaf88bf9ff9b Mon Sep 17 00:00:00 2001 From: xengineering Date: Fri, 11 Jun 2021 11:40:46 +0200 Subject: Implement AJAX Call --- Makefile | 1 + data/html/index.html | 2 ++ data/js/birdscan.js | 7 +++++++ src/web.go | 11 +++++++++++ 4 files changed, 21 insertions(+) create mode 100644 data/js/birdscan.js diff --git a/Makefile b/Makefile index 7531fdb..7e3e517 100644 --- a/Makefile +++ b/Makefile @@ -21,4 +21,5 @@ install: all install -Dm 644 systemd/birdscan.sysusers $(DESTDIR)$(PREFIX)/lib/sysusers.d/birdscan.conf 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 diff --git a/data/html/index.html b/data/html/index.html index af72c46..eddc2b3 100644 --- a/data/html/index.html +++ b/data/html/index.html @@ -25,8 +25,10 @@

birdscan

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 new file mode 100644 index 0000000..d1bba04 --- /dev/null +++ b/data/js/birdscan.js @@ -0,0 +1,7 @@ + +function singlePicture() { + const xhttp = new XMLHttpRequest(); + xhttp.open("POST", "./api/single_picture"); + xhttp.send(); +} + diff --git a/src/web.go b/src/web.go index 4306b2c..23bf1ff 100644 --- a/src/web.go +++ b/src/web.go @@ -26,6 +26,8 @@ func runServer(cfg *webConfig) { router.Get("/", indexHandler) router.Get("/css/birdscan.css", cssHandler) + router.Get("/js/birdscan.js", jsHandler) + router.Post("/api/single_picture", singlePictureHandler) log.Println("Binding to 'http://" + cfg.BindAddress + ":" + cfg.BindPort + "'") log.Fatal(http.ListenAndServe(cfg.BindAddress + ":" + cfg.BindPort, router)) @@ -39,3 +41,12 @@ func cssHandler(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, fmt.Sprintf("%s/css/birdscan.css", APP_DATA)) } +func jsHandler(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, fmt.Sprintf("%s/js/birdscan.js", APP_DATA)) +} + +func singlePictureHandler(w http.ResponseWriter, r *http.Request) { + log.Println("Received request for a single picture") + fmt.Fprintf(w, http.StatusText(http.StatusOK)) +} + -- cgit v1.2.3-70-g09d2