tempestas

A REST API for processing sensor.community data
git clone https://git.bracken.jp/tempestas.git
Log | Files | Refs | README | LICENSE

commit d5827970ba6882c8db6c34cdebc0501d2b74ebba
parent cee7b3fef2eedf5a58a519bf168b67b490a377d0
Author: Chris Bracken <chris@bracken.jp>
Date:   Mon, 11 Apr 2022 16:49:07 -0700

Inject static resource dir to HttpServer

This refactors the static resource directory containing HTML, CSS, and
JS assets for the server to a field on the HttpServer struct. This
allows us to make this directory configurable via an option, and to
separate default location probing from the server itself.

Diffstat:
Mhttp/http.go | 14++++++--------
Mmain.go | 2+-
2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/http/http.go b/http/http.go @@ -14,17 +14,15 @@ import ( "goji.io/pat" ) -const ( - staticResourceDir = "static" -) - type HttpServer struct { - ds storage.DataStore + ds storage.DataStore + resourceDir string } -func CreateServer(ds storage.DataStore) *HttpServer { +func CreateServer(ds storage.DataStore, resourceDir string) *HttpServer { s := new(HttpServer) s.ds = ds + s.resourceDir = resourceDir return s } @@ -37,7 +35,7 @@ func (s HttpServer) ListenAndServe(addr string) error { mux.HandleFunc(pat.Get("/sensor/sensors/"), s.QuerySensors) // UI endpoints. - fs := http.FileServer(http.Dir(staticResourceDir)) + fs := http.FileServer(http.Dir(s.resourceDir)) mux.Handle(pat.Get("/static/*"), http.StripPrefix("/static", fs)) mux.HandleFunc(pat.Get("/dashboard/"), s.PresentDashboard) @@ -104,7 +102,7 @@ func (s HttpServer) QuerySensors(w http.ResponseWriter, r *http.Request) { } func (s HttpServer) PresentDashboard(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, filepath.Join(staticResourceDir, "dashboard.html")) + http.ServeFile(w, r, filepath.Join(s.resourceDir, "dashboard.html")) } func errorResponse(w http.ResponseWriter, message string, httpStatusCode int) { diff --git a/main.go b/main.go @@ -23,6 +23,6 @@ func main() { addr = ":8080" } fmt.Println("Waiting for requests at " + addr) - s := http.CreateServer(ds) + s := http.CreateServer(ds, "static") log.Fatal(s.ListenAndServe(addr)) }