api-router.go 3.0 KB

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