From ac6a3df0d18cc73524c0096d954a57d24cad5669 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Sat, 30 Sep 2023 10:36:37 -0400 Subject: Xesite V4 (#723) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * scripts/ditherify: fix quoting Signed-off-by: Xe Iaso * clean up some old files Signed-off-by: Xe Iaso * import site into lume Signed-off-by: Xe Iaso * initial go code Signed-off-by: Xe Iaso * move vods index to top level Signed-off-by: Xe Iaso * remove the ads Signed-off-by: Xe Iaso * internal/lume: metrics Signed-off-by: Xe Iaso * delete old code Signed-off-by: Xe Iaso * load config into memory Signed-off-by: Xe Iaso * autogenerate data from dhall config Signed-off-by: Xe Iaso * various cleanups, import clackset logic Signed-off-by: Xe Iaso * Update signalboost.dhall (#722) Added myself, and also fixed someone’s typo * Add Connor Edwards to signal boost (#721) * add cache headers Signed-off-by: Xe Iaso * move command to xesite folder Signed-off-by: Xe Iaso * xesite: listen for GitHub webhook push events Signed-off-by: Xe Iaso * xesite: 5 minute timeout for rebuilding the site Signed-off-by: Xe Iaso * xesite: add rebuild metrics Signed-off-by: Xe Iaso * xesite: update default variables Signed-off-by: Xe Iaso * don't commit binaries oops lol Signed-off-by: Xe Iaso * lume: make search have a light background Signed-off-by: Xe Iaso * add a notfound page Signed-off-by: Xe Iaso * fetch info from patreon API Signed-off-by: Xe Iaso * create contact page Signed-off-by: Xe Iaso * Toot embedding Signed-off-by: Xe Iaso * attempt a docker image Signed-off-by: Xe Iaso * lume: fix deno lock Signed-off-by: Xe Iaso * add gokrazy post Signed-off-by: Xe Iaso * cmd/xesite: go up before trying to connect to the saas proxy Signed-off-by: Xe Iaso * blog: add Sine post/demo Signed-off-by: Xe Iaso --------- Signed-off-by: Xe Iaso Co-authored-by: bri <284789+b-@users.noreply.github.com> Co-authored-by: Connor Edwards <38229097+cedws@users.noreply.github.com> --- cmd/xesite/github.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 cmd/xesite/github.go (limited to 'cmd/xesite/github.go') diff --git a/cmd/xesite/github.go b/cmd/xesite/github.go new file mode 100644 index 0000000..bde7c9d --- /dev/null +++ b/cmd/xesite/github.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "encoding/json" + "expvar" + "fmt" + "log/slog" + "net/http" + "time" + + "github.com/go-git/go-git/v5" + "tailscale.com/metrics" + "xeiaso.net/v4/internal/github" + "xeiaso.net/v4/internal/lume" +) + +var ( + webhookCount = metrics.LabelMap{Label: "source"} + buildErrors = metrics.LabelMap{Label: "err"} +) + +func init() { + expvar.Publish("gauge_xesite_webhook_count", &webhookCount) + expvar.Publish("gauge_xesite_build_errors", &buildErrors) +} + +type GitHubWebhook struct { + fs *lume.FS +} + +func (gh *GitHubWebhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { + webhookCount.Add("github", 1) + + if r.Header.Get("X-GitHub-Event") != "push" { + slog.Info("not a push event", "event", r.Header.Get("X-GitHub-Event")) + } + + var event github.PushEvent + if err := json.NewDecoder(r.Body).Decode(&event); err != nil { + slog.Error("error decoding event", "error", err) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + slog.Info("push!", "ref", event.Ref, "author", event.Pusher.Login) + + if event.Ref != "refs/heads/"+*gitBranch { + slog.Error("not the right branch", "branch", event.Ref) + fmt.Fprintln(w, "OK") + return + } + + go func() { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) + defer cancel() + if err := gh.fs.Update(ctx); err != nil { + buildErrors.Add(err.Error(), 1) + if err == git.NoErrAlreadyUpToDate { + slog.Info("already up to date") + return + } + + slog.Error("error updating", "error", err) + } + }() +} -- cgit v1.2.3