api-router.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.GET("/:id", controller.GetToken)
  69. tokenRoute.POST("/", controller.AddToken)
  70. tokenRoute.PUT("/", controller.UpdateToken)
  71. tokenRoute.DELETE("/:id", controller.DeleteToken)
  72. }
  73. }
  74. }