nextzenjs

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2019 License: BSD-3-Clause Imports: 15 Imported by: 1

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 and link elements to the head element.
  • Append a data-nextzen-api-key attribute (and value) to the body 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

See also

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendAssetHandlers added in v0.1.1

func AppendAssetHandlers(mux *http.ServeMux) error

func AppendAssetHandlersWithPrefix added in v0.1.2

func AppendAssetHandlersWithPrefix(mux *http.ServeMux, prefix string) error

func AppendResourcesHandler added in v0.1.2

func AppendResourcesHandler(next http.Handler, opts *NextzenJSOptions) http.Handler

func AppendResourcesHandlerWithPrefix added in v0.1.2

func AppendResourcesHandlerWithPrefix(next http.Handler, opts *NextzenJSOptions, prefix string) http.Handler

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func NextzenJSAssetsHandler

func NextzenJSAssetsHandler() (http.Handler, error)

func NextzenJSAssetsHandlerWithPrefix added in v0.1.3

func NextzenJSAssetsHandlerWithPrefix(prefix string) (http.Handler, error)

func NextzenJSHandler

func NextzenJSHandler(next http.Handler, opts *NextzenJSOptions) (http.Handler, error)

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

Types

type NextzenJSOptions

type NextzenJSOptions struct {
	AppendAPIKey bool
	AppendJS     bool
	AppendCSS    bool
	APIKey       string
	JS           []string
	CSS          []string
}

func DefaultNextzenJSOptions

func DefaultNextzenJSOptions() *NextzenJSOptions

Directories

Path Synopsis
cmd
map

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL