api-router.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. channelRoute.GET("/export", controller.ExportChannels)
  117. channelRoute.POST("/import", controller.ImportChannels)
  118. }
  119. tokenRoute := apiRouter.Group("/token")
  120. tokenRoute.Use(middleware.UserAuth())
  121. {
  122. tokenRoute.GET("/", controller.GetAllTokens)
  123. tokenRoute.GET("/search", controller.SearchTokens)
  124. tokenRoute.GET("/tags", controller.GetTokenTags)
  125. tokenRoute.GET("/:id", controller.GetToken)
  126. tokenRoute.POST("/", controller.AddToken)
  127. tokenRoute.PUT("/", controller.UpdateToken)
  128. tokenRoute.DELETE("/:id", controller.DeleteToken)
  129. tokenRoute.POST("/batch", controller.DeleteTokenBatch)
  130. }
  131. redemptionRoute := apiRouter.Group("/redemption")
  132. redemptionRoute.Use(middleware.AdminAuth())
  133. {
  134. redemptionRoute.GET("/", controller.GetAllRedemptions)
  135. redemptionRoute.GET("/search", controller.SearchRedemptions)
  136. redemptionRoute.GET("/:id", controller.GetRedemption)
  137. redemptionRoute.POST("/", controller.AddRedemption)
  138. redemptionRoute.PUT("/", controller.UpdateRedemption)
  139. redemptionRoute.DELETE("/invalid", controller.DeleteInvalidRedemption)
  140. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  141. }
  142. logRoute := apiRouter.Group("/log")
  143. logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
  144. logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
  145. logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
  146. logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
  147. logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
  148. logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
  149. logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
  150. dataRoute := apiRouter.Group("/data")
  151. dataRoute.GET("/", middleware.AdminAuth(), controller.GetAllQuotaDates)
  152. dataRoute.GET("/self", middleware.UserAuth(), controller.GetUserQuotaDates)
  153. logRoute.Use(middleware.CORS())
  154. {
  155. logRoute.GET("/token", controller.GetLogByKey)
  156. }
  157. // 令牌查询接口
  158. apiRouter.POST("/token/search", controller.SearchTokenByToken)
  159. groupRoute := apiRouter.Group("/group")
  160. groupRoute.Use(middleware.AdminAuth())
  161. {
  162. groupRoute.GET("/", controller.GetGroups)
  163. }
  164. mjRoute := apiRouter.Group("/mj")
  165. mjRoute.GET("/self", middleware.UserAuth(), controller.GetUserMidjourney)
  166. mjRoute.GET("/", middleware.AdminAuth(), controller.GetAllMidjourney)
  167. taskRoute := apiRouter.Group("/task")
  168. {
  169. taskRoute.GET("/self", middleware.UserAuth(), controller.GetUserTask)
  170. taskRoute.GET("/", middleware.AdminAuth(), controller.GetAllTask)
  171. }
  172. // 用量统计路由
  173. usageStatsRoute := apiRouter.Group("/usage_statistics")
  174. {
  175. usageStatsRoute.GET("/", middleware.AdminAuth(), controller.GetUsageStatistics)
  176. usageStatsRoute.GET("/summary", middleware.AdminAuth(), controller.GetUsageStatisticsSummary)
  177. usageStatsRoute.GET("/self", middleware.UserAuth(), controller.GetUserUsageStatistics)
  178. }
  179. // 月度用量统计路由
  180. monthlyUsageStatsRoute := apiRouter.Group("/usage_statistics_monthly")
  181. {
  182. monthlyUsageStatsRoute.GET("/", middleware.AdminAuth(), controller.GetMonthlyUsageStatistics)
  183. monthlyUsageStatsRoute.GET("/summary", middleware.AdminAuth(), controller.GetMonthlyUsageStatisticsSummary)
  184. monthlyUsageStatsRoute.GET("/self", middleware.UserAuth(), controller.GetUserMonthlyUsageStatistics)
  185. }
  186. // 统计图表路由
  187. statisticsRoute := apiRouter.Group("/statistics")
  188. {
  189. statisticsRoute.GET("/channel", middleware.AdminAuth(), controller.GetChannelStatistics)
  190. statisticsRoute.GET("/token", middleware.AdminAuth(), controller.GetTokenStatistics)
  191. statisticsRoute.GET("/user", middleware.AdminAuth(), controller.GetUserStatistics)
  192. }
  193. }
  194. }