-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
executable file
·117 lines (101 loc) · 2.31 KB
/
api.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package livesplit_race_manager
import (
"embed"
"fmt"
"github.com/Masterminds/sprig/v3"
"github.com/gin-contrib/logger"
"github.com/gin-gonic/gin"
"github.com/golang/protobuf/ptypes/duration"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
"html/template"
"io/fs"
"net/http"
"strings"
)
var (
//go:embed html/*.gohtml
htmlTemplates embed.FS
)
type NewAPIOpts struct {
LSM *LiveSplitManager
Address string
}
func NewAPI(opts NewAPIOpts) error {
if opts.LSM == nil {
return errors.New("LSM can't be nil")
}
if opts.Address == "" {
return errors.New("isten address can't be empty")
}
SetGinLogMode()
r := gin.New()
r.Use(logger.SetLogger(), gin.Recovery())
t, err := loadTemplates()
if err != nil {
panic(err)
}
r.SetHTMLTemplate(t)
r.GET("/", func(c *gin.Context) {
liveSplits := opts.LSM.LiveSplits()
slices.SortFunc(liveSplits, func(i, j *LiveSplit) bool {
return strings.ToLower(i.ID) < strings.ToLower(j.ID)
})
c.HTML(http.StatusOK, "html/status.gohtml", liveSplits)
})
r.GET("/api/v1/livesplits", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"livesplits": opts.LSM.LiveSplits(),
})
})
log.Info().Str("listen", opts.Address).Msg("Start serving HTTP API")
err = r.Run(opts.Address)
if err != nil {
return err
}
return nil
}
func loadTemplates() (*template.Template, error) {
t := template.New("").Funcs(sprig.FuncMap()).Funcs(template.FuncMap{
"durationToString": func(duration *duration.Duration) string {
if duration == nil {
return "-"
}
hours := duration.Seconds / 3600
minutes := (duration.Seconds / 60) % 60
seconds := duration.Seconds % 60
ms := duration.Nanos / 1000000
return fmt.Sprintf("%02d:%02d:%02d.%03d", hours, minutes, seconds, ms)
},
})
err := fs.WalkDir(htmlTemplates, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
h, err := fs.ReadFile(htmlTemplates, path)
if err != nil {
return err
}
t, err = t.New(path).Parse(string(h))
if err != nil {
return err
}
return nil
})
return t, err
}
func SetGinLogMode() {
var mode string
switch zerolog.GlobalLevel() {
case zerolog.DebugLevel:
mode = gin.DebugMode
default:
mode = gin.ReleaseMode
}
gin.SetMode(mode)
}