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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package handlers
import (
"fmt"
"log/slog"
"slices"
"strconv"
"strings"
"github.com/SayaAndy/saya-today-web/internal/blog"
"github.com/SayaAndy/saya-today-web/internal/router"
"github.com/gofiber/fiber/v2"
)
type MapHandler struct {
router.BasicHandler
}
func init() {
router.Routes = append(router.Routes, &MapHandler{})
}
func (r *MapHandler) Filter() (method string, path string) {
return "GET", "/:lang/map"
}
func (r *MapHandler) IsTemplated() bool {
return false
}
func (r *MapHandler) TemplatesToInject() []string {
return []string{"views/pages/global-map.html"}
}
func (r *MapHandler) ToCache() router.CacheSetting {
return router.Disabled
}
func (r *MapHandler) ToValidateLang() router.LangSetting {
return router.InPath
}
func (r *MapHandler) SitemapInfo(supplements *router.Supplements) []router.SitemapInfo {
sitemapInfo := []router.SitemapInfo{}
for _, lang := range supplements.AvailableLanguages {
sitemapInfo = append(sitemapInfo, router.SitemapInfo{Loc: "/" + lang.Name + "/map", Priority: 0.3})
}
return sitemapInfo
}
func (r *MapHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
pages, err := supplements.BlogClient.Scan(lang + "/")
slices.SortFunc(pages, func(a *blog.Page, b *blog.Page) int {
return a.Metadata.PublishedTime.Compare(b.Metadata.PublishedTime)
})
status := fiber.StatusOK
if err != nil {
slog.Error("received an error while scanning b2 pages",
slog.String("error", err.Error()),
slog.String("lang", lang),
)
status = fiber.StatusPartialContent
pages = []*blog.Page{}
}
type MapMarker struct {
Index int `json:"Index"`
Title string `json:"Title"`
PageLink string `json:"PageLink"`
Lat float64 `json:"Lat"`
Long float64 `json:"Long"`
AccuracyMeters int64 `json:"AccuracyMeters"`
Thumbnail string `json:"Thumbnail"`
}
mapMarkers := make([]*MapMarker, 0, len(pages))
for i, page := range pages {
geolocationParts := strings.Split(page.Metadata.Geolocation, " ")
if len(geolocationParts) < 2 {
continue
}
var x, y float64
var areaError int64
if len(geolocationParts) >= 2 {
x, _ = strconv.ParseFloat(geolocationParts[0], 64)
y, _ = strconv.ParseFloat(geolocationParts[1], 64)
}
if len(geolocationParts) >= 3 {
areaError, _ = strconv.ParseInt(geolocationParts[2], 10, 64)
}
mapMarkers = append(mapMarkers, &MapMarker{
Index: i,
Title: page.Metadata.Title,
PageLink: fmt.Sprintf("/%s/blog/%s", lang, page.FileName),
Lat: x,
Long: y,
AccuracyMeters: areaError,
Thumbnail: page.Metadata.Thumbnail,
})
}
templateMap["MapMarkers"] = mapMarkers
templateMap["MapLocationLat"] = 45.4507
templateMap["MapLocationLong"] = 68.8319
return status, nil
}
|