api-router.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package router
  2. import (
  3. "github.com/gin-contrib/gzip"
  4. "github.com/gin-gonic/gin"
  5. "one-api/controller"
  6. "one-api/middleware"
  7. )
  8. func SetApiRouter(router *gin.Engine) {
  9. apiRouter := router.Group("/api")
  10. apiRouter.Use(gzip.Gzip(gzip.DefaultCompression))
  11. apiRouter.Use(middleware.GlobalAPIRateLimit())
  12. {
  13. apiRouter.GET("/status", controller.GetStatus)
  14. apiRouter.GET("/notice", controller.GetNotice)
  15. apiRouter.GET("/about", controller.GetAbout)
  16. apiRouter.GET("/verification", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendEmailVerification)
  17. apiRouter.GET("/reset_password", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendPasswordResetEmail)
  18. apiRouter.POST("/user/reset", middleware.CriticalRateLimit(), controller.ResetPassword)
  19. apiRouter.GET("/oauth/github", middleware.CriticalRateLimit(), controller.GitHubOAuth)
  20. apiRouter.GET("/oauth/wechat", middleware.CriticalRateLimit(), controller.WeChatAuth)
  21. apiRouter.GET("/oauth/wechat/bind", middleware.CriticalRateLimit(), middleware.UserAuth(), controller.WeChatBind)
  22. apiRouter.GET("/oauth/email/bind", middleware.CriticalRateLimit(), middleware.UserAuth(), controller.EmailBind)
  23. userRoute := apiRouter.Group("/user")
  24. {
  25. userRoute.POST("/register", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Register)
  26. userRoute.POST("/login", middleware.CriticalRateLimit(), controller.Login)
  27. userRoute.GET("/logout", controller.Logout)
  28. selfRoute := userRoute.Group("/")
  29. selfRoute.Use(middleware.UserAuth())
  30. {
  31. selfRoute.GET("/self", controller.GetSelf)
  32. selfRoute.PUT("/self", controller.UpdateSelf)
  33. selfRoute.DELETE("/self", controller.DeleteSelf)
  34. }
  35. adminRoute := userRoute.Group("/")
  36. adminRoute.Use(middleware.AdminAuth())
  37. {
  38. adminRoute.GET("/", controller.GetAllUsers)
  39. adminRoute.GET("/search", controller.SearchUsers)
  40. adminRoute.GET("/:id", controller.GetUser)
  41. adminRoute.POST("/", controller.CreateUser)
  42. adminRoute.POST("/manage", controller.ManageUser)
  43. adminRoute.PUT("/", controller.UpdateUser)
  44. adminRoute.DELETE("/:id", controller.DeleteUser)
  45. }
  46. }
  47. optionRoute := apiRouter.Group("/option")
  48. optionRoute.Use(middleware.RootAuth())
  49. {
  50. optionRoute.GET("/", controller.GetOptions)
  51. optionRoute.PUT("/", controller.UpdateOption)
  52. }
  53. channelRoute := apiRouter.Group("/channel")
  54. channelRoute.Use(middleware.AdminAuth())
  55. {
  56. channelRoute.GET("/", controller.GetAllChannels)
  57. channelRoute.GET("/search", controller.SearchChannels)
  58. channelRoute.GET("/:id", controller.GetChannel)
  59. channelRoute.POST("/", controller.AddChannel)
  60. channelRoute.PUT("/", controller.UpdateChannel)
  61. channelRoute.DELETE("/:id", controller.DeleteChannel)
  62. }
  63. tokenRoute := apiRouter.Group("/token")
  64. tokenRoute.Use(middleware.UserAuth())
  65. {
  66. tokenRoute.GET("/", controller.GetAllTokens)
  67. tokenRoute.GET("/search", controller.SearchTokens)
  68. tokenRoute.POST("/topup", controller.TopUp)
  69. tokenRoute.GET("/:id", controller.GetToken)
  70. tokenRoute.POST("/", controller.AddToken)
  71. tokenRoute.PUT("/", controller.UpdateToken)
  72. tokenRoute.DELETE("/:id", controller.DeleteToken)
  73. }
  74. redemptionRoute := apiRouter.Group("/redemption")
  75. redemptionRoute.Use(middleware.AdminAuth())
  76. {
  77. redemptionRoute.GET("/", controller.GetAllRedemptions)
  78. redemptionRoute.GET("/search", controller.SearchRedemptions)
  79. redemptionRoute.GET("/:id", controller.GetRedemption)
  80. redemptionRoute.POST("/", controller.AddRedemption)
  81. redemptionRoute.PUT("/", controller.UpdateRedemption)
  82. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  83. }
  84. }
  85. }