api-router.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. selfRoute.GET("/token", controller.GenerateAccessToken)
  35. }
  36. adminRoute := userRoute.Group("/")
  37. adminRoute.Use(middleware.AdminAuth())
  38. {
  39. adminRoute.GET("/", controller.GetAllUsers)
  40. adminRoute.GET("/search", controller.SearchUsers)
  41. adminRoute.GET("/:id", controller.GetUser)
  42. adminRoute.POST("/", controller.CreateUser)
  43. adminRoute.POST("/manage", controller.ManageUser)
  44. adminRoute.PUT("/", controller.UpdateUser)
  45. adminRoute.DELETE("/:id", controller.DeleteUser)
  46. }
  47. }
  48. optionRoute := apiRouter.Group("/option")
  49. optionRoute.Use(middleware.RootAuth())
  50. {
  51. optionRoute.GET("/", controller.GetOptions)
  52. optionRoute.PUT("/", controller.UpdateOption)
  53. }
  54. channelRoute := apiRouter.Group("/channel")
  55. channelRoute.Use(middleware.AdminAuth())
  56. {
  57. channelRoute.GET("/", controller.GetAllChannels)
  58. channelRoute.GET("/search", controller.SearchChannels)
  59. channelRoute.GET("/:id", controller.GetChannel)
  60. channelRoute.POST("/", controller.AddChannel)
  61. channelRoute.PUT("/", controller.UpdateChannel)
  62. channelRoute.DELETE("/:id", controller.DeleteChannel)
  63. }
  64. tokenRoute := apiRouter.Group("/token")
  65. tokenRoute.Use(middleware.UserAuth())
  66. {
  67. tokenRoute.GET("/", controller.GetAllTokens)
  68. tokenRoute.GET("/search", controller.SearchTokens)
  69. tokenRoute.POST("/topup", controller.TopUp)
  70. tokenRoute.GET("/:id", controller.GetToken)
  71. tokenRoute.POST("/", controller.AddToken)
  72. tokenRoute.PUT("/", controller.UpdateToken)
  73. tokenRoute.DELETE("/:id", controller.DeleteToken)
  74. }
  75. redemptionRoute := apiRouter.Group("/redemption")
  76. redemptionRoute.Use(middleware.AdminAuth())
  77. {
  78. redemptionRoute.GET("/", controller.GetAllRedemptions)
  79. redemptionRoute.GET("/search", controller.SearchRedemptions)
  80. redemptionRoute.GET("/:id", controller.GetRedemption)
  81. redemptionRoute.POST("/", controller.AddRedemption)
  82. redemptionRoute.PUT("/", controller.UpdateRedemption)
  83. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  84. }
  85. }
  86. }