api-router.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. apiRouter.POST("/stripe/webhook", controller.StripeWebhook)
  38. userRoute := apiRouter.Group("/user")
  39. {
  40. userRoute.POST("/register", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Register)
  41. userRoute.POST("/login", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Login)
  42. //userRoute.POST("/tokenlog", middleware.CriticalRateLimit(), controller.TokenLog)
  43. userRoute.GET("/logout", controller.Logout)
  44. userRoute.GET("/epay/notify", controller.EpayNotify)
  45. userRoute.GET("/groups", controller.GetUserGroups)
  46. selfRoute := userRoute.Group("/")
  47. selfRoute.Use(middleware.UserAuth())
  48. {
  49. selfRoute.GET("/self/groups", controller.GetUserGroups)
  50. selfRoute.GET("/self", controller.GetSelf)
  51. selfRoute.GET("/models", controller.GetUserModels)
  52. selfRoute.PUT("/self", controller.UpdateSelf)
  53. selfRoute.DELETE("/self", controller.DeleteSelf)
  54. selfRoute.GET("/token", controller.GenerateAccessToken)
  55. selfRoute.GET("/aff", controller.GetAffCode)
  56. selfRoute.POST("/topup", middleware.CriticalRateLimit(), controller.TopUp)
  57. selfRoute.POST("/pay", middleware.CriticalRateLimit(), controller.RequestEpay)
  58. selfRoute.POST("/amount", controller.RequestAmount)
  59. selfRoute.POST("/stripe/pay", middleware.CriticalRateLimit(), controller.RequestStripePay)
  60. selfRoute.POST("/stripe/amount", controller.RequestStripeAmount)
  61. selfRoute.POST("/aff_transfer", controller.TransferAffQuota)
  62. selfRoute.PUT("/setting", controller.UpdateUserSetting)
  63. }
  64. adminRoute := userRoute.Group("/")
  65. adminRoute.Use(middleware.AdminAuth())
  66. {
  67. adminRoute.GET("/", controller.GetAllUsers)
  68. adminRoute.GET("/search", controller.SearchUsers)
  69. adminRoute.GET("/:id", controller.GetUser)
  70. adminRoute.POST("/", controller.CreateUser)
  71. adminRoute.POST("/manage", controller.ManageUser)
  72. adminRoute.PUT("/", controller.UpdateUser)
  73. adminRoute.DELETE("/:id", controller.DeleteUser)
  74. }
  75. }
  76. optionRoute := apiRouter.Group("/option")
  77. optionRoute.Use(middleware.RootAuth())
  78. {
  79. optionRoute.GET("/", controller.GetOptions)
  80. optionRoute.PUT("/", controller.UpdateOption)
  81. optionRoute.POST("/rest_model_ratio", controller.ResetModelRatio)
  82. optionRoute.POST("/migrate_console_setting", controller.MigrateConsoleSetting) // 用于迁移检测的旧键,下个版本会删除
  83. }
  84. ratioSyncRoute := apiRouter.Group("/ratio_sync")
  85. ratioSyncRoute.Use(middleware.RootAuth())
  86. {
  87. ratioSyncRoute.GET("/channels", controller.GetSyncableChannels)
  88. ratioSyncRoute.POST("/fetch", controller.FetchUpstreamRatios)
  89. }
  90. channelRoute := apiRouter.Group("/channel")
  91. channelRoute.Use(middleware.AdminAuth())
  92. {
  93. channelRoute.GET("/", controller.GetAllChannels)
  94. channelRoute.GET("/search", controller.SearchChannels)
  95. channelRoute.GET("/models", controller.ChannelListModels)
  96. channelRoute.GET("/models_enabled", controller.EnabledListModels)
  97. channelRoute.GET("/:id", controller.GetChannel)
  98. channelRoute.GET("/test", controller.TestAllChannels)
  99. channelRoute.GET("/test/:id", controller.TestChannel)
  100. channelRoute.GET("/update_balance", controller.UpdateAllChannelsBalance)
  101. channelRoute.GET("/update_balance/:id", controller.UpdateChannelBalance)
  102. channelRoute.POST("/", controller.AddChannel)
  103. channelRoute.PUT("/", controller.UpdateChannel)
  104. channelRoute.DELETE("/disabled", controller.DeleteDisabledChannel)
  105. channelRoute.POST("/tag/disabled", controller.DisableTagChannels)
  106. channelRoute.POST("/tag/enabled", controller.EnableTagChannels)
  107. channelRoute.PUT("/tag", controller.EditTagChannels)
  108. channelRoute.DELETE("/:id", controller.DeleteChannel)
  109. channelRoute.POST("/batch", controller.DeleteChannelBatch)
  110. channelRoute.POST("/fix", controller.FixChannelsAbilities)
  111. channelRoute.GET("/fetch_models/:id", controller.FetchUpstreamModels)
  112. channelRoute.POST("/fetch_models", controller.FetchModels)
  113. channelRoute.POST("/batch/tag", controller.BatchSetChannelTag)
  114. channelRoute.GET("/tag/models", controller.GetTagModels)
  115. channelRoute.POST("/copy/:id", controller.CopyChannel)
  116. }
  117. tokenRoute := apiRouter.Group("/token")
  118. tokenRoute.Use(middleware.UserAuth())
  119. {
  120. tokenRoute.GET("/", controller.GetAllTokens)
  121. tokenRoute.GET("/search", controller.SearchTokens)
  122. tokenRoute.GET("/:id", controller.GetToken)
  123. tokenRoute.POST("/", controller.AddToken)
  124. tokenRoute.PUT("/", controller.UpdateToken)
  125. tokenRoute.DELETE("/:id", controller.DeleteToken)
  126. tokenRoute.POST("/batch", controller.DeleteTokenBatch)
  127. }
  128. redemptionRoute := apiRouter.Group("/redemption")
  129. redemptionRoute.Use(middleware.AdminAuth())
  130. {
  131. redemptionRoute.GET("/", controller.GetAllRedemptions)
  132. redemptionRoute.GET("/search", controller.SearchRedemptions)
  133. redemptionRoute.GET("/:id", controller.GetRedemption)
  134. redemptionRoute.POST("/", controller.AddRedemption)
  135. redemptionRoute.PUT("/", controller.UpdateRedemption)
  136. redemptionRoute.DELETE("/invalid", controller.DeleteInvalidRedemption)
  137. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  138. }
  139. logRoute := apiRouter.Group("/log")
  140. logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
  141. logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
  142. logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
  143. logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
  144. logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
  145. logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
  146. logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
  147. dataRoute := apiRouter.Group("/data")
  148. dataRoute.GET("/", middleware.AdminAuth(), controller.GetAllQuotaDates)
  149. dataRoute.GET("/self", middleware.UserAuth(), controller.GetUserQuotaDates)
  150. logRoute.Use(middleware.CORS())
  151. {
  152. logRoute.GET("/token", controller.GetLogByKey)
  153. }
  154. // 令牌查询接口
  155. apiRouter.POST("/token/search", controller.SearchTokenByToken)
  156. groupRoute := apiRouter.Group("/group")
  157. groupRoute.Use(middleware.AdminAuth())
  158. {
  159. groupRoute.GET("/", controller.GetGroups)
  160. }
  161. mjRoute := apiRouter.Group("/mj")
  162. mjRoute.GET("/self", middleware.UserAuth(), controller.GetUserMidjourney)
  163. mjRoute.GET("/", middleware.AdminAuth(), controller.GetAllMidjourney)
  164. taskRoute := apiRouter.Group("/task")
  165. {
  166. taskRoute.GET("/self", middleware.UserAuth(), controller.GetUserTask)
  167. taskRoute.GET("/", middleware.AdminAuth(), controller.GetAllTask)
  168. }
  169. // 用量统计路由
  170. usageStatsRoute := apiRouter.Group("/usage_statistics")
  171. {
  172. usageStatsRoute.GET("/", middleware.AdminAuth(), controller.GetUsageStatistics)
  173. usageStatsRoute.GET("/summary", middleware.AdminAuth(), controller.GetUsageStatisticsSummary)
  174. usageStatsRoute.GET("/self", middleware.UserAuth(), controller.GetUserUsageStatistics)
  175. }
  176. // 月度用量统计路由
  177. monthlyUsageStatsRoute := apiRouter.Group("/usage_statistics_monthly")
  178. {
  179. monthlyUsageStatsRoute.GET("/", middleware.AdminAuth(), controller.GetMonthlyUsageStatistics)
  180. monthlyUsageStatsRoute.GET("/summary", middleware.AdminAuth(), controller.GetMonthlyUsageStatisticsSummary)
  181. monthlyUsageStatsRoute.GET("/self", middleware.UserAuth(), controller.GetUserMonthlyUsageStatistics)
  182. }
  183. }
  184. }