Skip to content

Peace-and-Passion/go-http-mapzenjs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-mapzen-js

Go middleware package for mapzen.js

Install

You will need to have both Go (specifically a version of Go more recent than 1.6 so let's just assume you need Go 1.8 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

MapzenJSHandler(http.Handler, MapzenJSOptions) (http.Handler, error)

This handler will optionally modify the output of the your_handler http.Handler as follows:

  • Append the relevant mapzen.js script and link elements to the head element.
  • Append a data-mapzen-api-key attribute (and value) to the body element.
import (
	"github.com/whosonfirst/go-http-mapzenjs"
	"net/http"
)

func main(){

	opts := mapzenjs.DefaultMapzenJSOptions()
	opts.APIKey = "mapzen-1a2b3c"

	www_handler := YourDefaultWWWHandler()
	
	mapzenjs_handler, _ := mapzenjs.MapzenJSHandler(www_handler, opts)

	mux := http.NewServeMux()
	mux.Handle("/", mapzenjs_handler)

Note that error handling has been removed for the sake of brevity.

MapzenJSOptions

The definition for MapzenJSOptions looks like this:

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

Default MapzenJSOptions are:

	opts := MapzenJSOptions{
		AppendAPIKey: true,
		AppendJS:     true,
		AppendCSS:    true,
		APIKey:       "mapzen-xxxxxx",
		JS:           []string{"/javascript/mapzen.min.js"},
		CSS:          []string{"/css/mapzen.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 MapzenJSHandler 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/mapzen.min.js"></script>
	  <link rel="stylesheet" type="text/css" href="/css/mapzen.js.css" />
  </head>
  <body data-mapzen-api-key="mapzen-1a2b3c">
  <!-- and so on... ->

MapzenJSAssetsHandler() (http.Handler, error)

The handler will serve mapzen.js and tangram.js related assets which have been bundled with this package.

import (
	"github.com/whosonfirst/go-http-mapzenjs"
	"net/http"
)

func main(){

	mapzenjs_assets_handler, _ := mapzen.MapzenJSAssetsHandler()

	mux := http.NewServeMux()

	mux.Handle("/javascript/mapzen.js", mapzenjs_handler)
	mux.Handle("/javascript/mapzen.min.js", mapzenjs_handler)
	mux.Handle("/javascript/tangram.js", mapzenjs_handler)	
	mux.Handle("/javascript/tangram.min.js", mapzenjs_handler)
	mux.Handle("/css/mapzen.js.css", mapzenjs_handler)
	mux.Handle("/tangram/refill-style.zip", mapzenjs_handler)

}

You can update the various mapzen.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:

To do

  • Add a tile caching proxy

See also

About

Go middleware package for mapzen.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 99.9%
  • Other 0.1%