draft grpc

This commit is contained in:
2026-03-13 02:17:18 +00:00
parent ea2edbb9e0
commit 91e5e3542b
116 changed files with 44505 additions and 558 deletions

View File

@@ -1,29 +1,38 @@
//go:build ignore
// +build ignore
package app
import (
"context"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"stream.api/internal/api/admin"
"stream.api/internal/api/adtemplates"
"stream.api/internal/api/auth"
"stream.api/internal/api/domains"
"stream.api/internal/api/notifications"
"stream.api/internal/api/payment"
"stream.api/internal/api/plan"
"stream.api/internal/api/preferences"
"stream.api/internal/api/usage"
"stream.api/internal/api/video"
"stream.api/internal/config"
"stream.api/internal/middleware"
videoruntime "stream.api/internal/video/runtime"
"stream.api/pkg/cache"
"stream.api/pkg/logger"
"stream.api/pkg/response"
"stream.api/pkg/storage"
"stream.api/pkg/token"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "stream.api/docs"
)
func SetupRouter(cfg *config.Config, db *gorm.DB, c cache.Cache, t token.Provider, l logger.Logger) *gin.Engine {
func SetupRouter(cfg *config.Config, db *gorm.DB, c cache.Cache, t token.Provider, l logger.Logger) (*gin.Engine, *videoruntime.Module, error) {
if cfg.Server.Mode == "release" {
gin.SetMode(gin.ReleaseMode)
}
@@ -36,7 +45,7 @@ func SetupRouter(cfg *config.Config, db *gorm.DB, c cache.Cache, t token.Provide
r.Use(middleware.ErrorHandler()) // Handle c.Errors
// CORS Middleware
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:5173", "http://localhost:8080"},
AllowOrigins: cfg.CORS.AllowOrigins,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Content-Type"},
ExposeHeaders: []string{"Content-Length"},
@@ -57,7 +66,7 @@ func SetupRouter(cfg *config.Config, db *gorm.DB, c cache.Cache, t token.Provide
})
// Auth Handler
authHandler := auth.NewHandler(c, t, l, cfg)
authHandler := auth.NewHandler(c, t, l, cfg, db)
// api := r.Group("/v")
authGroup := r.Group("/auth")
{
@@ -70,7 +79,7 @@ func SetupRouter(cfg *config.Config, db *gorm.DB, c cache.Cache, t token.Provide
}
// Auth Middleware
authMiddleware := middleware.NewAuthMiddleware(c, t, cfg)
authMiddleware := middleware.NewAuthMiddleware(c, t, cfg, db, l)
// Init Storage Provider (S3)
s3Provider, err := storage.NewS3Provider(cfg)
@@ -81,22 +90,47 @@ func SetupRouter(cfg *config.Config, db *gorm.DB, c cache.Cache, t token.Provide
}
// Handlers
planHandler := plan.NewHandler(l, cfg)
paymentHandler := payment.NewHandler(l, cfg)
videoHandler := video.NewHandler(l, cfg, s3Provider)
planHandler := plan.NewHandler(l, cfg, db)
paymentHandler := payment.NewHandler(l, cfg, db)
usageHandler := usage.NewHandler(l, db)
videoHandler := video.NewHandler(l, cfg, db, s3Provider)
preferencesHandler := preferences.NewHandler(l, db)
notificationHandler := notifications.NewHandler(l, db)
domainHandler := domains.NewHandler(l, db)
adTemplateHandler := adtemplates.NewHandler(l, db)
// Example protected group
protected := r.Group("")
protected.Use(authMiddleware.Handle())
{
protected.GET("/me", func(c *gin.Context) {
user, _ := c.Get("user")
response.Success(c, gin.H{"user": user})
// c.JSON(http.StatusOK, gin.H{
// "user": user,
// })
})
protected.GET("/me", authHandler.GetMe)
protected.PUT("/me", authHandler.UpdateMe)
protected.DELETE("/me", authHandler.DeleteMe)
protected.POST("/me/clear-data", authHandler.ClearMyData)
protected.POST("/auth/logout", authHandler.Logout)
protected.POST("/auth/change-password", authHandler.ChangePassword)
preferences := protected.Group("/settings/preferences")
preferences.GET("", preferencesHandler.GetPreferences)
preferences.PUT("", preferencesHandler.UpdatePreferences)
notifications := protected.Group("/notifications")
notifications.GET("", notificationHandler.ListNotifications)
notifications.POST("/:id/read", notificationHandler.MarkRead)
notifications.POST("/read-all", notificationHandler.MarkAllRead)
notifications.DELETE("/:id", notificationHandler.DeleteNotification)
notifications.DELETE("", notificationHandler.ClearNotifications)
domains := protected.Group("/domains")
domains.GET("", domainHandler.ListDomains)
domains.POST("", domainHandler.CreateDomain)
domains.DELETE("/:id", domainHandler.DeleteDomain)
adTemplates := protected.Group("/ad-templates")
adTemplates.GET("", adTemplateHandler.ListTemplates)
adTemplates.POST("", adTemplateHandler.CreateTemplate)
adTemplates.PUT("/:id", adTemplateHandler.UpdateTemplate)
adTemplates.DELETE("/:id", adTemplateHandler.DeleteTemplate)
// Plans
plans := protected.Group("/plans")
@@ -105,6 +139,12 @@ func SetupRouter(cfg *config.Config, db *gorm.DB, c cache.Cache, t token.Provide
// Payments
payments := protected.Group("/payments")
payments.POST("", paymentHandler.CreatePayment)
payments.GET("/history", paymentHandler.ListPaymentHistory)
payments.GET("/:id/invoice", paymentHandler.DownloadInvoice)
wallet := protected.Group("/wallet")
wallet.POST("/topups", paymentHandler.TopupWallet)
protected.GET("/usage", usageHandler.GetUsage)
// Videos
video := protected.Group("/videos")
@@ -112,7 +152,68 @@ func SetupRouter(cfg *config.Config, db *gorm.DB, c cache.Cache, t token.Provide
video.POST("", videoHandler.CreateVideo)
video.GET("", videoHandler.ListVideos)
video.GET("/:id", videoHandler.GetVideo)
video.PUT("/:id", videoHandler.UpdateVideo)
video.DELETE("/:id", videoHandler.DeleteVideo)
}
return r
renderModule, err := videoruntime.NewModule(context.Background(), cfg, db, c, t, l)
if err != nil {
return nil, nil, err
}
r.Use(videoruntime.MetricsMiddleware())
r.GET("/health/live", renderModule.HandleLive)
r.GET("/health/ready", renderModule.HandleReady)
r.GET("/health/detailed", renderModule.HandleDetailed)
r.GET("/metrics", renderModule.MetricsHandler())
// Admin routes — require auth + admin role
adminHandler := admin.NewHandler(l, db, renderModule)
adminGroup := r.Group("/admin")
adminGroup.Use(authMiddleware.Handle())
adminGroup.Use(middleware.RequireAdmin())
{
adminGroup.GET("/dashboard", adminHandler.Dashboard)
adminGroup.GET("/users", adminHandler.ListUsers)
adminGroup.POST("/users", adminHandler.CreateUser)
adminGroup.GET("/users/:id", adminHandler.GetUser)
adminGroup.PUT("/users/:id", adminHandler.UpdateUser)
adminGroup.PUT("/users/:id/role", adminHandler.UpdateUserRole)
adminGroup.DELETE("/users/:id", adminHandler.DeleteUser)
adminGroup.GET("/videos", adminHandler.ListVideos)
adminGroup.POST("/videos", adminHandler.CreateVideo)
adminGroup.GET("/videos/:id", adminHandler.GetVideo)
adminGroup.PUT("/videos/:id", adminHandler.UpdateVideo)
adminGroup.DELETE("/videos/:id", adminHandler.DeleteVideo)
adminGroup.GET("/payments", adminHandler.ListPayments)
adminGroup.POST("/payments", adminHandler.CreatePayment)
adminGroup.GET("/payments/:id", adminHandler.GetPayment)
adminGroup.PUT("/payments/:id", adminHandler.UpdatePayment)
adminGroup.GET("/plans", adminHandler.ListPlans)
adminGroup.POST("/plans", adminHandler.CreatePlan)
adminGroup.PUT("/plans/:id", adminHandler.UpdatePlan)
adminGroup.DELETE("/plans/:id", adminHandler.DeletePlan)
adminGroup.GET("/ad-templates", adminHandler.ListAdTemplates)
adminGroup.POST("/ad-templates", adminHandler.CreateAdTemplate)
adminGroup.GET("/ad-templates/:id", adminHandler.GetAdTemplate)
adminGroup.PUT("/ad-templates/:id", adminHandler.UpdateAdTemplate)
adminGroup.DELETE("/ad-templates/:id", adminHandler.DeleteAdTemplate)
adminGroup.GET("/jobs", adminHandler.ListJobs)
adminGroup.POST("/jobs", adminHandler.CreateJob)
adminGroup.GET("/jobs/:id", adminHandler.GetJob)
adminGroup.GET("/jobs/:id/logs", adminHandler.GetJobLogs)
adminGroup.POST("/jobs/:id/cancel", adminHandler.CancelJob)
adminGroup.POST("/jobs/:id/retry", adminHandler.RetryJob)
adminGroup.GET("/agents", adminHandler.ListAgents)
adminGroup.POST("/agents/:id/restart", adminHandler.RestartAgent)
adminGroup.POST("/agents/:id/update", adminHandler.UpdateAgent)
}
return r, renderModule, nil
}