hswaw/site: load leaflet from NPM package
Instead of manually packaging leaflet.js into the Git repository, this
uses an http_archive to download it on demand, and augments the static
serving code to accept different regexes as paths so that the
http_archive's contents can be served directly.
Change-Id: Icb8d624fea855fb748f107471133ac8adb5f2776
diff --git a/hswaw/site/main.go b/hswaw/site/main.go
index 9510a3d..a7f3e54 100644
--- a/hswaw/site/main.go
+++ b/hswaw/site/main.go
@@ -5,6 +5,7 @@
"fmt"
"mime"
"net/http"
+ "regexp"
"strings"
"sync"
@@ -55,19 +56,48 @@
<-mi.Done()
}
+var (
+ // staticRoutes define the resolution of static file paths into assets
+ // built into //hswaw/site/static, whose names correspond to their origin
+ // within the Bazel workspace.
+ // The regexp will be matched against the normalized within the URL. If it
+ // matches, its first group/submatch will be used to format the string
+ // corresponding to this regexp, and that string will be then used to
+ // retrieve a path embedded within //hswaw/site/static:static
+ // (go_embed_data).
+ //
+ // To see paths available within that go_embed_data, you can do:
+ // $ bazel build //hswaw/site/static:static
+ // $ grep -A100 'Data =' bazel-bin/hswaw/site/static/static.go
+ staticRoutes = map[*regexp.Regexp]string{
+ regexp.MustCompile(`^static/site/(.+)$`): "hswaw/site/static/%s",
+ regexp.MustCompile(`^static/leaflet/(.+)$`): "external/com_npmjs_leaflet/package/dist/%s",
+ }
+)
+
+// handleHTTPStatic uses staticRoutes to serve static files embedded within
+// //hswaw/site/static.
func (s *service) handleHTTPStatic(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
-
- staticPath := fmt.Sprintf("hswaw/site/%s", path)
- if data, ok := static.Data[staticPath]; ok {
+ for from, to := range staticRoutes {
+ match := from.FindStringSubmatch(path)
+ if match == nil {
+ continue
+ }
+ to := fmt.Sprintf(to, match[1])
+ data, ok := static.Data[to]
+ if !ok {
+ continue
+ }
parts := strings.Split(path, ".")
ext := fmt.Sprintf(".%s", parts[len(parts)-1])
t := mime.TypeByExtension(ext)
w.Header().Set("Content-Type", t)
w.Write(data)
- } else {
- http.NotFound(w, r)
+ return
+
}
+ http.NotFound(w, r)
}
func (s *service) registerHTTP(mux *http.ServeMux) {