router.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package handler
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/redis/go-redis/v9"
  7. "gorm.io/gorm"
  8. "spider/internal/service"
  9. )
  10. // SetupRouter builds and returns the Gin engine with all routes registered.
  11. func SetupRouter(db *gorm.DB, rdb *redis.Client, taskSvc *service.TaskService) *gin.Engine {
  12. r := gin.Default()
  13. // Health check.
  14. r.GET("/ping", func(c *gin.Context) {
  15. c.JSON(http.StatusOK, gin.H{"message": "pong"})
  16. })
  17. api := r.Group("/api/v1")
  18. // Dashboard.
  19. dash := &DashboardHandler{db: db}
  20. api.GET("/dashboard", dash.Get)
  21. // Channels.
  22. ch := &ChannelHandler{db: db}
  23. api.GET("/channels", ch.List)
  24. api.GET("/channels/stats", ch.Stats)
  25. // Keywords.
  26. kw := &KeywordHandler{db: db}
  27. api.GET("/keywords", kw.List)
  28. api.POST("/keywords", kw.Create)
  29. api.PUT("/keywords/:id", kw.Update)
  30. api.DELETE("/keywords/:id", kw.Delete)
  31. // Seeds.
  32. sd := &SeedHandler{db: db}
  33. api.GET("/seeds", sd.List)
  34. api.POST("/seeds", sd.Create)
  35. api.PUT("/seeds/:id", sd.Update)
  36. api.DELETE("/seeds/:id", sd.Delete)
  37. // Merchants.
  38. mc := &MerchantHandler{db: db}
  39. api.GET("/merchants/stats", mc.Stats)
  40. api.GET("/merchants/raw", mc.ListRaw)
  41. api.GET("/merchants/clean", mc.ListClean)
  42. api.GET("/merchants/:id", mc.GetByID)
  43. // Nav sites.
  44. ns := &NavSiteHandler{db: db}
  45. api.GET("/nav-sites", ns.List)
  46. // Config / Settings.
  47. cfg := &ConfigHandler{db: db}
  48. api.GET("/config/settings", cfg.ListSettings)
  49. api.PUT("/config/settings/:key", cfg.UpdateSetting)
  50. // Tasks.
  51. th := NewTaskHandler(db, taskSvc, rdb)
  52. api.GET("/tasks", th.List)
  53. api.POST("/tasks/start", th.Start)
  54. api.GET("/tasks/:id", th.Get)
  55. api.POST("/tasks/:id/stop", th.Stop)
  56. api.GET("/tasks/:id/logs", th.Logs)
  57. return r
  58. }
  59. // ServerAddr returns the listen address string for the given port.
  60. func ServerAddr(port int) string {
  61. return fmt.Sprintf(":%d", port)
  62. }