Files
flatrender/services/notification/cmd/server/main.go
T
soroush.asadi 6dbb14d146
Build backend images / build content-svc (push) Failing after 14s
Build backend images / build file-svc (push) Failing after 22s
Build backend images / build gateway (push) Failing after 1m21s
Build backend images / build identity-svc (push) Failing after 1m43s
Build backend images / build notification-svc (push) Failing after 1m6s
Build backend images / build render-svc (push) Failing after 53s
Build backend images / build studio-svc (push) Failing after 1m5s
feat(notifications+admin): marketing campaigns
- campaigns table (migration 19) + CRUD + send endpoint in notification-svc
- audience resolution reads cross-schema from identity.users (all / verified /
  with_plan); send dispatches via the SMS or Email channel and logs deliveries
- endpoints: GET/POST /v1/campaigns, POST /v1/campaigns/:id/send, DELETE
- gateway route /v1/campaigns/* → notification
- /admin/marketing: create campaign (channel, audience, template/subject/body),
  list with status + sent counts, send, delete

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 18:17:19 +03:30

111 lines
3.9 KiB
Go

package main
import (
"context"
"log"
"net/http"
"os"
"github.com/flatrender/notification-svc/internal/db"
"github.com/flatrender/notification-svc/internal/handlers"
"github.com/flatrender/notification-svc/internal/middleware"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
)
func getEnv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func main() {
dbURL := getEnv("DATABASE_URL", "postgres://postgres:postgres@localhost:5432/flatrender?search_path=notification,public")
jwtSecret := getEnv("JWT_SECRET", "change-me")
serviceToken := getEnv("SERVICE_TOKEN", "internal-service-secret")
port := getEnv("PORT", "8080")
pool, err := pgxpool.New(context.Background(), dbURL)
if err != nil {
log.Fatalf("connect db: %v", err)
}
defer pool.Close()
if err := pool.Ping(context.Background()); err != nil {
log.Fatalf("ping db: %v", err)
}
store := db.NewStore(pool)
notifH := handlers.NewNotificationHandler(store)
prefH := handlers.NewPreferenceHandler(store)
tplH := handlers.NewTemplateHandler(store)
chH := handlers.NewChannelHandler(store)
campH := handlers.NewCampaignHandler(store)
r := gin.Default()
r.GET("/health", func(c *gin.Context) {
if err := pool.Ping(c.Request.Context()); err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"status": "down", "error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
auth := middleware.JWTAuth(jwtSecret)
admin := middleware.RequireAdmin()
// Service-to-service calls: Bearer token must equal SERVICE_TOKEN
serviceAuth := func(c *gin.Context) {
hdr := c.GetHeader("Authorization")
if len(hdr) > 7 && hdr[7:] == serviceToken {
c.Next()
return
}
// Also accept regular JWT with admin flag
auth(c)
}
v1 := r.Group("/v1")
// ── Notification feed (user) ──────────────────────────────────────────────
notifs := v1.Group("/notifications", auth)
{
notifs.GET("", notifH.List)
notifs.GET("/unread-count", notifH.UnreadCount)
notifs.POST("/seen-all", notifH.MarkAllSeen)
notifs.GET("/:id", notifH.Get)
notifs.POST("/:id/seen", notifH.MarkSeen)
notifs.POST("/:id/click", notifH.MarkClicked)
notifs.DELETE("/:id", notifH.Delete)
}
// ── Preferences (user) ────────────────────────────────────────────────────
notifs.GET("/preferences", prefH.List)
notifs.PUT("/preferences", prefH.Upsert)
// ── Templates (admin) ─────────────────────────────────────────────────────
v1.GET("/notification-templates", auth, admin, tplH.List)
v1.PUT("/notification-templates", auth, admin, tplH.Upsert)
// ── Channels: SMS (Kavenegar) + Email (SMTP) config & send (admin) ────────
v1.GET("/channels", auth, admin, chH.ListConfigs)
v1.PUT("/channels/:channel", auth, admin, chH.UpsertConfig)
v1.POST("/sms/send", auth, admin, chH.SendSMS)
v1.POST("/email/send", auth, admin, chH.SendEmail)
// ── Marketing campaigns (admin) ───────────────────────────────────────────
v1.GET("/campaigns", auth, admin, campH.List)
v1.POST("/campaigns", auth, admin, campH.Create)
v1.POST("/campaigns/:id/send", auth, admin, campH.Send)
v1.DELETE("/campaigns/:id", auth, admin, campH.Delete)
// ── Internal: create notification (service-to-service) ───────────────────
v1.POST("/internal/notifications", serviceAuth, notifH.CreateInternal)
log.Printf("notification-svc listening on :%s", port)
if err := r.Run(":" + port); err != nil {
log.Fatalf("server: %v", err)
}
}