| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package handler
- import (
- "fmt"
- "net/http"
- "github.com/gin-gonic/gin"
- "github.com/redis/go-redis/v9"
- "gorm.io/gorm"
- "spider/internal/service"
- )
- // SetupRouter builds and returns the Gin engine with all routes registered.
- func SetupRouter(db *gorm.DB, rdb *redis.Client, taskSvc *service.TaskService) *gin.Engine {
- r := gin.Default()
- // Health check.
- r.GET("/ping", func(c *gin.Context) {
- c.JSON(http.StatusOK, gin.H{"message": "pong"})
- })
- api := r.Group("/api/v1")
- // Dashboard.
- dash := &DashboardHandler{db: db}
- api.GET("/dashboard", dash.Get)
- // Channels.
- ch := &ChannelHandler{db: db}
- api.GET("/channels", ch.List)
- api.GET("/channels/stats", ch.Stats)
- // Keywords.
- kw := &KeywordHandler{db: db}
- api.GET("/keywords", kw.List)
- api.POST("/keywords", kw.Create)
- api.PUT("/keywords/:id", kw.Update)
- api.DELETE("/keywords/:id", kw.Delete)
- // Seeds.
- sd := &SeedHandler{db: db}
- api.GET("/seeds", sd.List)
- api.POST("/seeds", sd.Create)
- api.PUT("/seeds/:id", sd.Update)
- api.DELETE("/seeds/:id", sd.Delete)
- // Merchants.
- mc := &MerchantHandler{db: db}
- api.GET("/merchants/stats", mc.Stats)
- api.GET("/merchants/raw", mc.ListRaw)
- api.GET("/merchants/clean", mc.ListClean)
- api.GET("/merchants/:id", mc.GetByID)
- // Nav sites.
- ns := &NavSiteHandler{db: db}
- api.GET("/nav-sites", ns.List)
- // Config / Settings.
- cfg := &ConfigHandler{db: db}
- api.GET("/config/settings", cfg.ListSettings)
- api.PUT("/config/settings/:key", cfg.UpdateSetting)
- // Tasks.
- th := NewTaskHandler(db, taskSvc, rdb)
- api.GET("/tasks", th.List)
- api.POST("/tasks/start", th.Start)
- api.GET("/tasks/:id", th.Get)
- api.POST("/tasks/:id/stop", th.Stop)
- api.GET("/tasks/:id/logs", th.Logs)
- return r
- }
- // ServerAddr returns the listen address string for the given port.
- func ServerAddr(port int) string {
- return fmt.Sprintf(":%d", port)
- }
|