# README
go-nextzen-js
Go middleware package for nextzen.js
Install
You will need to have both Go
(version 1.12 or higher) and the make
programs installed on your computer. Assuming you do just type:
make bin
All of this package's dependencies are bundled with the code in the vendor
directory.
Handlers
NextzenJSHandler(http.Handler, NextzenJSOptions) (http.Handler, error)
This handler will optionally modify the output of the your_handler http.Handler
as follows:
- Append the relevant nextzen.js
script
andlink
elements to thehead
element. - Append a
data-nextzen-api-key
attribute (and value) to thebody
element.
import (
"github.com/whosonfirst/go-http-nextzenjs"
"net/http"
)
func main(){
opts := nextzenjs.DefaultNextzenJSOptions()
opts.APIKey = "nextzen-1a2b3c"
www_handler := YourDefaultWWWHandler()
nextzenjs_handler, _ := nextzenjs.NextzenJSHandler(www_handler, opts)
mux := http.NewServeMux()
mux.Handle("/", nextzenjs_handler)
Note that error handling has been removed for the sake of brevity.
NextzenJSOptions
The definition for NextzenJSOptions
looks like this:
type NextzenJSOptions struct {
AppendAPIKey bool
AppendJS bool
AppendCSS bool
APIKey string
JS []string
CSS []string
}
Default NextzenJSOptions
are:
opts := NextzenJSOptions{
AppendAPIKey: true,
AppendJS: true,
AppendCSS: true,
APIKey: "nextzen-xxxxxx",
JS: []string{"/javascript/nextzen.min.js"},
CSS: []string{"/css/nextzen.js.css"},
}
Example
Given the following markup generated by your http.Handler
output:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="referrer" content="origin">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="HandheldFriendly" content="true" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
</head>
<body>
<!-- and so on... ->
The NextzenJSHandler
handler will modify that markup to return:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="referrer" content="origin">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="HandheldFriendly" content="true" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<script type="text/javascript" src="/javascript/nextzen.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/nextzen.js.css" />
</head>
<body data-nextzen-api-key="nextzen-1a2b3c">
<!-- and so on... ->
NextzenJSAssetsHandler() (http.Handler, error)
The handler will serve nextzen.js and tangram.js related assets which have been bundled with this package.
import (
"github.com/whosonfirst/go-http-nextzenjs"
"net/http"
)
func main(){
nextzenjs_assets_handler, _ := nextzen.NextzenJSAssetsHandler()
mux := http.NewServeMux()
mux.Handle("/javascript/nextzen.js", nextzenjs_handler)
mux.Handle("/javascript/nextzen.min.js", nextzenjs_handler)
mux.Handle("/javascript/tangram.js", nextzenjs_handler)
mux.Handle("/javascript/tangram.min.js", nextzenjs_handler)
mux.Handle("/css/nextzen.js.css", nextzenjs_handler)
mux.Handle("/tangram/refill-style.zip", nextzenjs_handler)
}
You can update the various nextzen.js
and tangram.js
assets manually by invoking the build
target in the included Makefile.
Styles
Currently the following styles are bundled with this package:
Example
For a complete example, have a look at cmd/map/main.go which run like this:
go run cmd/map/main.go -api-key {NEXTZEN_API_KEY}
Produces this:
To do
- Add a tile caching proxy