router.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package handler
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "spider/internal/store"
  7. "spider/internal/task"
  8. )
  9. // SetupRouter builds and returns the Gin engine with all routes registered.
  10. func SetupRouter(s *store.Store, taskMgr *task.Manager) *gin.Engine {
  11. r := gin.Default()
  12. r.GET("/ping", func(c *gin.Context) {
  13. c.JSON(http.StatusOK, gin.H{"message": "pong"})
  14. })
  15. api := r.Group("/api/v1")
  16. // Keywords (unified: search keywords + seeds)
  17. kw := &KeywordHandler{store: s}
  18. api.GET("/keywords", kw.List)
  19. api.POST("/keywords", kw.Create)
  20. api.PUT("/keywords/:id", kw.Update)
  21. api.DELETE("/keywords/:id", kw.Delete)
  22. // Merchants
  23. mc := &MerchantHandler{store: s}
  24. api.GET("/merchants/stats", mc.Stats)
  25. api.GET("/merchants/raw", mc.ListRaw)
  26. api.GET("/merchants/clean", mc.ListClean)
  27. api.GET("/merchants/clean/export", mc.ExportCSV)
  28. api.GET("/merchants/:id", mc.GetByID)
  29. // Tasks
  30. th := &TaskHandler{store: s, taskMgr: taskMgr}
  31. api.GET("/tasks", th.List)
  32. api.POST("/tasks/start", th.Start)
  33. api.GET("/tasks/:id", th.Get)
  34. api.POST("/tasks/:id/stop", th.Stop)
  35. api.GET("/tasks/:id/logs", th.Logs)
  36. return r
  37. }
  38. // ServerAddr returns the listen address string for the given port.
  39. func ServerAddr(port int) string {
  40. return fmt.Sprintf(":%d", port)
  41. }