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

@@ -7,13 +7,16 @@ import (
"net"
"net/http"
"os"
"regexp"
"strings"
"sync"
"time"
"git.polynom.me/rio/internal/acme"
"git.polynom.me/rio/internal/certificates"
"git.polynom.me/rio/internal/context"
"git.polynom.me/rio/internal/dns"
"git.polynom.me/rio/internal/metrics"
"git.polynom.me/rio/internal/pages"
"git.polynom.me/rio/internal/repo"
"git.polynom.me/rio/internal/server"
@@ -24,14 +27,14 @@ import (
"github.com/urfave/cli/v2"
)
func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, lokiConfig *pages.LokiMetricConfig, w http.ResponseWriter) {
func handleSubdomain(ctx *context.GlobalContext, domain, cname, path string, req *http.Request, w http.ResponseWriter) {
username := ""
if cname != "" {
// If we are accessed via a CNAME, then CNAME contains our <user>.<pages domain> value.
username = dns.ExtractUsername(pagesDomain, cname)
username = dns.ExtractUsername(ctx.PagesDomain, cname)
} else {
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
username = dns.ExtractUsername(pagesDomain, domain)
username = dns.ExtractUsername(ctx.PagesDomain, domain)
}
// Strip the leading /
@@ -52,7 +55,7 @@ func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp stri
domain,
cname,
path,
giteaClient,
ctx.Gitea,
)
if err != nil {
log.Errorf("Failed to get repo: %s", err)
@@ -60,15 +63,30 @@ func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp stri
return
}
pages.ServeFile(username, repo.Name, path, defaultCsp, domain, giteaClient, lokiConfig, w)
d := domain
if cname != "" {
d = cname
}
c := &context.Context{
Username: username,
Reponame: repo.Name,
Domain: d,
Path: path,
Referrer: req.Header.Get("Referer"),
UserAgent: req.Header.Get("User-Agent"),
Writer: w,
Global: ctx,
}
pages.ServeFile(c)
}
func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, lokiConfig *pages.LokiMetricConfig) http.HandlerFunc {
func Handler(ctx *context.GlobalContext) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Server", "rio")
// Is the direct domain requested?
if req.Host == pagesDomain {
if req.Host == ctx.PagesDomain {
log.Debug("Direct pages domain is requested.")
// TODO: Handle
@@ -77,9 +95,9 @@ func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaCl
}
// Is a direct subdomain requested?
if strings.HasSuffix(req.Host, pagesDomain) {
if strings.HasSuffix(req.Host, ctx.PagesDomain) {
log.Debug("Domain can be directly handled")
handleSubdomain(pagesDomain, req.Host, "", req.URL.Path, giteaUrl, defaultCsp, giteaClient, lokiConfig, w)
handleSubdomain(ctx, req.Host, "", req.URL.Path, req, w)
return
}
@@ -94,9 +112,9 @@ func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaCl
// Is a direct subdomain requested after CNAME lookup?
// NOTE: We now require the leading dot because a CNAME to the direct
// pages domain makes no sense.
if strings.HasSuffix(cname, "."+pagesDomain) {
if strings.HasSuffix(cname, "."+ctx.PagesDomain) {
log.Debugf("%s is alias of %s and can be handled after a CNAME query", req.Host, cname)
handleSubdomain(pagesDomain, req.Host, cname, req.URL.Path, giteaUrl, defaultCsp, giteaClient, lokiConfig, w)
handleSubdomain(ctx, req.Host, cname, req.URL.Path, req, w)
return
}
@@ -132,6 +150,7 @@ func runServer(ctx *cli.Context) error {
acmeDisable := ctx.Bool("acme-disable")
defaultCsp := ctx.String("default-csp")
lokiUrl := ctx.String("loki-url")
metricsBotList := ctx.String("metrics-bot-list")
// Init Logging
if ctx.Bool("debug") {
@@ -141,15 +160,24 @@ func runServer(ctx *cli.Context) error {
}
// Set up the Loki metrics
var lokiConfig pages.LokiMetricConfig
var lokiConfig metrics.LokiMetricConfig
if lokiUrl == "" {
lokiConfig = pages.LokiMetricConfig{
lokiConfig = metrics.LokiMetricConfig{
Enabled: false,
}
} else {
lokiConfig = pages.LokiMetricConfig{
Enabled: true,
Url: lokiUrl,
var patterns []regexp.Regexp
if metricsBotList != "" {
patterns, _ = metrics.ReadBotPatterns(metricsBotList)
} else {
patterns = make([]regexp.Regexp, 0)
}
log.Infof("Read %d bot patterns from disk", len(patterns))
lokiConfig = metrics.LokiMetricConfig{
Enabled: true,
BotUserAgents: &patterns,
Url: lokiUrl,
}
}
@@ -243,6 +271,13 @@ func runServer(ctx *cli.Context) error {
listener = tls.NewListener(listener, tlsConfig)
}
globalCtx := &context.GlobalContext{
DefaultCSP: defaultCsp,
PagesDomain: domain,
Gitea: &giteaClient,
MetricConfig: &lokiConfig,
}
var waitGroup sync.WaitGroup
servers := 2
if acmeDisable {
@@ -254,7 +289,7 @@ func runServer(ctx *cli.Context) error {
defer waitGroup.Done()
log.Debug("Listening on main HTTP server")
if err := http.Serve(listener, Handler(domain, giteaUrl, defaultCsp, &giteaClient, &lokiConfig)); err != nil {
if err := http.Serve(listener, Handler(globalCtx)); err != nil {
log.Fatal(fmt.Errorf("Listening failed: %v", err))
}
log.Debug("Listening on main HTTP server done!")
@@ -370,6 +405,12 @@ func main() {
Value: "",
EnvVars: []string{"LOKI_URL"},
},
&cli.StringFlag{
Name: "metrics-bot-list",
Usage: "File to read a list of regular expressions modelling bot user agents from",
Value: "",
EnvVars: []string{"METRICS_BOT_LIST"},
},
},
}