1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// vim: shiftwidth=4 tabstop=4 noexpandtab
package main
import (
"fmt"
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
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 NewWebServer(cfg *webConfig) WebServer {
server := WebServer{}
server.config = cfg
return server
}
func (server *WebServer) run() {
router := chi.NewRouter()
router.Use(middleware.Logger)
router.Get("/", indexHandler)
router.Get("/css/birdscan.css", cssHandler)
router.Get("/js/birdscan.js", jsHandler)
router.Post("/api/state", stateHandler)
router.Post("/api/single_picture", singlePictureHandler)
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) {
http.ServeFile(w, r, fmt.Sprintf("%s/html/index.html", APP_DATA))
}
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 stateHandler(w http.ResponseWriter, r *http.Request) {
known_state := r.FormValue("known_state")
w.Write([]byte(fmt.Sprintf("%s", camera.GetStateOnChange(known_state))))
}
func singlePictureHandler(w http.ResponseWriter, r *http.Request) {
camera.SendEvent("take_single_picture")
fmt.Fprintf(w, http.StatusText(http.StatusOK))
}
|