feat: Pass around one big global context
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2024-02-03 12:05:10 +01:00
parent cb123537d5
commit 315bb39f44
5 changed files with 183 additions and 44 deletions

View File

@@ -1,74 +0,0 @@
package pages
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
type LokiMetricConfig struct {
Url string
Enabled bool
}
// Checks if we should send a metric ping to Loki based on the served path.
func (c *LokiMetricConfig) shouldSendMetrics(path string) bool {
return strings.HasSuffix(path, ".html") && c.Enabled
}
func (c *LokiMetricConfig) sendMetricPing(domain, path string) {
data := map[string]interface{}{
"streams": []map[string]interface{}{
{
"stream": map[string]string{
// Labels
"service": "rio",
"domain": domain,
"type": "metric",
},
"values": [][]interface{}{
{
strconv.Itoa(int(time.Now().UnixNano())),
"path=" + path,
},
},
},
},
}
jsonData, err := json.Marshal(data)
if err != nil {
log.Errorf("Failed to send metric ping to Loki: %v", err)
return
}
log.Debugf("Sending payload %s", string(jsonData))
// Send the ping to the Loki server
go func() {
res, err := http.Post(
c.Url,
"application/json",
strings.NewReader(string(jsonData)),
)
if err != nil {
log.Errorf("Failed to send payload to Loki: %v", err)
return
}
defer res.Body.Close()
if res.StatusCode != 204 {
log.Errorf("Loki returned non-204 status code %d", res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Warnf("Failed to read body. No more specific error message")
return
}
log.Errorf("-> %s", body)
}
}()
}

View File

@@ -8,6 +8,7 @@ import (
"time"
"git.polynom.me/rio/internal/constants"
"git.polynom.me/rio/internal/context"
"git.polynom.me/rio/internal/repo"
"github.com/patrickmn/go-cache"
@@ -45,13 +46,14 @@ func addHeaders(csp, contentType string, contentLength int, w http.ResponseWrite
}
}
func ServeFile(username, reponame, path, defaultCsp, domain string, giteaClient *repo.GiteaClient, metricConfig *LokiMetricConfig, w http.ResponseWriter) {
func ServeFile(context *context.Context) {
// Strip away a starting / as it messes with Gitea
path := context.Path
if path[:1] == "/" {
path = path[1:]
}
key := makePageContentCacheEntry(username, path)
key := makePageContentCacheEntry(context.Username, path)
entry, found := pageCache.Get(key)
var content []byte
var mimeType string
@@ -65,25 +67,25 @@ func ServeFile(username, reponame, path, defaultCsp, domain string, giteaClient
since = &sinceRaw
}
content, changed, err := giteaClient.GetFile(
username,
reponame,
content, changed, err := context.Global.Gitea.GetFile(
context.Username,
context.Reponame,
constants.PagesBranch,
path,
since,
)
csp := repo.GetCSPForRepository(username, reponame, "", giteaClient)
csp := repo.GetCSPForRepository(context.Username, context.Reponame, "", context.Global.Gitea)
if err != nil {
if !found {
log.Errorf("Failed to get file %s/%s/%s (%s)", username, reponame, path, err)
addHeaders(csp, "text/html", 0, w)
w.WriteHeader(404)
log.Errorf("Failed to get file %s/%s/%s (%s)", context.Username, context.Reponame, path, err)
addHeaders(csp, "text/html", 0, context.Writer)
context.Writer.WriteHeader(404)
} else {
log.Debugf("Request failed but page %s is cached in memory", path)
addHeaders(csp, mimeType, len(content), w)
w.WriteHeader(200)
w.Write(content)
addHeaders(csp, mimeType, len(content), context.Writer)
context.Writer.WriteHeader(200)
context.Writer.Write(content)
}
return
@@ -91,9 +93,9 @@ func ServeFile(username, reponame, path, defaultCsp, domain string, giteaClient
if found && !changed {
log.Debugf("Page %s is unchanged and cached in memory", path)
addHeaders(csp, mimeType, len(content), w)
w.WriteHeader(200)
w.Write(content)
addHeaders(csp, mimeType, len(content), context.Writer)
context.Writer.WriteHeader(200)
context.Writer.Write(content)
return
}
@@ -113,12 +115,12 @@ func ServeFile(username, reponame, path, defaultCsp, domain string, giteaClient
)
log.Debugf("Page %s requested from Gitea and cached in memory at %v", path, now)
addHeaders(csp, mimeType, len(content), w)
w.WriteHeader(200)
w.Write(content)
addHeaders(csp, mimeType, len(content), context.Writer)
context.Writer.WriteHeader(200)
context.Writer.Write(content)
// Tell Loki about if, if desired
if metricConfig.shouldSendMetrics(path) {
metricConfig.sendMetricPing(domain, path)
if context.Global.MetricConfig.ShouldSendMetrics(path, context.UserAgent) {
context.Global.MetricConfig.SendMetricPing(context.Domain, path, context.Referrer)
}
}