-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
96 lines (85 loc) · 2.39 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"embed"
"html/template"
"net/http"
"strings"
"sync"
"time"
"github.com/StevenKGER/s85.berlin/internal"
"github.com/kataras/i18n"
"github.com/sirupsen/logrus"
)
type TemplateDetails struct {
Status internal.DepartureStatus
DetailText template.HTML
OriginalDetailText template.HTML
Time string
}
var (
lock = sync.RWMutex{}
information = &internal.DepartureInformation{
Status: internal.NO_INFORMATION,
Time: time.Now(),
StatusMessages: map[string][]string{},
}
//go:embed index.html
indexFS embed.FS
indexTemplate = template.Must(template.New("index.html").Funcs(template.FuncMap{"t": i18n.Tr}).ParseFS(indexFS, "*"))
//go:embed i18n/*
i18nFS embed.FS
translate *i18n.I18n
)
func main() {
loader, err := i18n.FS(i18nFS, "./i18n/*.json")
if err != nil {
internal.Log.Fatalln("Error while loading i18n:", err)
}
translate, err = i18n.New(loader, "en", "de")
if err != nil {
internal.Log.Fatalln("Error while loading i18n:", err)
}
go func() {
for {
lock.Lock()
information = internal.CrawlInformationAboutDeparture()
lock.Unlock()
time.Sleep(1 * time.Minute)
}
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
locale := translate.GetLocale(r)
language := locale.Tag().String()
var originalDetail string
lock.RLock()
detail := strings.Join(information.StatusMessages[language], "<br><br>")
if language != "de" {
originalDetail = strings.Join(information.StatusMessages["de"], "<br><br>")
if len(detail) == 0 && len(originalDetail) > 0 { // maybe the translation didn't work, so we fallback
detail = originalDetail
originalDetail = ""
}
}
data := TemplateDetails{
Status: information.Status,
DetailText: template.HTML(detail),
OriginalDetailText: template.HTML(originalDetail),
Time: information.Time.Format("2006-01-02 15:04:05"),
}
indexTemplate.Funcs(template.FuncMap{
"t": locale.GetMessage,
})
err = indexTemplate.Execute(w, data)
lock.RUnlock()
if err != nil {
internal.Log.Errorln("Error while writing a response using the index template", err)
}
internal.Log.WithFields(logrus.Fields{
"uri": r.RequestURI,
"method": r.Method,
"header": r.Header,
"ip": r.RemoteAddr,
}).Info("request completed")
})
panic(http.ListenAndServe(":4269", nil))
}