api-router.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package router
  2. import (
  3. "one-api/controller"
  4. "one-api/middleware"
  5. "github.com/gin-contrib/gzip"
  6. "github.com/gin-gonic/gin"
  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("/home_page_content", controller.GetHomePageContent)
  17. apiRouter.GET("/verification", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendEmailVerification)
  18. apiRouter.GET("/reset_password", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendPasswordResetEmail)
  19. apiRouter.POST("/user/reset", middleware.CriticalRateLimit(), controller.ResetPassword)
  20. apiRouter.GET("/oauth/github", middleware.CriticalRateLimit(), controller.GitHubOAuth)
  21. apiRouter.GET("/oauth/state", middleware.CriticalRateLimit(), controller.GenerateOAuthCode)
  22. apiRouter.GET("/oauth/wechat", middleware.CriticalRateLimit(), controller.WeChatAuth)
  23. apiRouter.GET("/oauth/wechat/bind", middleware.CriticalRateLimit(), middleware.UserAuth(), controller.WeChatBind)
  24. apiRouter.GET("/oauth/email/bind", middleware.CriticalRateLimit(), middleware.UserAuth(), controller.EmailBind)
  25. userRoute := apiRouter.Group("/user")
  26. {
  27. userRoute.POST("/register", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Register)
  28. userRoute.POST("/login", middleware.CriticalRateLimit(), controller.Login)
  29. userRoute.GET("/logout", controller.Logout)
  30. selfRoute := userRoute.Group("/")
  31. selfRoute.Use(middleware.UserAuth())
  32. {
  33. selfRoute.GET("/self", controller.GetSelf)
  34. selfRoute.PUT("/self", controller.UpdateSelf)
  35. selfRoute.DELETE("/self", controller.DeleteSelf)
  36. selfRoute.GET("/token", controller.GenerateAccessToken)
  37. selfRoute.GET("/aff", controller.GetAffCode)
  38. selfRoute.POST("/topup", controller.TopUp)
  39. }
  40. adminRoute := userRoute.Group("/")
  41. adminRoute.Use(middleware.AdminAuth())
  42. {
  43. adminRoute.GET("/", controller.GetAllUsers)
  44. adminRoute.GET("/search", controller.SearchUsers)
  45. adminRoute.GET("/:id", controller.GetUser)
  46. adminRoute.POST("/", controller.CreateUser)
  47. adminRoute.POST("/manage", controller.ManageUser)
  48. adminRoute.PUT("/", controller.UpdateUser)
  49. adminRoute.DELETE("/:id", controller.DeleteUser)
  50. }
  51. }
  52. optionRoute := apiRouter.Group("/option")
  53. optionRoute.Use(middleware.RootAuth())
  54. {
  55. optionRoute.GET("/", controller.GetOptions)
  56. optionRoute.PUT("/", controller.UpdateOption)
  57. }
  58. channelRoute := apiRouter.Group("/channel")
  59. channelRoute.Use(middleware.AdminAuth())
  60. {
  61. channelRoute.GET("/", controller.GetAllChannels)
  62. channelRoute.GET("/search", controller.SearchChannels)
  63. channelRoute.GET("/models", controller.ListModels)
  64. channelRoute.GET("/:id", controller.GetChannel)
  65. channelRoute.GET("/test", controller.TestAllChannels)
  66. channelRoute.GET("/test/:id", controller.TestChannel)
  67. channelRoute.GET("/update_balance", controller.UpdateAllChannelsBalance)
  68. channelRoute.GET("/update_balance/:id", controller.UpdateChannelBalance)
  69. channelRoute.POST("/", controller.AddChannel)
  70. channelRoute.PUT("/", controller.UpdateChannel)
  71. channelRoute.DELETE("/:id", controller.DeleteChannel)
  72. }
  73. tokenRoute := apiRouter.Group("/token")
  74. tokenRoute.Use(middleware.UserAuth())
  75. {
  76. tokenRoute.GET("/", controller.GetAllTokens)
  77. tokenRoute.GET("/search", controller.SearchTokens)
  78. tokenRoute.GET("/:id", controller.GetToken)
  79. tokenRoute.POST("/", controller.AddToken)
  80. tokenRoute.PUT("/", controller.UpdateToken)
  81. tokenRoute.DELETE("/:id", controller.DeleteToken)
  82. }
  83. redemptionRoute := apiRouter.Group("/redemption")
  84. redemptionRoute.Use(middleware.AdminAuth())
  85. {
  86. redemptionRoute.GET("/", controller.GetAllRedemptions)
  87. redemptionRoute.GET("/search", controller.SearchRedemptions)
  88. redemptionRoute.GET("/:id", controller.GetRedemption)
  89. redemptionRoute.POST("/", controller.AddRedemption)
  90. redemptionRoute.PUT("/", controller.UpdateRedemption)
  91. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  92. }
  93. logRoute := apiRouter.Group("/log")
  94. logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
  95. logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
  96. logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
  97. logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
  98. logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
  99. logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
  100. logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
  101. groupRoute := apiRouter.Group("/group")
  102. groupRoute.Use(middleware.AdminAuth())
  103. {
  104. groupRoute.GET("/", controller.GetGroups)
  105. }
  106. }
  107. }