|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "log" |
| 7 | + "log/slog" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + |
| 12 | + "github.com/donatj/hmacsig" |
| 13 | + "github.com/facebookgo/flagenv" |
| 14 | + _ "github.com/joho/godotenv/autoload" |
| 15 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 16 | + "gorm.io/driver/postgres" |
| 17 | + "gorm.io/gorm" |
| 18 | + "xeiaso.net/v4/internal" |
| 19 | + "xeiaso.net/v4/internal/models" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + bind = flag.String("bind", ":4823", "Port to listen on") |
| 24 | + databaseURL = flag.String("database-url", "", "Database URL") |
| 25 | + githubSponsorsSecret = flag.String("github-sponsors-secret", "", "GitHub Sponsors secret to use for webhooks") |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + flagenv.Parse() |
| 30 | + flag.Parse() |
| 31 | + internal.Slog() |
| 32 | + |
| 33 | + _ = context.Background() |
| 34 | + |
| 35 | + ln, err := net.Listen("tcp", *bind) |
| 36 | + if err != nil { |
| 37 | + log.Fatal(err) |
| 38 | + } |
| 39 | + |
| 40 | + if *databaseURL == "" { |
| 41 | + slog.Error("database-url is required") |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + if *githubSponsorsSecret == "" { |
| 46 | + slog.Error("github-sponsors-secret is required") |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + slog.Info("starting GitHub Sponsors webhook service", |
| 51 | + "bind", *bind, |
| 52 | + ) |
| 53 | + |
| 54 | + db, err := gorm.Open(postgres.Open(*databaseURL), &gorm.Config{}) |
| 55 | + if err != nil { |
| 56 | + slog.Error("can't connect to database", "err", err) |
| 57 | + os.Exit(1) |
| 58 | + } |
| 59 | + |
| 60 | + if err := db.Exec("SELECT 1 + 1").Error; err != nil { |
| 61 | + slog.Error("can't ping database", "err", err) |
| 62 | + os.Exit(1) |
| 63 | + } |
| 64 | + |
| 65 | + if err := models.SetupDatabase(db); err != nil { |
| 66 | + slog.Error("database setup error", "err", err) |
| 67 | + os.Exit(1) |
| 68 | + } |
| 69 | + |
| 70 | + gsh := &GitHubSponsorsWebhook{DB: db} |
| 71 | + s := hmacsig.Handler256(gsh, *githubSponsorsSecret) |
| 72 | + |
| 73 | + mux := http.NewServeMux() |
| 74 | + mux.Handle("/.within/hook/github-sponsors", s) |
| 75 | + mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { |
| 76 | + w.Header().Set("Content-Type", "application/json") |
| 77 | + w.WriteHeader(http.StatusOK) |
| 78 | + w.Write([]byte(`{"status":"ok","service":"github-sponsor-webhook"}`)) |
| 79 | + }) |
| 80 | + |
| 81 | + // Expose Prometheus metrics at /metrics for observability |
| 82 | + mux.Handle("/metrics", promhttp.Handler()) |
| 83 | + |
| 84 | + var h http.Handler = mux |
| 85 | + h = internal.CacheHeader(h) |
| 86 | + h = internal.AcceptEncodingMiddleware(h) |
| 87 | + h = internal.RefererMiddleware(h) |
| 88 | + |
| 89 | + slog.Info("GitHub Sponsors webhook service ready", "bind", *bind) |
| 90 | + log.Fatal(http.Serve(ln, h)) |
| 91 | +} |
0 commit comments