forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.go
72 lines (66 loc) · 2.2 KB
/
geo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"context"
"embed"
"encoding/json"
"fmt"
"strings"
gogeouri "git.jlel.se/jlelse/go-geouri"
"github.com/carlmjohnson/requests"
geojson "github.com/paulmach/go.geojson"
"github.com/samber/lo"
"go.goblog.app/app/pkgs/bufferpool"
)
func (a *goBlog) geoTitle(g *gogeouri.Geo, lang string) string {
if name, ok := g.Parameters["name"]; ok && len(name) > 0 && name[0] != "" {
return name[0]
}
fc, err := a.photonReverse(g.Latitude, g.Longitude, lang)
if err != nil || len(fc.Features) < 1 {
return ""
}
f := fc.Features[0]
return strings.Join(lo.Filter([]string{
f.PropertyMustString("name", ""), f.PropertyMustString("city", ""), f.PropertyMustString("state", ""), f.PropertyMustString("country", ""),
}, func(s string, _ int) bool { return s != "" }), ", ")
}
func (a *goBlog) photonReverse(lat, lon float64, lang string) (*geojson.FeatureCollection, error) {
// Only allow one concurrent request
a.photonMutex.Lock()
defer a.photonMutex.Unlock()
// Create feature collection
fc := geojson.NewFeatureCollection()
// Check cache
cacheKey := fmt.Sprintf("photon-%v-%v-%v", lat, lon, lang)
if cache, _ := a.db.retrievePersistentCache(cacheKey); cache != nil {
// Cache hit, unmarshal and return
if err := json.Unmarshal(cache, fc); err != nil {
return nil, err
}
return fc, nil
}
// No cache, fetch from Photon
buf := bufferpool.Get()
defer bufferpool.Put(buf)
// Create request
rb := requests.URL("https://photon.komoot.io/reverse").Client(a.httpClient).UserAgent(appUserAgent).ToBytesBuffer(buf)
// Set parameters
rb.Param("lat", fmt.Sprintf("%v", lat)).Param("lon", fmt.Sprintf("%v", lon))
rb.Param("lang", lo.If(lang == "de" || lang == "fr" || lang == "it", lang).Else("en")) // Photon only supports en, de, fr, it
// Do request
if err := rb.Fetch(context.Background()); err != nil {
return nil, err
}
// Cache response
_ = a.db.cachePersistently(cacheKey, buf.Bytes())
// Unmarshal response
if err := json.NewDecoder(buf).Decode(fc); err != nil {
return nil, err
}
return fc, nil
}
func geoOSMLink(g *gogeouri.Geo) string {
return fmt.Sprintf("https://www.openstreetmap.org/?mlat=%v&mlon=%v", g.Latitude, g.Longitude)
}
//go:embed leaflet/*
var leafletFiles embed.FS