Documentation
¶
Overview ¶
`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.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 (Note the way we are embedding the HTML and .pmtiles database as an embed.FS instance)
import (
"embed"
"github.com/sfomuseum/go-http-protomaps"
"log"
"net/http"
"net/url"
)
//go:embed index.html sfo.pmtiles
var staticFS embed.FS
func main() {
tile_url := "/sfo.pmtiles"
static_fs := http.FS(staticFS)
static_handler := http.FileServer(static_fs)
mux := http.NewServeMux()
mux.Handle(*tile_url, static_handler)
protomaps.AppendAssetHandlers(mux)
pm_opts := protomaps.DefaultProtomapsOptions()
pm_opts.TileURL = *tile_url
index_handler := protomaps.AppendResourcesHandler(static_handler, pm_opts)
mux.Handle("/", index_handler)
err = http.ListenAndServe(":8080", mux)
}
Index ¶
- Variables
- func AppendAssetHandlers(mux *http.ServeMux) error
- func AppendAssetHandlersWithPrefix(mux *http.ServeMux, prefix string) error
- func AppendResourcesHandler(next http.Handler, opts *ProtomapsOptions) http.Handler
- func AppendResourcesHandlerWithPrefix(next http.Handler, opts *ProtomapsOptions, prefix string) http.Handler
- func AssetsHandler() (http.Handler, error)
- func AssetsHandlerWithPrefix(prefix string) (http.Handler, error)
- func FileHandlerFromPath(path string, prefix string) (string, http.Handler, error)
- type ProtomapsOptions
Constants ¶
This section is empty.
Variables ¶
var APPEND_LEAFLET_ASSETS = true
By default the go-http-protomaps package will also include and reference Leaflet.js assets using the aaronland/go-http-leaflet package. If you want or need to disable this behaviour set this variable to false.
var APPEND_LEAFLET_RESOURCES = true
By default the go-http-protomaps package will also include and reference Leaflet.js resources using the aaronland/go-http-leaflet package. If you want or need to disable this behaviour set this variable to false.
Functions ¶
func AppendAssetHandlers ¶
Append all the files in the net/http FS instance containing the embedded Protomaps assets to an *http.ServeMux instance.
func AppendAssetHandlersWithPrefix ¶
Append all the files in the net/http FS instance containing the embedded Protomaps assets to an *http.ServeMux instance ensuring that all URLs are prepended with prefix.
func AppendResourcesHandler ¶
func AppendResourcesHandler(next http.Handler, opts *ProtomapsOptions) http.Handler
AppendResourcesHandler will rewrite any HTML produced by previous handler to include the necessary markup to load Protomaps JavaScript files and related assets.
func AppendResourcesHandlerWithPrefix ¶
func AppendResourcesHandlerWithPrefix(next http.Handler, opts *ProtomapsOptions, prefix string) http.Handler
AppendResourcesHandlerWithPrefix will rewrite any HTML produced by previous handler to include the necessary markup to load Protomaps JavaScript files and related assets ensuring that all URIs are prepended with a prefix.
func AssetsHandler ¶
AssetsHandler returns a net/http FS instance containing the embedded Protomaps assets that are included with this package.
func AssetsHandlerWithPrefix ¶
AssetsHandler returns a net/http FS instance containing the embedded Protomaps assets that are included with this package ensuring that all URLs are stripped of prefix.
func FileHandlerFromPath ¶ added in v0.0.3
FileHandlerFromPath will take a path and create a http.FileServer handler instance for the files in its root directory. The handler is returned with a relative URI for the filename in 'path' to be assigned to a net/http ServeMux instance.
Types ¶
type ProtomapsOptions ¶
type ProtomapsOptions struct {
// A list of relative JavaScript files to reference in one or more <script> tags
JS []string
// A list of relative CSS files to reference in one or more <link rel="stylesheet"> tags
CSS []string
// A URL for a specific PMTiles database to include as a 'data-protomaps-tile-url' attribute on the <body> tag.
TileURL string
// A leaflet.LeafletOptions struct
LeafletOptions *leaflet.LeafletOptions
}
ProtomapsOptions provides a list of JavaScript and CSS link to include with HTML output as well as a URL referencing a specific Protomaps PMTiles database to include a data attribute.
func DefaultProtomapsOptions ¶
func DefaultProtomapsOptions() *ProtomapsOptions
Return a *ProtomapsOptions struct with default paths and URIs.
