From 9d5a2f9870e52bfc0fe6db8c27981f29d91dcb55 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 13 Jun 2021 12:54:28 +0200 Subject: Implement working State Machine --- src/web.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/web.go') diff --git a/src/web.go b/src/web.go index 23bf1ff..6515f47 100644 --- a/src/web.go +++ b/src/web.go @@ -15,12 +15,22 @@ const ( APP_DATA = "/usr/share/birdscan" ) +type WebServer struct { + config *webConfig +} + type webConfig struct { BindAddress string `json:"bind_address"` BindPort string `json:"bind_port"` } -func runServer(cfg *webConfig) { +func NewWebServer(cfg *webConfig) WebServer { + server := WebServer{} + server.config = cfg + return server +} + +func (server *WebServer) run() { router := chi.NewRouter() router.Use(middleware.Logger) @@ -29,8 +39,8 @@ func runServer(cfg *webConfig) { 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)) + log.Println("Binding to 'http://" + server.config.BindAddress + ":" + server.config.BindPort + "'") + log.Fatal(http.ListenAndServe(server.config.BindAddress + ":" + server.config.BindPort, router)) } func indexHandler(w http.ResponseWriter, r *http.Request) { @@ -47,6 +57,7 @@ func jsHandler(w http.ResponseWriter, r *http.Request) { func singlePictureHandler(w http.ResponseWriter, r *http.Request) { log.Println("Received request for a single picture") + camera.SendEvent("take_single_picture") fmt.Fprintf(w, http.StatusText(http.StatusOK)) } -- cgit v1.2.3-70-g09d2