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