Skip to content

Commit

Permalink
Update to use sfomuseum/go-http-rollup (#4)
Browse files Browse the repository at this point in the history
* snapshot: start wiring in JS at EOF hooks; doesn't work yet

* working through bugs

* update to use aaronland/go-http-static v0.0.3, aaronland/go-http-leaflet v0.3.0; add support for appending JS resource at end of HTML

* update to support RollupAssets flags and other changes to go-http-leaflet

* add rollup stuff; break everything

* snapshot: workable code with rollups

* update to not do rollups if there is only a single JS/CSS file

* update to use aaronland/go-http-leaflet v0.4.0; update docs
  • Loading branch information
thisisaaronland committed Mar 14, 2023
1 parent 3c7dba3 commit 226ba9b
Show file tree
Hide file tree
Showing 67 changed files with 18,839 additions and 71 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
GOMOD=readonly

cli:
go build -mod vendor -o bin/example cmd/example/main.go
go build -mod $(GOMOD) -o bin/example cmd/example/main.go

debug:
go run -mod vendor cmd/example/main.go -javascript-at-eof
go run -mod $(GOMOD) cmd/example/main.go -javascript-at-eof -rollup-assets

protomaps-js:
curl -s -L -o static/javascript/protomaps.js https://unpkg.com/protomaps@latest/dist/protomaps.js
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

`go-http-protomaps` is an HTTP middleware package for including Protomaps.js assets in web applications. It exports two principal methods:

* `protomaps.AppendAssetHandlers(*http.ServeMux)` which is used to append HTTP handlers to a `http.ServeMux` instance for serving Protomaps JavaScript files, and related assets.
* `protomaps.AppendAssetHandlers(*http.ServeMux, *ProtomapsOptions)` which is used to append HTTP handlers to a `http.ServeMux` instance for serving Protomaps JavaScript files, and related assets.
* `protomaps.AppendResourcesHandler(http.Handler, *ProtomapsOptions)` which is used to rewrite any HTML produced by previous handler to include the necessary markup to load Protomaps JavaScript files and related assets.

## Example
Expand All @@ -20,10 +20,11 @@ package main
import (
"embed"
"github.com/sfomuseum/go-http-protomaps"
"log"
"net/http"
"net/url"
"github.com/sfomuseum/go-http-protomaps"
)
//go:embed index.html sfo.pmtiles
Expand All @@ -39,12 +40,12 @@ func main() {
mux := http.NewServeMux()
mux.Handle(*tile_url, static_handler)
protomaps.AppendAssetHandlers(mux)
pm_opts := protomaps.DefaultProtomapsOptions()
pm_opts.TileURL = *tile_url
protomaps.AppendAssetHandlers(mux, pm_opts)
index_handler := protomaps.AppendResourcesHandler(static_handler, pm_opts)
mux.Handle("/", index_handler)
Expand Down
41 changes: 25 additions & 16 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,59 @@ func main() {

js_eof := flag.Bool("javascript-at-eof", false, "Append JavaScript resources to end of HTML file.")

rollup_assets := flag.Bool("rollup-assets", false, "Rollup (minify and bundle) JavaScript and CSS assets.")

flag.Parse()

ctx := context.Background()

logger := log.Default()

static_fs := http.FS(staticFS)
static_handler := http.FileServer(static_fs)

index_handler := static_handler

mux := http.NewServeMux()

pm_opts := protomaps.DefaultProtomapsOptions()
pm_opts.TileURL = *tile_url

pm_opts.LeafletOptions.EnableHash()
pm_opts.AppendJavaScriptAtEOF = *js_eof
pm_opts.RollupAssets = *rollup_assets
pm_opts.Logger = logger

if !*append_leaflet {

protomaps.APPEND_LEAFLET_RESOURCES = false
protomaps.APPEND_LEAFLET_ASSETS = false
pm_opts.AppendLeafletResources = false
pm_opts.AppendLeafletAssets = false

leaflet_opts := leaflet.DefaultLeafletOptions()
leaflet_opts.AppendJavaScriptAtEOF = *js_eof
leaflet_opts.RollupAssets = *rollup_assets

leaflet_opts.EnableHash()

index_handler = leaflet.AppendResourcesHandler(index_handler, leaflet_opts)

err := leaflet.AppendAssetHandlers(mux)
err := leaflet.AppendAssetHandlers(mux, leaflet_opts)

if err != nil {
log.Fatalf("Failed to append Leaflet asset handlers, %v", err)
logger.Fatalf("Failed to append Leaflet asset handlers, %v", err)
}
}

err := protomaps.AppendAssetHandlers(mux)
err := protomaps.AppendAssetHandlers(mux, pm_opts)

if err != nil {
log.Fatalf("Failed to append leaflet-protomaps asset handler, %v", err)
logger.Fatalf("Failed to append leaflet-protomaps asset handler, %v", err)
}

u, err := url.Parse(*tile_url)

if err != nil {
log.Fatalf("Failed to parse '%s', %v", tile_url, err)
logger.Fatalf("Failed to parse '%s', %v", tile_url, err)
}

switch u.Scheme {
Expand All @@ -75,7 +90,7 @@ func main() {
mux_url, mux_handler, err := protomaps.FileHandlerFromPath(u.Path, "")

if err != nil {
log.Fatalf("Failed to determine absolute path for '%s', %v", *tile_url, err)
logger.Fatalf("Failed to determine absolute path for '%s', %v", *tile_url, err)
}

mux.Handle(mux_url, mux_handler)
Expand All @@ -88,27 +103,21 @@ func main() {
log.Fatalf("Invalid or unsupported scheme")
}

pm_opts := protomaps.DefaultProtomapsOptions()
pm_opts.TileURL = *tile_url

pm_opts.LeafletOptions.EnableHash()
pm_opts.AppendJavaScriptAtEOF = *js_eof

index_handler = protomaps.AppendResourcesHandler(index_handler, pm_opts)
mux.Handle("/", index_handler)

s, err := server.NewServer(ctx, *server_uri)

if err != nil {
log.Fatalf("Failed to start server '%s', %v", *server_uri, err)
logger.Fatalf("Failed to start server '%s', %v", *server_uri, err)
}

log.Printf("Listening for requests on %s\n", s.Address())

err = s.ListenAndServe(ctx, mux)

if err != nil {
log.Fatalf("Failed to start server, %v", err)
logger.Fatalf("Failed to start server, %v", err)
}

}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ module github.com/sfomuseum/go-http-protomaps
go 1.18

require (
github.com/aaronland/go-http-leaflet v0.3.0
github.com/aaronland/go-http-leaflet v0.4.0
github.com/aaronland/go-http-server v1.0.1
github.com/aaronland/go-http-static v0.0.3
github.com/sfomuseum/go-http-rollup v0.0.2
)

require (
github.com/aaronland/go-http-rewrite v1.1.0 // indirect
github.com/aaronland/go-log/v2 v2.0.0 // indirect
github.com/aaronland/go-roster v1.0.0 // indirect
github.com/akrylysov/algnhsa v1.0.0 // indirect
github.com/aws/aws-lambda-go v1.37.0 // indirect
github.com/tdewolff/minify/v2 v2.12.4 // indirect
github.com/tdewolff/parse/v2 v2.6.4 // indirect
golang.org/x/net v0.8.0 // indirect
)
21 changes: 19 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
github.com/aaronland/go-http-leaflet v0.3.0 h1:d88rMERw+m+CiwGTPgWL9LoPDpJH9eBH8r+np9pOxLY=
github.com/aaronland/go-http-leaflet v0.3.0/go.mod h1:5tlCL3eWYRQBpzW7ZxRB7ocwWaW832qTRXkzw/3r+Jo=
github.com/aaronland/go-http-leaflet v0.4.0 h1:1aUH60aINVtRl0C2k/zLlYIngFOFchKYXlif9CxS6Ys=
github.com/aaronland/go-http-leaflet v0.4.0/go.mod h1:wdo1JQvSZ3SvTnanwY1vmaRp4Nt7zynAom0MWXtTP6A=
github.com/aaronland/go-http-rewrite v1.1.0 h1:HhsltNyYRnIz2FR+qANZLx2ykiRuNuNK1JgPonKBLHQ=
github.com/aaronland/go-http-rewrite v1.1.0/go.mod h1:iA9jQBureJrzO6fTzmRLS+fkbAI1Fna71iTb1hP8n3k=
github.com/aaronland/go-http-server v1.0.1 h1:ULzPl1cinglgIDd1fRV02WFRUrG4Sv1LW06rGasLHqA=
github.com/aaronland/go-http-server v1.0.1/go.mod h1:sg3+O9NJREN2YTChO5e2sdjmuhFJKTH39u+HV5V8Bpo=
github.com/aaronland/go-http-static v0.0.3 h1:0MnYXNwGVDCzre5YwinfPSdAFHInbBbmBv5hJtb/FX8=
github.com/aaronland/go-http-static v0.0.3/go.mod h1:/s6lHGXq95ZR9nMJmE9slE4uMi92GP34CuG3f9xlfsY=
github.com/aaronland/go-log/v2 v2.0.0 h1:lMoaVDHd4Etaz2+ibze3ReqvHHG68z2SpuxYXv12WlE=
github.com/aaronland/go-log/v2 v2.0.0/go.mod h1:jOLBCaHVyOC7DQpbVFFQSiuU8CA8MRMrqA1mMSJQtLs=
github.com/aaronland/go-roster v1.0.0 h1:FRDGrTqsYySKjWnAhbBGXyeGlI/o5/t9FZYCbUmyQtI=
github.com/aaronland/go-roster v1.0.0/go.mod h1:KIsYZgrJlAsyb9LsXSCvlqvbcCBVjCSqcQiZx42i9ro=
github.com/akrylysov/algnhsa v1.0.0 h1:qlogYL9n7MfU/TJJJCKqpg6gLgCuR/IkdFGwIJClBnE=
github.com/akrylysov/algnhsa v1.0.0/go.mod h1:ConzNpk7uLAl7Hi5LqcImgl3Oq2flRe6W7zum5A1p/8=
github.com/aws/aws-lambda-go v1.37.0 h1:WXkQ/xhIcXZZ2P5ZBEw+bbAKeCEcb5NtiYpSwVVzIXg=
github.com/aws/aws-lambda-go v1.37.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/djherbis/atime v1.1.0/go.mod h1:28OF6Y8s3NQWwacXc5eZTsEsiMzp7LF8MbXE+XJPdBE=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/sfomuseum/go-http-rollup v0.0.2 h1:cc0p0pppG1VJc6o3IUdkbocPnJedRzOqxWFNlz2mkYM=
github.com/sfomuseum/go-http-rollup v0.0.2/go.mod h1:kxVTXkkaUsxVAMq3lFSNoILzKkv1L4ETuaQiDCQcBfI=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/tdewolff/minify/v2 v2.12.4 h1:kejsHQMM17n6/gwdw53qsi6lg0TGddZADVyQOz1KMdE=
github.com/tdewolff/minify/v2 v2.12.4/go.mod h1:h+SRvSIX3kwgwTFOpSckvSxgax3uy8kZTSF1Ojrr3bk=
github.com/tdewolff/parse/v2 v2.6.4 h1:KCkDvNUMof10e3QExio9OPZJT8SbdKojLBumw8YZycQ=
github.com/tdewolff/parse/v2 v2.6.4/go.mod h1:woz0cgbLwFdtbjJu8PIKxhW05KplTFQkOdX78o+Jgrs=
github.com/tdewolff/test v1.0.7 h1:8Vs0142DmPFW/bQeHRP3MV19m1gvndjUb1sn8yy74LM=
github.com/tdewolff/test v1.0.7/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Loading

0 comments on commit 226ba9b

Please sign in to comment.