Documentation ¶
Overview ¶
`go-http-tangramjs` is an HTTP middleware package for including Tangram.js assets in web applications. It exports two principal methods:
* `tangramjs.AppendAssetHandlers(*http.ServeMux)` which is used to append HTTP handlers to a `http.ServeMux` instance for serving Tangramjs JavaScript files, and related assets. * `tangramjs.AppendResourcesHandler(http.Handler, *TangramJSOptions)` which is used to rewrite any HTML produced by previous handler to include the necessary markup to load Tangramjs JavaScript files and related assets.
Example
import ( "embed" "github.com/aaronland/go-http-tangramjs" "html/template" "log" "net/http" ) //go:embed *.html var FS embed.FS func ExampleHandler(templates *template.Template) (http.Handler, error) { t := templates.Lookup("example") fn := func(rsp http.ResponseWriter, req *http.Request) { err := t.Execute(rsp, nil) return } return http.HandlerFunc(fn), nil } func main() { api_key := "****" style_url := "/tangram/refill-style.zip" t, _ := template.ParseFS(FS, "*.html") mux := http.NewServeMux() tangramjs.AppendAssetHandlers(mux) map_handler, _:= ExampleHandler(t) tangramjs_opts := tangramjs.DefaultTangramJSOptions() tangramjs_opts.NextzenOptions.APIKey = api_key tangramjs_opts.NextzenOptions.StyleURL = style_url map_handler = tangramjs.AppendResourcesHandler(map_handler, tangramjs_opts) mux.Handle("/", map_handler) endpoint := "localhost:8080" log.Printf("Listening for requests on %s\n", endpoint) http.ListenAndServe(endpoint, mux) }
Index ¶
- Constants
- Variables
- func AppendAssetHandlers(mux *http.ServeMux) error
- func AppendAssetHandlersWithPrefix(mux *http.ServeMux, prefix string) error
- func AppendResourcesHandler(next http.Handler, opts *TangramJSOptions) http.Handler
- func AppendResourcesHandlerWithPrefix(next http.Handler, opts *TangramJSOptions, prefix string) http.Handler
- func AssetsHandler() (http.Handler, error)
- func AssetsHandlerWithPrefix(prefix string) (http.Handler, error)
- type NextzenOptions
- type TangramJSOptions
Constants ¶
const NEXTZEN_MVT_ENDPOINT string = "https://tile.nextzen.org/tilezen/vector/v1/512/all/{z}/{x}/{y}.mvt"
NEXTZEN_MVT_ENDPOINT is the default endpoint for Nextzen vector tiles
Variables ¶
var APPEND_LEAFLET_ASSETS = true
By default the go-http-tangramjs 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-tangramjs 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 Tangram.js assets to an *http.ServeMux instance.
func AppendAssetHandlersWithPrefix ¶
Append all the files in the net/http FS instance containing the embedded Tangram.js assets to an *http.ServeMux instance ensuring that all URLs are prepended with prefix.
func AppendResourcesHandler ¶
func AppendResourcesHandler(next http.Handler, opts *TangramJSOptions) http.Handler
AppendResourcesHandler will rewrite any HTML produced by previous handler to include the necessary markup to load Tangram.js files and related assets.
func AppendResourcesHandlerWithPrefix ¶
func AppendResourcesHandlerWithPrefix(next http.Handler, opts *TangramJSOptions, prefix string) http.Handler
AppendResourcesHandlerWithPrefix will rewrite any HTML produced by previous handler to include the necessary markup to load Tangram.js 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 Tangram.js assets that are included with this package.
Types ¶
type NextzenOptions ¶
type NextzenOptions struct { // A valid Nextzen developer API key APIKey string // The URL for a valid Tangram.js style. StyleURL string // The URL template to use for fetching Nextzen map tiles. TileURL string }
NextzenOptions provides configuration variables for Nextzen map tiles.
func DefaultNextzenOptions ¶
func DefaultNextzenOptions() *NextzenOptions
Return a *NextzenOptions struct with default values.
type TangramJSOptions ¶
type TangramJSOptions struct { // A list of Tangram.js Javascript files to append to HTML resources. JS []string // A list of Tangram.js CSS files to append to HTML resources. CSS []string // A NextzenOptions instance. NextzenOptions *NextzenOptions // A leaflet.LeafletOptions instance. LeafletOptions *leaflet.LeafletOptions }
TangramJSOptions provides a list of JavaScript and CSS link to include with HTML output as well as options for Nextzen tiles and Leaflet.js.
func DefaultTangramJSOptions ¶
func DefaultTangramJSOptions() *TangramJSOptions
Return a *TangramJSOptions struct with default values.