-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
159 lines (137 loc) · 4.14 KB
/
server.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package server
import (
"embed"
"encoding/base64"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"sort"
"strconv"
"strings"
"text/template"
"time"
"github.com/chelmertz/elly/internal/points"
"github.com/chelmertz/elly/internal/storage"
"github.com/chelmertz/elly/internal/types"
)
func check(err error) {
if err != nil {
panic(err)
}
}
type IndexHtmlData struct {
Prs []types.ViewPr
PointsPerPrUrl map[string]*points.Points
CurrentUser string
RefreshUrl string
LastRefreshed string
RefreshIntervalMinutes int
Version string
}
//go:embed index.html
var index embed.FS
func ServeWeb(url, username string, store *storage.Storage, refreshingChannel chan types.RefreshAction, timeoutMinutes int, version string, logger *slog.Logger) {
temp, err := template.ParseFS(index, "index.html")
check(err)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
storedPrs := store.Prs()
prs_ := storedPrs.Prs
if r.Method == http.MethodPost {
wo := strings.TrimPrefix(r.URL.Path, "/api/v0/prs/")
parts := strings.Split(wo, "/")
if len(parts) == 2 && (parts[1] == "bury" || parts[1] == "unbury") {
prUrlBytes, err := base64.StdEncoding.DecodeString(parts[0])
if err != nil {
w.Write([]byte("invalid PR ID"))
w.WriteHeader(http.StatusBadRequest)
return
}
var buryFunc func(string) error
if parts[1] == "bury" {
buryFunc = store.Bury
} else {
buryFunc = store.Unbury
}
ghPrUrl := string(prUrlBytes)
if err := buryFunc(ghPrUrl); err != nil {
w.Write([]byte(fmt.Sprintf("couldn't toggle bury for PR %s", ghPrUrl)))
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
return
}
}
pointsPerPrUrl := make(map[string]*points.Points)
for _, pr := range prs_ {
pointsPerPrUrl[pr.Url] = points.StandardPrPoints(pr, username)
}
sort.Slice(prs_, func(i, j int) bool {
pri := pointsPerPrUrl[prs_[i].Url].Total
prj := pointsPerPrUrl[prs_[j].Url].Total
if pri == prj {
lastUpdated := prs_[j].LastUpdated.Before(prs_[i].LastUpdated)
return lastUpdated
}
return pri > prj
})
data := IndexHtmlData{
Prs: prs_,
PointsPerPrUrl: pointsPerPrUrl,
CurrentUser: username,
LastRefreshed: storedPrs.LastFetched.Format(time.RFC3339),
RefreshIntervalMinutes: timeoutMinutes,
Version: version,
}
err := temp.Execute(w, data)
check(err)
})
// Let's say that v0 represents "may change at any time", read the code.
// Should be bumped before tagging this repo as v1
http.HandleFunc("/api/v0/prs", func(w http.ResponseWriter, r *http.Request) {
storedPrs := store.Prs().Prs
prsToReturn := make([]types.ViewPr, 0)
minimumPoints := -999
if minPoints := r.URL.Query().Get("minPoints"); minPoints != "" {
if min, err := strconv.Atoi(minPoints); err == nil && min >= -999 && min <= 999 {
minimumPoints = min
}
}
pointsPerPrUrl := make(map[string]*points.Points)
for _, pr := range storedPrs {
points := points.StandardPrPoints(pr, username)
pointsPerPrUrl[pr.Url] = points
}
for _, pr := range storedPrs {
points := pointsPerPrUrl[pr.Url]
if points.Total >= minimumPoints {
prsToReturn = append(prsToReturn, pr)
}
}
sort.Slice(prsToReturn, func(i, j int) bool {
pri := pointsPerPrUrl[storedPrs[i].Url].Total
prj := pointsPerPrUrl[storedPrs[j].Url].Total
if pri == prj {
lastUpdated := storedPrs[j].LastUpdated.Before(storedPrs[i].LastUpdated)
return lastUpdated
}
return pri > prj
})
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(prsToReturn)
check(err)
})
http.HandleFunc("/api/v0/prs/refresh", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
refreshingChannel <- types.RefreshManual
} else if r.Method == http.MethodGet {
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
})
logger.Info("starting web server at", slog.String("url", "http://"+url))
err = http.ListenAndServe(url, nil)
check(err)
}