feat: Implement simple page metrics
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -24,7 +24,7 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, w http.ResponseWriter) {
|
||||
func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, lokiConfig *pages.LokiMetricConfig, w http.ResponseWriter) {
|
||||
username := ""
|
||||
if cname != "" {
|
||||
// If we are accessed via a CNAME, then CNAME contains our <user>.<pages domain> value.
|
||||
@@ -60,10 +60,10 @@ func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp stri
|
||||
return
|
||||
}
|
||||
|
||||
pages.ServeFile(username, repo.Name, path, defaultCsp, giteaClient, w)
|
||||
pages.ServeFile(username, repo.Name, path, defaultCsp, domain, giteaClient, lokiConfig, w)
|
||||
}
|
||||
|
||||
func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient) http.HandlerFunc {
|
||||
func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, lokiConfig *pages.LokiMetricConfig) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Server", "rio")
|
||||
|
||||
@@ -79,7 +79,7 @@ func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaCl
|
||||
// Is a direct subdomain requested?
|
||||
if strings.HasSuffix(req.Host, pagesDomain) {
|
||||
log.Debug("Domain can be directly handled")
|
||||
handleSubdomain(pagesDomain, req.Host, "", req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
||||
handleSubdomain(pagesDomain, req.Host, "", req.URL.Path, giteaUrl, defaultCsp, giteaClient, lokiConfig, w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaCl
|
||||
// pages domain makes no sense.
|
||||
if strings.HasSuffix(cname, "."+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, w)
|
||||
handleSubdomain(pagesDomain, req.Host, cname, req.URL.Path, giteaUrl, defaultCsp, giteaClient, lokiConfig, w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -131,6 +131,7 @@ func runServer(ctx *cli.Context) error {
|
||||
acmeDnsProvider := ctx.String("acme-dns-provider")
|
||||
acmeDisable := ctx.Bool("acme-disable")
|
||||
defaultCsp := ctx.String("default-csp")
|
||||
lokiUrl := ctx.String("loki-url")
|
||||
|
||||
// Init Logging
|
||||
if ctx.Bool("debug") {
|
||||
@@ -139,6 +140,19 @@ func runServer(ctx *cli.Context) error {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
|
||||
// Set up the Loki metrics
|
||||
var lokiConfig pages.LokiMetricConfig
|
||||
if lokiUrl == "" {
|
||||
lokiConfig = pages.LokiMetricConfig{
|
||||
Enabled: false,
|
||||
}
|
||||
} else {
|
||||
lokiConfig = pages.LokiMetricConfig{
|
||||
Enabled: true,
|
||||
Url: lokiUrl,
|
||||
}
|
||||
}
|
||||
|
||||
// Setup the Gitea stuff
|
||||
httpClient := http.Client{Timeout: 10 * time.Second}
|
||||
giteaApiClient, err := gitea.NewClient(
|
||||
@@ -240,7 +254,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)); err != nil {
|
||||
if err := http.Serve(listener, Handler(domain, giteaUrl, defaultCsp, &giteaClient, &lokiConfig)); err != nil {
|
||||
log.Fatal(fmt.Errorf("Listening failed: %v", err))
|
||||
}
|
||||
log.Debug("Listening on main HTTP server done!")
|
||||
@@ -350,6 +364,12 @@ func main() {
|
||||
Value: "",
|
||||
EnvVars: []string{"DEFAULT_CSP"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "loki-url",
|
||||
Usage: "The URL for Loki metric pings",
|
||||
Value: "",
|
||||
EnvVars: []string{"LOKI_URL"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user