Head of Product & Engineering 5312eb0ca2
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: per-location sender ID with admin API
Allows each GHL sub-account to use a different Cast sender ID instead of
the global CAST_SENDER_ID default.

- store.TokenRecord gains a sender_id field (MongoDB)
- store.UpdateSenderID method to set it per location
- cast.Client.SendSMS accepts a senderID override param (empty = use
  client-level default)
- webhook.processOutbound reads the location's sender_id from the token
  record and passes it to Cast
- new admin handler: PUT /api/admin/locations/{locationId}/sender-id
  protected by Authorization: Bearer <INBOUND_API_KEY>

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 12:47:00 +02:00

98 lines
2.7 KiB
Go

package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"git.sds.dev/CAST/cast-ghl-plugin/internal/cast"
"git.sds.dev/CAST/cast-ghl-plugin/internal/config"
"git.sds.dev/CAST/cast-ghl-plugin/internal/ghl"
"git.sds.dev/CAST/cast-ghl-plugin/internal/store"
)
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})))
if err := run(); err != nil {
slog.Error("fatal error", "err", err)
os.Exit(1)
}
}
func run() error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("config: %w", err)
}
ctx := context.Background()
s, err := store.NewStore(ctx, cfg.MongoURI)
if err != nil {
return fmt.Errorf("mongodb: %w", err)
}
defer func() { _ = s.Close(ctx) }()
castClient := cast.NewClient(cfg.CastAPIURL, cfg.CastAPIKey, cfg.CastSenderID)
ghlAPI := ghl.NewAPIClient()
oauthHandler := ghl.NewOAuthHandler(cfg.GHLClientID, cfg.GHLClientSecret, cfg.BaseURL, cfg.GHLConversationProviderID, s)
webhookHandler, err := ghl.NewWebhookHandler(cfg.GHLWebhookPublicKey, castClient, ghlAPI, oauthHandler, s)
if err != nil {
return fmt.Errorf("webhook handler: %w", err)
}
adminHandler := ghl.NewAdminHandler(cfg.InboundAPIKey, s)
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(60 * time.Second))
r.Get("/health", healthCheck)
r.Get("/install", oauthHandler.HandleInstall)
r.Get("/oauth-callback", oauthHandler.HandleCallback)
r.Post("/api/ghl/v1/webhook/messages", webhookHandler.HandleWebhook)
r.Post("/api/ghl/v1/webhook/uninstall", webhookHandler.HandleUninstall)
r.Put("/api/admin/locations/{locationId}/sender-id", adminHandler.HandleSetSenderID)
srv := &http.Server{
Addr: ":" + cfg.Port,
Handler: r,
ReadHeaderTimeout: 10 * time.Second,
}
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
slog.Info("shutting down...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
slog.Error("shutdown error", "err", err)
}
}()
slog.Info("cast-ghl-provider started", "port", cfg.Port, "base_url", cfg.BaseURL)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
return fmt.Errorf("server: %w", err)
}
return nil
}
func healthCheck(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"ok","service":"cast-ghl-provider"}`))
}