forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoTiles.go
82 lines (76 loc) · 2.01 KB
/
geoTiles.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
73
74
75
76
77
78
79
80
81
82
package main
import (
"io"
"net/http"
"strings"
"github.com/go-chi/chi/v5"
)
func (a *goBlog) proxyTiles() http.HandlerFunc {
tileSource := "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
if c := a.cfg.MapTiles; c != nil && c.Source != "" {
tileSource = c.Source
}
return func(w http.ResponseWriter, r *http.Request) {
// Create a new request to proxy to the tile server
targetUrl := tileSource
targetUrl = strings.ReplaceAll(targetUrl, "{s}", chi.URLParam(r, "s"))
targetUrl = strings.ReplaceAll(targetUrl, "{z}", chi.URLParam(r, "z"))
targetUrl = strings.ReplaceAll(targetUrl, "{x}", chi.URLParam(r, "x"))
targetUrl = strings.ReplaceAll(targetUrl, "{y}", chi.URLParam(r, "y"))
proxyRequest, _ := http.NewRequestWithContext(r.Context(), http.MethodGet, targetUrl, nil)
proxyRequest.Header.Set(userAgent, appUserAgent)
// Copy request headers
for _, k := range []string{
"Accept-Encoding",
"Accept-Language",
"Accept",
cacheControl,
"If-Modified-Since",
"If-None-Match",
"User-Agent",
} {
proxyRequest.Header.Set(k, r.Header.Get(k))
}
// Do the request
res, err := a.httpClient.Do(proxyRequest)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
// Copy result headers
for _, k := range []string{
"Accept-Ranges",
"Access-Control-Allow-Origin",
"Age",
cacheControl,
"Content-Length",
"Content-Type",
"Etag",
"Expires",
} {
w.Header().Set(k, res.Header.Get(k))
}
// Copy result
w.WriteHeader(res.StatusCode)
_, _ = io.Copy(w, res.Body)
_ = res.Body.Close()
}
}
func (a *goBlog) getMinZoom() int {
if c := a.cfg.MapTiles; c != nil {
return c.MinZoom
}
return 0
}
func (a *goBlog) getMaxZoom() int {
if c := a.cfg.MapTiles; c != nil && c.MaxZoom > 0 {
return c.MaxZoom
}
return 20
}
func (a *goBlog) getMapAttribution() string {
if c := a.cfg.MapTiles; c != nil {
return c.Attribution
}
return `© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors`
}