Skip to content

Commit

Permalink
use constants for all flags; support plain old leaflet tiles; add /da…
Browse files Browse the repository at this point in the history
…ta endpoint
  • Loading branch information
thisisaaronland committed Mar 23, 2021
1 parent f34107c commit d4474e4
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 61 deletions.
26 changes: 17 additions & 9 deletions flags/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@ package flags

import (
"flag"
"fmt"
"github.com/aaronland/go-http-tangramjs"
)

func AppendWWWFlags(fs *flag.FlagSet) error {

fs.String("server-uri", "http://localhost:8080", "A valid aaronland/go-http-server URI.")
fs.String(SERVER_URI, "http://localhost:8080", "A valid aaronland/go-http-server URI.")

fs.Bool("enable-geojson", false, "Allow users to request GeoJSON FeatureCollection formatted responses. This is automatically enabled if the -enable-www flag is set.")
desc_geojson := fmt.Sprintf("Allow users to request GeoJSON FeatureCollection formatted responses. This is automatically enabled if the -%s flag is set.", ENABLE_WWW)
fs.Bool(ENABLE_GEOJSON, false, desc_geojson)

fs.Bool("enable-www", false, "Enable the interactive /debug endpoint to query points and display results.")
fs.Bool(ENABLE_WWW, false, "Enable the interactive /debug endpoint to query points and display results.")

fs.String(PATH_PREFIX, "", "Prepend this prefix to all assets (but not HTTP handlers). This is mostly for API Gateway integrations.")

fs.String(PATH_API, "/api", "The root URL for all API handlers")
fs.String(PATH_PING, "/health/ping", "The URL for the ping (health check) handler")
fs.String(PATH_PIP, "/point-in-polygon", "The URL for the point in polygon web handler")
fs.String(PATH_DATA, "/data", "The URL for data (GeoJSON) handler")

fs.String("nextzen-apikey", "", "A valid Nextzen API key")
fs.String("nextzen-style-url", "/tangram/refill-style.zip", "...")
fs.String("nextzen-tile-url", tangramjs.NEXTZEN_MVT_ENDPOINT, "...")
leaflet_desc := fmt.Sprintf("A valid Leaflet (slippy map) tile template URL to use for rendering maps (if -%s is false)", ENABLE_TANGRAM)
fs.String(LEAFLET_TILE_URL, "", leaflet_desc)

fs.Float64("initial-latitude", 37.616906, "...")
fs.Float64("initial-longitude", -122.386665, "...")
fs.Int("initial-zoom", 14, "...")
fs.Bool(ENABLE_TANGRAM, false, "Use Tangram.js for rendering map tiles")

fs.String(NEXTZEN_APIKEY, "", "A valid Nextzen API key")
fs.String(NEXTZEN_STYLE_URL, "/tangram/refill-style.zip", "...")
fs.String(NEXTZEN_TILE_URL, tangramjs.NEXTZEN_MVT_ENDPOINT, "...")

fs.Float64(INITIAL_LATITUDE, 37.616906, "...")
fs.Float64(INITIAL_LONGITUDE, -122.386665, "...")
fs.Int(INITIAL_ZOOM, 14, "...")

return nil
}
18 changes: 18 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,21 @@ const PATH_PREFIX string = "path-prefix"
const PATH_API = "path-root-api"
const PATH_PING string = "path-ping"
const PATH_PIP string = "path-pip"
const PATH_DATA string = "path-data"

const ENABLE_WWW string = "enable-www"
const ENABLE_GEOJSON string = "enable-geojson"

const ENABLE_TANGRAM string = "enable-tangram"

const NEXTZEN_APIKEY string = "nextzen-apikey"
const NEXTZEN_STYLE_URL string = "nextzen-style-url"
const NEXTZEN_TILE_URL string = "nextzen-tile-url"

const LEAFLET_TILE_URL string = "leaflet-tile-url"

const INITIAL_LATITUDE string = "initial-latitude"
const INITIAL_LONGITUDE string = "initial-longitude"
const INITIAL_ZOOM string = "initial-zoom"

const SERVER_URI string = "server-uri"
55 changes: 48 additions & 7 deletions flags/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func ValidateWWWFlags(fs *flag.FlagSet) error {

enable_www, err := lookup.BoolVar(fs, "enable-www")
enable_www, err := lookup.BoolVar(fs, ENABLE_WWW)

if err != nil {
return err
Expand All @@ -20,12 +20,12 @@ func ValidateWWWFlags(fs *flag.FlagSet) error {
return nil
}

log.Println("-enable-www flag is true causing the following flags to also be true: -enable-data -enable-candidates -enable-properties")
log.Printf("-%s flag is true causing the following flags to also be true: -%s\n", ENABLE_WWW, ENABLE_GEOJSON)

fs.Set("enable-geojson", "true")
fs.Set("enable-properties", "true")
fs.Set(ENABLE_GEOJSON, "true")
// fs.Set(ENABLE_PROPERTIES, "true")

init_lat, err := lookup.Float64Var(fs, "initial-latitude")
init_lat, err := lookup.Float64Var(fs, INITIAL_LATITUDE)

if err != nil {
return err
Expand All @@ -35,7 +35,7 @@ func ValidateWWWFlags(fs *flag.FlagSet) error {
return errors.New("Invalid latitude")
}

init_lon, err := lookup.Float64Var(fs, "initial-longitude")
init_lon, err := lookup.Float64Var(fs, INITIAL_LONGITUDE)

if err != nil {
return err
Expand All @@ -45,7 +45,7 @@ func ValidateWWWFlags(fs *flag.FlagSet) error {
return errors.New("Invalid longitude")
}

init_zoom, err := lookup.IntVar(fs, "initial-zoom")
init_zoom, err := lookup.IntVar(fs, INITIAL_ZOOM)

if err != nil {
return err
Expand All @@ -58,6 +58,7 @@ func ValidateWWWFlags(fs *flag.FlagSet) error {
path_flags := []string{
PATH_PREFIX,
PATH_API,
PATH_DATA,
PATH_PING,
PATH_PIP,
}
Expand All @@ -71,5 +72,45 @@ func ValidateWWWFlags(fs *flag.FlagSet) error {
}
}

enable_tangram, err := lookup.BoolVar(fs, ENABLE_TANGRAM)

if err != nil {
return err
}

if enable_tangram {

nz_keys := []string{
NEXTZEN_APIKEY,
NEXTZEN_STYLE_URL,
NEXTZEN_TILE_URL,
}

for _, k := range nz_keys {

v, err := lookup.StringVar(fs, k)

if err != nil {
return err
}

if v == "" {
log.Printf("-%s flag is empty, this will probably result in unexpected behaviour\n", k)
}
}

} else {

v, err := lookup.StringVar(fs, LEAFLET_TILE_URL)

if err != nil {
return err
}

if v == "" {
log.Printf("-%s flag is empty, this will probably result in unexpected behaviour\n", LEAFLET_TILE_URL)
}
}

return nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ require (
github.com/aaronland/go-http-tangramjs v0.0.9
github.com/rs/cors v1.7.0
github.com/sfomuseum/go-flags v0.8.1
github.com/whosonfirst/go-reader v0.5.0
github.com/whosonfirst/go-whosonfirst-placetypes v0.3.0
github.com/whosonfirst/go-whosonfirst-spatial v0.0.46
github.com/whosonfirst/go-whosonfirst-spatial-pip v0.0.5
github.com/whosonfirst/go-whosonfirst-spatial-rtree v0.0.9
github.com/whosonfirst/go-whosonfirst-uri v0.2.0
)
54 changes: 54 additions & 0 deletions http/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package http

// TBD: make this part of whosonfirst/go-reader package...

import (
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-uri"
"io"
gohttp "net/http"
)

func NewDataHandler(r reader.Reader) (gohttp.Handler, error) {

fn := func(rsp gohttp.ResponseWriter, req *gohttp.Request) {

path := req.URL.Path

id, uri_args, err := uri.ParseURI(path)

if err != nil {
gohttp.Error(rsp, err.Error(), gohttp.StatusBadRequest)
return
}

rel_path, err := uri.Id2RelPath(id, uri_args)

if err != nil {
gohttp.Error(rsp, err.Error(), gohttp.StatusBadRequest)
return
}

ctx := req.Context()
fh, err := r.Read(ctx, rel_path)

if err != nil {
gohttp.Error(rsp, err.Error(), gohttp.StatusBadRequest)
return
}

rsp.Header().Set("Content-Type", "application/json")

_, err = io.Copy(rsp, fh)

if err != nil {
gohttp.Error(rsp, err.Error(), gohttp.StatusBadRequest)
return
}

return
}

h := gohttp.HandlerFunc(fn)
return h, nil
}
3 changes: 3 additions & 0 deletions http/pip.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ type PointInPolygonHandlerOptions struct {
InitialLongitude float64
InitialZoom int
DataEndpoint string
LeafletTileURL string
}

type PointInPolygonHandlerTemplateVars struct {
InitialLatitude float64
InitialLongitude float64
InitialZoom int
DataEndpoint string
LeafletTileURL string
Placetypes []*placetypes.WOFPlacetype
}

Expand Down Expand Up @@ -56,6 +58,7 @@ func PointInPolygonHandler(spatial_app *app.SpatialApplication, opts *PointInPol
InitialLatitude: opts.InitialLatitude,
InitialLongitude: opts.InitialLongitude,
InitialZoom: opts.InitialZoom,
LeafletTileURL: opts.LeafletTileURL,
DataEndpoint: opts.DataEndpoint,
Placetypes: pt_list,
}
Expand Down
Loading

0 comments on commit d4474e4

Please sign in to comment.