-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
123 lines (98 loc) · 2.2 KB
/
app.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
118
119
120
121
122
123
package handler
import (
"crypto/rand"
"encoding/hex"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/dxta-dev/app/internal/template"
"github.com/donseba/go-htmx"
)
type App struct {
HTMX *htmx.HTMX
BuildTimestamp string
DebugMode bool
Nonce string
State State
}
type State struct {
Team *int64
}
func (app *App) GenerateNonce() error {
nonce := make([]byte, 16)
_, err := rand.Read(nonce)
if err != nil {
return err
}
encodedNonce := hex.EncodeToString(nonce)
app.Nonce = encodedNonce
return nil
}
func (app *App) LoadState(r *http.Request) State {
var team *int64
if r.URL.Query().Has("team") {
value, err := strconv.ParseInt(r.URL.Query().Get("team"), 10, 64)
if err == nil {
team = &value
}
}
app.State.Team = team
return app.State
}
func GetUrl(currentUrl string, params url.Values) (string, error) {
if params == nil {
params = url.Values{}
}
parsedURL, err := url.Parse(currentUrl)
if err != nil {
return "", err
}
requestUri := parsedURL.Path
encodedParams := params.Encode()
if encodedParams != "" {
return fmt.Sprintf("%s?%s", requestUri, encodedParams), nil
}
return requestUri, nil
}
func (a *App) GetNavState() (template.NavState, error) {
params := url.Values{}
navState := template.GetDefaultNavState()
if a.State.Team == nil {
return navState, nil
}
if a.State.Team != nil {
params.Add("team", fmt.Sprint(*a.State.Team))
}
rootUrl, err := GetUrl(navState.Root, params)
if err != nil {
return template.NavState{}, err
}
qMetricsUrl, err := GetUrl(navState.Metrics.Quality, params)
if err != nil {
return template.NavState{}, err
}
tMetricsUrl, err := GetUrl(navState.Metrics.Throughput, params)
if err != nil {
return template.NavState{}, err
}
return template.NavState{
Root: rootUrl,
Metrics: struct {
Quality string
Throughput string
}{
Quality: qMetricsUrl,
Throughput: tMetricsUrl,
},
}, nil
}
func (app *App) GetUrlAppState(currentUrl string, params url.Values) (string, error) {
if params == nil {
params = url.Values{}
}
if app.State.Team != nil && !params.Has("team") {
params.Add("team", fmt.Sprint(*app.State.Team))
}
return GetUrl(currentUrl, params)
}