api-router.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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("/setup", controller.GetSetup)
  14. apiRouter.POST("/setup", controller.PostSetup)
  15. apiRouter.GET("/status", controller.GetStatus)
  16. apiRouter.GET("/uptime/status", controller.GetUptimeKumaStatus)
  17. apiRouter.GET("/models", middleware.UserAuth(), controller.DashboardListModels)
  18. apiRouter.GET("/status/test", middleware.AdminAuth(), controller.TestStatus)
  19. apiRouter.GET("/notice", controller.GetNotice)
  20. apiRouter.GET("/about", controller.GetAbout)
  21. //apiRouter.GET("/midjourney", controller.GetMidjourney)
  22. apiRouter.GET("/home_page_content", controller.GetHomePageContent)
  23. apiRouter.GET("/pricing", middleware.TryUserAuth(), controller.GetPricing)
  24. apiRouter.GET("/verification", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendEmailVerification)
  25. apiRouter.GET("/reset_password", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendPasswordResetEmail)
  26. apiRouter.POST("/user/reset", middleware.CriticalRateLimit(), controller.ResetPassword)
  27. apiRouter.GET("/oauth/github", middleware.CriticalRateLimit(), controller.GitHubOAuth)
  28. apiRouter.GET("/oauth/oidc", middleware.CriticalRateLimit(), controller.OidcAuth)
  29. apiRouter.GET("/oauth/linuxdo", middleware.CriticalRateLimit(), controller.LinuxdoOAuth)
  30. apiRouter.GET("/oauth/state", middleware.CriticalRateLimit(), controller.GenerateOAuthCode)
  31. apiRouter.GET("/oauth/wechat", middleware.CriticalRateLimit(), controller.WeChatAuth)
  32. apiRouter.GET("/oauth/wechat/bind", middleware.CriticalRateLimit(), controller.WeChatBind)
  33. apiRouter.GET("/oauth/email/bind", middleware.CriticalRateLimit(), controller.EmailBind)
  34. apiRouter.GET("/oauth/telegram/login", middleware.CriticalRateLimit(), controller.TelegramLogin)
  35. apiRouter.GET("/oauth/telegram/bind", middleware.CriticalRateLimit(), controller.TelegramBind)
  36. apiRouter.GET("/ratio_config", middleware.CriticalRateLimit(), controller.GetRatioConfig)
  37. userRoute := apiRouter.Group("/user")
  38. {
  39. userRoute.POST("/register", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Register)
  40. userRoute.POST("/login", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Login)
  41. //userRoute.POST("/tokenlog", middleware.CriticalRateLimit(), controller.TokenLog)
  42. userRoute.GET("/logout", controller.Logout)
  43. userRoute.GET("/epay/notify", controller.EpayNotify)
  44. userRoute.GET("/groups", controller.GetUserGroups)
  45. selfRoute := userRoute.Group("/")
  46. selfRoute.Use(middleware.UserAuth())
  47. {
  48. selfRoute.GET("/self/groups", controller.GetUserGroups)
  49. selfRoute.GET("/self", controller.GetSelf)
  50. selfRoute.GET("/models", controller.GetUserModels)
  51. selfRoute.PUT("/self", controller.UpdateSelf)
  52. selfRoute.DELETE("/self", controller.DeleteSelf)
  53. selfRoute.GET("/token", controller.GenerateAccessToken)
  54. selfRoute.GET("/aff", controller.GetAffCode)
  55. selfRoute.POST("/topup", controller.TopUp)
  56. selfRoute.POST("/pay", controller.RequestEpay)
  57. selfRoute.POST("/amount", controller.RequestAmount)
  58. selfRoute.POST("/aff_transfer", controller.TransferAffQuota)
  59. selfRoute.PUT("/setting", controller.UpdateUserSetting)
  60. }
  61. adminRoute := userRoute.Group("/")
  62. adminRoute.Use(middleware.AdminAuth())
  63. {
  64. adminRoute.GET("/", controller.GetAllUsers)
  65. adminRoute.GET("/search", controller.SearchUsers)
  66. adminRoute.GET("/:id", controller.GetUser)
  67. adminRoute.POST("/", controller.CreateUser)
  68. adminRoute.POST("/manage", controller.ManageUser)
  69. adminRoute.PUT("/", controller.UpdateUser)
  70. adminRoute.DELETE("/:id", controller.DeleteUser)
  71. }
  72. }
  73. optionRoute := apiRouter.Group("/option")
  74. optionRoute.Use(middleware.RootAuth())
  75. {
  76. optionRoute.GET("/", controller.GetOptions)
  77. optionRoute.PUT("/", controller.UpdateOption)
  78. optionRoute.POST("/rest_model_ratio", controller.ResetModelRatio)
  79. optionRoute.POST("/migrate_console_setting", controller.MigrateConsoleSetting) // 用于迁移检测的旧键,下个版本会删除
  80. }
  81. ratioSyncRoute := apiRouter.Group("/ratio_sync")
  82. ratioSyncRoute.Use(middleware.RootAuth())
  83. {
  84. ratioSyncRoute.GET("/channels", controller.GetSyncableChannels)
  85. ratioSyncRoute.POST("/fetch", controller.FetchUpstreamRatios)
  86. }
  87. channelRoute := apiRouter.Group("/channel")
  88. channelRoute.Use(middleware.AdminAuth())
  89. {
  90. channelRoute.GET("/", controller.GetAllChannels)
  91. channelRoute.GET("/search", controller.SearchChannels)
  92. channelRoute.GET("/models", controller.ChannelListModels)
  93. channelRoute.GET("/models_enabled", controller.EnabledListModels)
  94. channelRoute.GET("/:id", controller.GetChannel)
  95. channelRoute.GET("/test", controller.TestAllChannels)
  96. channelRoute.GET("/test/:id", controller.TestChannel)
  97. channelRoute.GET("/update_balance", controller.UpdateAllChannelsBalance)
  98. channelRoute.GET("/update_balance/:id", controller.UpdateChannelBalance)
  99. channelRoute.POST("/", controller.AddChannel)
  100. channelRoute.PUT("/", controller.UpdateChannel)
  101. channelRoute.DELETE("/disabled", controller.DeleteDisabledChannel)
  102. channelRoute.POST("/tag/disabled", controller.DisableTagChannels)
  103. channelRoute.POST("/tag/enabled", controller.EnableTagChannels)
  104. channelRoute.PUT("/tag", controller.EditTagChannels)
  105. channelRoute.DELETE("/:id", controller.DeleteChannel)
  106. channelRoute.POST("/batch", controller.DeleteChannelBatch)
  107. channelRoute.POST("/fix", controller.FixChannelsAbilities)
  108. channelRoute.GET("/fetch_models/:id", controller.FetchUpstreamModels)
  109. channelRoute.POST("/fetch_models", controller.FetchModels)
  110. channelRoute.POST("/batch/tag", controller.BatchSetChannelTag)
  111. channelRoute.GET("/tag/models", controller.GetTagModels)
  112. }
  113. tokenRoute := apiRouter.Group("/token")
  114. tokenRoute.Use(middleware.UserAuth())
  115. {
  116. tokenRoute.GET("/", controller.GetAllTokens)
  117. tokenRoute.GET("/search", controller.SearchTokens)
  118. tokenRoute.GET("/:id", controller.GetToken)
  119. tokenRoute.POST("/", controller.AddToken)
  120. tokenRoute.PUT("/", controller.UpdateToken)
  121. tokenRoute.DELETE("/:id", controller.DeleteToken)
  122. tokenRoute.POST("/batch", controller.DeleteTokenBatch)
  123. }
  124. redemptionRoute := apiRouter.Group("/redemption")
  125. redemptionRoute.Use(middleware.AdminAuth())
  126. {
  127. redemptionRoute.GET("/", controller.GetAllRedemptions)
  128. redemptionRoute.GET("/search", controller.SearchRedemptions)
  129. redemptionRoute.GET("/:id", controller.GetRedemption)
  130. redemptionRoute.POST("/", controller.AddRedemption)
  131. redemptionRoute.PUT("/", controller.UpdateRedemption)
  132. redemptionRoute.DELETE("/invalid", controller.DeleteInvalidRedemption)
  133. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  134. }
  135. logRoute := apiRouter.Group("/log")
  136. logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
  137. logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
  138. logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
  139. logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
  140. logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
  141. logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
  142. logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
  143. dataRoute := apiRouter.Group("/data")
  144. dataRoute.GET("/", middleware.AdminAuth(), controller.GetAllQuotaDates)
  145. dataRoute.GET("/self", middleware.UserAuth(), controller.GetUserQuotaDates)
  146. logRoute.Use(middleware.CORS())
  147. {
  148. logRoute.GET("/token", controller.GetLogByKey)
  149. }
  150. groupRoute := apiRouter.Group("/group")
  151. groupRoute.Use(middleware.AdminAuth())
  152. {
  153. groupRoute.GET("/", controller.GetGroups)
  154. }
  155. mjRoute := apiRouter.Group("/mj")
  156. mjRoute.GET("/self", middleware.UserAuth(), controller.GetUserMidjourney)
  157. mjRoute.GET("/", middleware.AdminAuth(), controller.GetAllMidjourney)
  158. taskRoute := apiRouter.Group("/task")
  159. {
  160. taskRoute.GET("/self", middleware.UserAuth(), controller.GetUserTask)
  161. taskRoute.GET("/", middleware.AdminAuth(), controller.GetAllTask)
  162. }
  163. }
  164. }