api-router.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.EmailVerificationRateLimit(), 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. // Universal secure verification routes
  39. apiRouter.POST("/verify", middleware.UserAuth(), middleware.CriticalRateLimit(), controller.UniversalVerify)
  40. apiRouter.GET("/verify/status", middleware.UserAuth(), controller.GetVerificationStatus)
  41. userRoute := apiRouter.Group("/user")
  42. {
  43. userRoute.POST("/register", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Register)
  44. userRoute.POST("/login", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Login)
  45. userRoute.POST("/login/2fa", middleware.CriticalRateLimit(), controller.Verify2FALogin)
  46. userRoute.POST("/passkey/login/begin", middleware.CriticalRateLimit(), controller.PasskeyLoginBegin)
  47. userRoute.POST("/passkey/login/finish", middleware.CriticalRateLimit(), controller.PasskeyLoginFinish)
  48. //userRoute.POST("/tokenlog", middleware.CriticalRateLimit(), controller.TokenLog)
  49. userRoute.GET("/logout", controller.Logout)
  50. userRoute.GET("/epay/notify", controller.EpayNotify)
  51. userRoute.GET("/groups", controller.GetUserGroups)
  52. selfRoute := userRoute.Group("/")
  53. selfRoute.Use(middleware.UserAuth())
  54. {
  55. selfRoute.GET("/self/groups", controller.GetUserGroups)
  56. selfRoute.GET("/self", controller.GetSelf)
  57. selfRoute.GET("/models", controller.GetUserModels)
  58. selfRoute.PUT("/self", controller.UpdateSelf)
  59. selfRoute.DELETE("/self", controller.DeleteSelf)
  60. selfRoute.GET("/token", controller.GenerateAccessToken)
  61. selfRoute.GET("/passkey", controller.PasskeyStatus)
  62. selfRoute.POST("/passkey/register/begin", controller.PasskeyRegisterBegin)
  63. selfRoute.POST("/passkey/register/finish", controller.PasskeyRegisterFinish)
  64. selfRoute.POST("/passkey/verify/begin", controller.PasskeyVerifyBegin)
  65. selfRoute.POST("/passkey/verify/finish", controller.PasskeyVerifyFinish)
  66. selfRoute.DELETE("/passkey", controller.PasskeyDelete)
  67. selfRoute.GET("/aff", controller.GetAffCode)
  68. selfRoute.GET("/topup/info", controller.GetTopUpInfo)
  69. selfRoute.GET("/topup/self", controller.GetUserTopUps)
  70. selfRoute.POST("/topup", middleware.CriticalRateLimit(), controller.TopUp)
  71. selfRoute.POST("/pay", middleware.CriticalRateLimit(), controller.RequestEpay)
  72. selfRoute.POST("/amount", controller.RequestAmount)
  73. selfRoute.POST("/stripe/pay", middleware.CriticalRateLimit(), controller.RequestStripePay)
  74. selfRoute.POST("/stripe/amount", controller.RequestStripeAmount)
  75. selfRoute.POST("/aff_transfer", controller.TransferAffQuota)
  76. selfRoute.POST("/topup/complete", middleware.AdminAuth(), controller.AdminCompleteTopUp)
  77. selfRoute.PUT("/setting", controller.UpdateUserSetting)
  78. // 2FA routes
  79. selfRoute.GET("/2fa/status", controller.Get2FAStatus)
  80. selfRoute.POST("/2fa/setup", controller.Setup2FA)
  81. selfRoute.POST("/2fa/enable", controller.Enable2FA)
  82. selfRoute.POST("/2fa/disable", controller.Disable2FA)
  83. selfRoute.POST("/2fa/backup_codes", controller.RegenerateBackupCodes)
  84. }
  85. adminRoute := userRoute.Group("/")
  86. adminRoute.Use(middleware.AdminAuth())
  87. {
  88. adminRoute.GET("/", controller.GetAllUsers)
  89. adminRoute.GET("/search", controller.SearchUsers)
  90. adminRoute.GET("/:id", controller.GetUser)
  91. adminRoute.POST("/", controller.CreateUser)
  92. adminRoute.POST("/manage", controller.ManageUser)
  93. adminRoute.PUT("/", controller.UpdateUser)
  94. adminRoute.DELETE("/:id", controller.DeleteUser)
  95. adminRoute.DELETE("/:id/reset_passkey", controller.AdminResetPasskey)
  96. // Admin 2FA routes
  97. adminRoute.GET("/2fa/stats", controller.Admin2FAStats)
  98. adminRoute.DELETE("/:id/2fa", controller.AdminDisable2FA)
  99. }
  100. }
  101. optionRoute := apiRouter.Group("/option")
  102. optionRoute.Use(middleware.RootAuth())
  103. {
  104. optionRoute.GET("/", controller.GetOptions)
  105. optionRoute.PUT("/", controller.UpdateOption)
  106. optionRoute.POST("/rest_model_ratio", controller.ResetModelRatio)
  107. optionRoute.POST("/migrate_console_setting", controller.MigrateConsoleSetting) // 用于迁移检测的旧键,下个版本会删除
  108. }
  109. ratioSyncRoute := apiRouter.Group("/ratio_sync")
  110. ratioSyncRoute.Use(middleware.RootAuth())
  111. {
  112. ratioSyncRoute.GET("/channels", controller.GetSyncableChannels)
  113. ratioSyncRoute.POST("/fetch", controller.FetchUpstreamRatios)
  114. }
  115. channelRoute := apiRouter.Group("/channel")
  116. channelRoute.Use(middleware.AdminAuth())
  117. {
  118. channelRoute.GET("/", controller.GetAllChannels)
  119. channelRoute.GET("/search", controller.SearchChannels)
  120. channelRoute.GET("/models", controller.ChannelListModels)
  121. channelRoute.GET("/models_enabled", controller.EnabledListModels)
  122. channelRoute.GET("/:id", controller.GetChannel)
  123. channelRoute.POST("/:id/key", middleware.CriticalRateLimit(), middleware.DisableCache(), middleware.SecureVerificationRequired(), controller.GetChannelKey)
  124. channelRoute.GET("/test", controller.TestAllChannels)
  125. channelRoute.GET("/test/:id", controller.TestChannel)
  126. channelRoute.GET("/update_balance", controller.UpdateAllChannelsBalance)
  127. channelRoute.GET("/update_balance/:id", controller.UpdateChannelBalance)
  128. channelRoute.POST("/", controller.AddChannel)
  129. channelRoute.PUT("/", controller.UpdateChannel)
  130. channelRoute.DELETE("/disabled", controller.DeleteDisabledChannel)
  131. channelRoute.POST("/tag/disabled", controller.DisableTagChannels)
  132. channelRoute.POST("/tag/enabled", controller.EnableTagChannels)
  133. channelRoute.PUT("/tag", controller.EditTagChannels)
  134. channelRoute.DELETE("/:id", controller.DeleteChannel)
  135. channelRoute.POST("/batch", controller.DeleteChannelBatch)
  136. channelRoute.POST("/fix", controller.FixChannelsAbilities)
  137. channelRoute.GET("/fetch_models/:id", controller.FetchUpstreamModels)
  138. channelRoute.POST("/fetch_models", controller.FetchModels)
  139. channelRoute.POST("/batch/tag", controller.BatchSetChannelTag)
  140. channelRoute.GET("/tag/models", controller.GetTagModels)
  141. channelRoute.POST("/copy/:id", controller.CopyChannel)
  142. channelRoute.POST("/multi_key/manage", controller.ManageMultiKeys)
  143. }
  144. tokenRoute := apiRouter.Group("/token")
  145. tokenRoute.Use(middleware.UserAuth())
  146. {
  147. tokenRoute.GET("/", controller.GetAllTokens)
  148. tokenRoute.GET("/search", controller.SearchTokens)
  149. tokenRoute.GET("/:id", controller.GetToken)
  150. tokenRoute.POST("/", controller.AddToken)
  151. tokenRoute.PUT("/", controller.UpdateToken)
  152. tokenRoute.DELETE("/:id", controller.DeleteToken)
  153. tokenRoute.POST("/batch", controller.DeleteTokenBatch)
  154. }
  155. usageRoute := apiRouter.Group("/usage")
  156. usageRoute.Use(middleware.CriticalRateLimit())
  157. {
  158. tokenUsageRoute := usageRoute.Group("/token")
  159. tokenUsageRoute.Use(middleware.TokenAuth())
  160. {
  161. tokenUsageRoute.GET("/", controller.GetTokenUsage)
  162. }
  163. }
  164. redemptionRoute := apiRouter.Group("/redemption")
  165. redemptionRoute.Use(middleware.AdminAuth())
  166. {
  167. redemptionRoute.GET("/", controller.GetAllRedemptions)
  168. redemptionRoute.GET("/search", controller.SearchRedemptions)
  169. redemptionRoute.GET("/:id", controller.GetRedemption)
  170. redemptionRoute.POST("/", controller.AddRedemption)
  171. redemptionRoute.PUT("/", controller.UpdateRedemption)
  172. redemptionRoute.DELETE("/invalid", controller.DeleteInvalidRedemption)
  173. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  174. }
  175. logRoute := apiRouter.Group("/log")
  176. logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
  177. logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
  178. logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
  179. logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
  180. logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
  181. logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
  182. logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
  183. dataRoute := apiRouter.Group("/data")
  184. dataRoute.GET("/", middleware.AdminAuth(), controller.GetAllQuotaDates)
  185. dataRoute.GET("/self", middleware.UserAuth(), controller.GetUserQuotaDates)
  186. logRoute.Use(middleware.CORS())
  187. {
  188. logRoute.GET("/token", controller.GetLogByKey)
  189. }
  190. groupRoute := apiRouter.Group("/group")
  191. groupRoute.Use(middleware.AdminAuth())
  192. {
  193. groupRoute.GET("/", controller.GetGroups)
  194. }
  195. prefillGroupRoute := apiRouter.Group("/prefill_group")
  196. prefillGroupRoute.Use(middleware.AdminAuth())
  197. {
  198. prefillGroupRoute.GET("/", controller.GetPrefillGroups)
  199. prefillGroupRoute.POST("/", controller.CreatePrefillGroup)
  200. prefillGroupRoute.PUT("/", controller.UpdatePrefillGroup)
  201. prefillGroupRoute.DELETE("/:id", controller.DeletePrefillGroup)
  202. }
  203. mjRoute := apiRouter.Group("/mj")
  204. mjRoute.GET("/self", middleware.UserAuth(), controller.GetUserMidjourney)
  205. mjRoute.GET("/", middleware.AdminAuth(), controller.GetAllMidjourney)
  206. taskRoute := apiRouter.Group("/task")
  207. {
  208. taskRoute.GET("/self", middleware.UserAuth(), controller.GetUserTask)
  209. taskRoute.GET("/", middleware.AdminAuth(), controller.GetAllTask)
  210. }
  211. vendorRoute := apiRouter.Group("/vendors")
  212. vendorRoute.Use(middleware.AdminAuth())
  213. {
  214. vendorRoute.GET("/", controller.GetAllVendors)
  215. vendorRoute.GET("/search", controller.SearchVendors)
  216. vendorRoute.GET("/:id", controller.GetVendorMeta)
  217. vendorRoute.POST("/", controller.CreateVendorMeta)
  218. vendorRoute.PUT("/", controller.UpdateVendorMeta)
  219. vendorRoute.DELETE("/:id", controller.DeleteVendorMeta)
  220. }
  221. modelsRoute := apiRouter.Group("/models")
  222. modelsRoute.Use(middleware.AdminAuth())
  223. {
  224. modelsRoute.GET("/sync_upstream/preview", controller.SyncUpstreamPreview)
  225. modelsRoute.POST("/sync_upstream", controller.SyncUpstreamModels)
  226. modelsRoute.GET("/missing", controller.GetMissingModels)
  227. modelsRoute.GET("/", controller.GetAllModelsMeta)
  228. modelsRoute.GET("/search", controller.SearchModelsMeta)
  229. modelsRoute.GET("/:id", controller.GetModelMeta)
  230. modelsRoute.POST("/", controller.CreateModelMeta)
  231. modelsRoute.PUT("/", controller.UpdateModelMeta)
  232. modelsRoute.DELETE("/:id", controller.DeleteModelMeta)
  233. }
  234. }
  235. }