| 1 | package main |
| 2 |
| 3 | import ( |
| 4 | "cmp" |
| 5 | "context" |
| 6 | "encoding/base64" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "log" |
| 10 | "net/http" |
| 11 | "os" |
| 12 | "os/signal" |
| 13 | "strconv" |
| 14 | "syscall" |
| 15 | "time" |
| 16 |
| 17 | "github.com/jackc/pgx/v5/pgxpool" |
| 18 | ) |
| 19 |
| 20 | func main() { |
| 21 | ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 22 | defer stop() |
| 23 | databaseURL := os.Getenv("DATABASE_URL") |
| 24 | port := os.Getenv("PORT") |
| 25 | if port == "" { |
| 26 | port = "8080" |
| 27 | } |
| 28 |
| 29 | if databaseURL == "" { |
| 30 | log.Fatal("DATABASE_URL is not set") |
| 31 | } |
| 32 | workers := envInt("WORKERS", 32) |
| 33 | pool, err := pgxpool.New(ctx, databaseURL) |
| 34 | if err != nil { |
| 35 | log.Fatalf("unable to connect to database: %v", err) |
| 36 | } |
| 37 | defer pool.Close() |
| 38 |
| 39 | if err := pool.Ping(ctx); err != nil { |
| 40 | log.Fatalf("unable to connect to database: %v", err) |
| 41 | } |
| 42 |
| 43 | if _, err := pool.Exec(ctx, "select 1 from delivery_job limit 0"); err != nil { |
| 44 | log.Fatalf("delivery_job not found, run the migrations first: %v", err) |
| 45 | } |
| 46 |
| 47 | mux := http.NewServeMux() |
| 48 | mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) { |
| 49 | w.Write([]byte("ok")) |
| 50 | }) |
| 51 | srv := &http.Server{Addr: ":" + port, Handler: mux} |
| 52 | go func() { |
| 53 | if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { |
| 54 | log.Fatalf("http server: %v", err) |
| 55 | } |
| 56 | }() |
| 57 | log.Printf("listening on :%s", port) |
| 58 |
| 59 | wake := make(chan struct{}, 1) |
| 60 | go listen(ctx, pool, wake) |
| 61 | go pruneEvery(ctx, pool, pruneInterval) |
| 62 | email := relaySender{ |
| 63 | url: os.Getenv("EMAIL_PROXY"), |
| 64 | secret: os.Getenv("EMAIL_PROXY_SECRET"), |
| 65 | from: cmp.Or(os.Getenv("EMAIL_SENDER"), "Ghost <noreply@ghost.local>"), |
| 66 | client: &http.Client{}, |
| 67 | } |
| 68 | if email.url == "" { |
| 69 | log.Printf("EMAIL_PROXY is not set: email jobs will fail and retry until it is") |
| 70 | } |
| 71 | var key []byte |
| 72 | if v := os.Getenv("WEBHOOK_SECRET_KEY"); v != "" { |
| 73 | key, err = base64.StdEncoding.DecodeString(v) |
| 74 | if err != nil || len(key) != 32 { |
| 75 | log.Fatal("WEBHOOK_SECRET_KEY must be 32 bytes, base64-encoded") |
| 76 | } |
| 77 | } else { |
| 78 | log.Printf("WEBHOOK_SECRET_KEY is not set: webhook jobs will fail and retry until it is") |
| 79 | } |
| 80 | hostConcurrency := envInt("WEBHOOK_HOST_CONCURRENCY", 4) |
| 81 | webhook := webhookSender{ |
| 82 | db: pool, |
| 83 | client: newWebhookClient(os.Getenv("WEBHOOK_ALLOW_PRIVATE_NETWORKS") == "true"), |
| 84 | key: key, |
| 85 | hosts: newHostLimiter(hostConcurrency), |
| 86 | } |
| 87 | send := func(ctx context.Context, j Job) error { |
| 88 | switch j.Kind { |
| 89 | case "email": |
| 90 | return email.send(ctx, j) |
| 91 | case "webhook": |
| 92 | return webhook.send(ctx, j) |
| 93 | } |
| 94 | return fmt.Errorf("no sender for job kind %q", j.Kind) |
| 95 | } |
| 96 | run(ctx, pool, workers, wake, send) // returns after SIGTERM once in-flight sends drain |
| 97 |
| 98 | shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 99 | defer cancel() |
| 100 | if err := srv.Shutdown(shutdownCtx); err != nil { |
| 101 | log.Printf("http shutdown: %v", err) |
| 102 | } |
| 103 | } |
| 104 |
| 105 | // envInt reads a positive integer setting, or returns def when it is unset. |
| 106 | func envInt(name string, def int) int { |
| 107 | v := os.Getenv(name) |
| 108 | if v == "" { |
| 109 | return def |
| 110 | } |
| 111 | n, err := strconv.Atoi(v) |
| 112 | if err != nil || n < 1 { |
| 113 | log.Fatalf("%s must be a positive integer, got %q", name, v) |
| 114 | } |
| 115 | return n |
| 116 | } |