1
0
Fork 0
mirror of https://github.com/Luzifer/staticmap.git synced 2024-12-20 04:41:18 +00:00

Move parameters into struct form

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-06-17 15:19:28 +02:00
parent 9a7a0ec7c9
commit aa33543af8
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
4 changed files with 55 additions and 47 deletions

View file

@ -1,22 +1,7 @@
package main
import (
"crypto/sha256"
"fmt"
"io"
"strings"
"github.com/golang/geo/s2"
)
type cacheFunction func(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) (io.ReadCloser, error)
func cacheKeyHelper(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) string {
markerString := []string{}
for _, m := range marker {
markerString = append(markerString, m.String())
}
hashString := fmt.Sprintf("%s|%d|%s|%dx%d|%v", center.String(), zoom, strings.Join(markerString, "+"), x, y, disableAttribution)
return fmt.Sprintf("%x", sha256.Sum256([]byte(hashString)))
}
type cacheFunction func(generateMapConfig) (io.ReadCloser, error)

View file

@ -7,12 +7,10 @@ import (
"os"
"path"
"time"
"github.com/golang/geo/s2"
)
func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) (io.ReadCloser, error) {
cacheKey := cacheKeyHelper(center, zoom, marker, x, y, disableAttribution)
func filesystemCache(opts generateMapConfig) (io.ReadCloser, error) {
cacheKey := opts.getCacheKey()
cacheFileName := path.Join(cfg.CacheDir, cacheKey[0:2], cacheKey+".png")
if info, err := os.Stat(cacheFileName); err == nil && info.ModTime().Add(cfg.ForceCache).After(time.Now()) {
@ -20,7 +18,7 @@ func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int, disa
}
// No cache hit, generate a new map
mapReader, err := generateMap(center, zoom, marker, x, y, disableAttribution)
mapReader, err := generateMap(opts)
if err != nil {
return nil, err
}

36
main.go
View file

@ -65,36 +65,34 @@ func main() {
func handleMapRequest(res http.ResponseWriter, r *http.Request) {
var (
center *s2.LatLng
disableAttribution bool = r.URL.Query().Get("no-attribution") == "true"
err error
mapReader io.ReadCloser
markers []marker
x, y int
zoom int
err error
mapReader io.ReadCloser
opts = generateMapConfig{
DisableAttribution: r.URL.Query().Get("no-attribution") == "true",
}
)
if center, err = parseCoordinate(r.URL.Query().Get("center")); err != nil {
if opts.Center, err = parseCoordinate(r.URL.Query().Get("center")); err != nil {
http.Error(res, fmt.Sprintf("Unable to parse 'center' parameter: %s", err), http.StatusBadRequest)
return
}
if zoom, err = strconv.Atoi(r.URL.Query().Get("zoom")); err != nil {
if opts.Zoom, err = strconv.Atoi(r.URL.Query().Get("zoom")); err != nil {
http.Error(res, fmt.Sprintf("Unable to parse 'zoom' parameter: %s", err), http.StatusBadRequest)
return
}
if x, y, err = parseSize(r.URL.Query().Get("size"), true); err != nil {
if opts.Width, opts.Height, err = parseSize(r.URL.Query().Get("size"), true); err != nil {
http.Error(res, fmt.Sprintf("Unable to parse 'size' parameter: %s", err), http.StatusBadRequest)
return
}
if markers, err = parseMarkerLocations(r.URL.Query()["markers"]); err != nil {
if opts.Markers, err = parseMarkerLocations(r.URL.Query()["markers"]); err != nil {
http.Error(res, fmt.Sprintf("Unable to parse 'markers' parameter: %s", err), http.StatusBadRequest)
return
}
if mapReader, err = cacheFunc(*center, zoom, markers, x, y, disableAttribution); err != nil {
if mapReader, err = cacheFunc(opts); err != nil {
log.Errorf("Map render failed: %s (Request: %s)", err, r.URL.String())
http.Error(res, fmt.Sprintf("I experienced difficulties rendering your map: %s", err), http.StatusInternalServerError)
return
@ -106,14 +104,14 @@ func handleMapRequest(res http.ResponseWriter, r *http.Request) {
io.Copy(res, mapReader)
}
func parseCoordinate(coord string) (*s2.LatLng, error) {
func parseCoordinate(coord string) (s2.LatLng, error) {
if coord == "" {
return nil, errors.New("No coordinate given")
return s2.LatLng{}, errors.New("No coordinate given")
}
parts := strings.Split(coord, ",")
if len(parts) != 2 {
return nil, errors.New("Coordinate not in format lat,lon")
return s2.LatLng{}, errors.New("Coordinate not in format lat,lon")
}
var (
@ -122,15 +120,15 @@ func parseCoordinate(coord string) (*s2.LatLng, error) {
)
if lat, err = strconv.ParseFloat(parts[0], 64); err != nil {
return nil, errors.New("Latitude not parseable as float")
return s2.LatLng{}, errors.New("Latitude not parseable as float")
}
if lon, err = strconv.ParseFloat(parts[1], 64); err != nil {
return nil, errors.New("Longitude not parseable as float")
return s2.LatLng{}, errors.New("Longitude not parseable as float")
}
pt := s2.LatLngFromDegrees(lat, lon)
return &pt, nil
return pt, nil
}
func parseSize(size string, validate bool) (x, y int, err error) {
@ -203,7 +201,7 @@ func parseMarkerLocations(markers []string) ([]marker, error) {
return nil, fmt.Errorf("Unparsable chunk found in marker: %q", p)
}
result = append(result, marker{
pos: *pos,
pos: pos,
color: col,
size: size,
})

41
map.go
View file

@ -2,9 +2,11 @@ package main
import (
"bytes"
"crypto/sha256"
"fmt"
"image/color"
"io"
"strings"
staticMap "github.com/Luzifer/go-staticmaps"
"github.com/fogleman/gg"
@ -43,20 +45,45 @@ func (m marker) String() string {
return fmt.Sprintf("%s|%.0f|%d,%d,%d,%d", m.pos.String(), m.size, r, g, b, a)
}
func generateMap(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) (io.Reader, error) {
type generateMapConfig struct {
Center s2.LatLng
Zoom int
Markers []marker
Width int
Height int
DisableAttribution bool
}
func (g generateMapConfig) getCacheKey() string {
markerString := []string{}
for _, m := range g.Markers {
markerString = append(markerString, m.String())
}
hashString := fmt.Sprintf("%s|%d|%s|%dx%d|%v",
g.Center.String(),
g.Zoom,
strings.Join(markerString, "+"),
g.Width,
g.Height,
g.DisableAttribution)
return fmt.Sprintf("%x", sha256.Sum256([]byte(hashString)))
}
func generateMap(opts generateMapConfig) (io.Reader, error) {
ctx := staticMap.NewContext()
ctx.SetUserAgent(fmt.Sprintf("Mozilla/5.0+(compatible; staticmap/%s; https://github.com/Luzifer/staticmap)", version))
ctx.SetSize(x, y)
ctx.SetCenter(center)
ctx.SetZoom(zoom)
ctx.SetSize(opts.Width, opts.Height)
ctx.SetCenter(opts.Center)
ctx.SetZoom(opts.Zoom)
if disableAttribution {
if opts.DisableAttribution {
ctx.OverrideAttribution("")
}
if marker != nil {
for _, m := range marker {
if opts.Markers != nil {
for _, m := range opts.Markers {
ctx.AddMarker(staticMap.NewMarker(m.pos, m.color, float64(m.size)))
}
}