507ac7e6a4
Build backend images / build content-svc (push) Failing after 56s
Build backend images / build file-svc (push) Failing after 47s
Build backend images / build gateway (push) Failing after 1m0s
Build backend images / build identity-svc (push) Failing after 56s
Build backend images / build notification-svc (push) Failing after 11s
Build backend images / build render-svc (push) Failing after 4m5s
Build backend images / build studio-svc (push) Failing after 56s
Backend (notification-svc):
- channel_config table (per-tenant Kavenegar + SMTP settings) + migration 18
- sender pkg: Kavenegar SMS client + SMTP mailer (STARTTLS / implicit TLS), stdlib only
- endpoints: GET/PUT /v1/channels[/:channel], POST /v1/sms/send, POST /v1/email/send
(template + {{var}} rendering); deliveries logged
- seeded 3 Persian email templates: welcome / account_verification / promotion
- gateway routes /v1/{channels,sms,email}/* → notification
Admin UI:
- /admin/messaging: SMS + Email provider config cards, test-send, email template editor
- nav link + fa/en labels
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
104 lines
3.4 KiB
Go
104 lines
3.4 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)
|
|
|
|
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)
|
|
|
|
// ── 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)
|
|
}
|
|
}
|