api-router.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package router
  2. import (
  3. "github.com/QuantumNous/new-api/controller"
  4. "github.com/QuantumNous/new-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("/user-agreement", controller.GetUserAgreement)
  21. apiRouter.GET("/privacy-policy", controller.GetPrivacyPolicy)
  22. apiRouter.GET("/about", controller.GetAbout)
  23. //apiRouter.GET("/midjourney", controller.GetMidjourney)
  24. apiRouter.GET("/home_page_content", controller.GetHomePageContent)
  25. apiRouter.GET("/pricing", middleware.TryUserAuth(), controller.GetPricing)
  26. apiRouter.GET("/verification", middleware.EmailVerificationRateLimit(), middleware.TurnstileCheck(), controller.SendEmailVerification)
  27. apiRouter.GET("/reset_password", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.SendPasswordResetEmail)
  28. apiRouter.POST("/user/reset", middleware.CriticalRateLimit(), controller.ResetPassword)
  29. apiRouter.GET("/oauth/github", middleware.CriticalRateLimit(), controller.GitHubOAuth)
  30. apiRouter.GET("/oauth/discord", middleware.CriticalRateLimit(), controller.DiscordOAuth)
  31. apiRouter.GET("/oauth/oidc", middleware.CriticalRateLimit(), controller.OidcAuth)
  32. apiRouter.GET("/oauth/linuxdo", middleware.CriticalRateLimit(), controller.LinuxdoOAuth)
  33. apiRouter.GET("/oauth/state", middleware.CriticalRateLimit(), controller.GenerateOAuthCode)
  34. apiRouter.GET("/oauth/wechat", middleware.CriticalRateLimit(), controller.WeChatAuth)
  35. apiRouter.GET("/oauth/wechat/bind", middleware.CriticalRateLimit(), controller.WeChatBind)
  36. apiRouter.GET("/oauth/email/bind", middleware.CriticalRateLimit(), controller.EmailBind)
  37. apiRouter.GET("/oauth/telegram/login", middleware.CriticalRateLimit(), controller.TelegramLogin)
  38. apiRouter.GET("/oauth/telegram/bind", middleware.CriticalRateLimit(), controller.TelegramBind)
  39. apiRouter.GET("/ratio_config", middleware.CriticalRateLimit(), controller.GetRatioConfig)
  40. apiRouter.POST("/stripe/webhook", controller.StripeWebhook)
  41. apiRouter.POST("/creem/webhook", controller.CreemWebhook)
  42. // Universal secure verification routes
  43. apiRouter.POST("/verify", middleware.UserAuth(), middleware.CriticalRateLimit(), controller.UniversalVerify)
  44. apiRouter.GET("/verify/status", middleware.UserAuth(), controller.GetVerificationStatus)
  45. userRoute := apiRouter.Group("/user")
  46. {
  47. userRoute.POST("/register", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Register)
  48. userRoute.POST("/login", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Login)
  49. userRoute.POST("/login/2fa", middleware.CriticalRateLimit(), controller.Verify2FALogin)
  50. userRoute.POST("/passkey/login/begin", middleware.CriticalRateLimit(), controller.PasskeyLoginBegin)
  51. userRoute.POST("/passkey/login/finish", middleware.CriticalRateLimit(), controller.PasskeyLoginFinish)
  52. //userRoute.POST("/tokenlog", middleware.CriticalRateLimit(), controller.TokenLog)
  53. userRoute.GET("/logout", controller.Logout)
  54. userRoute.GET("/epay/notify", controller.EpayNotify)
  55. userRoute.GET("/groups", controller.GetUserGroups)
  56. selfRoute := userRoute.Group("/")
  57. selfRoute.Use(middleware.UserAuth())
  58. {
  59. selfRoute.GET("/self/groups", controller.GetUserGroups)
  60. selfRoute.GET("/self", controller.GetSelf)
  61. selfRoute.GET("/models", controller.GetUserModels)
  62. selfRoute.PUT("/self", controller.UpdateSelf)
  63. selfRoute.DELETE("/self", controller.DeleteSelf)
  64. selfRoute.GET("/token", controller.GenerateAccessToken)
  65. selfRoute.GET("/passkey", controller.PasskeyStatus)
  66. selfRoute.POST("/passkey/register/begin", controller.PasskeyRegisterBegin)
  67. selfRoute.POST("/passkey/register/finish", controller.PasskeyRegisterFinish)
  68. selfRoute.POST("/passkey/verify/begin", controller.PasskeyVerifyBegin)
  69. selfRoute.POST("/passkey/verify/finish", controller.PasskeyVerifyFinish)
  70. selfRoute.DELETE("/passkey", controller.PasskeyDelete)
  71. selfRoute.GET("/aff", controller.GetAffCode)
  72. selfRoute.GET("/topup/info", controller.GetTopUpInfo)
  73. selfRoute.GET("/topup/self", controller.GetUserTopUps)
  74. selfRoute.POST("/topup", middleware.CriticalRateLimit(), controller.TopUp)
  75. selfRoute.POST("/pay", middleware.CriticalRateLimit(), controller.RequestEpay)
  76. selfRoute.POST("/amount", controller.RequestAmount)
  77. selfRoute.POST("/stripe/pay", middleware.CriticalRateLimit(), controller.RequestStripePay)
  78. selfRoute.POST("/stripe/amount", controller.RequestStripeAmount)
  79. selfRoute.POST("/creem/pay", middleware.CriticalRateLimit(), controller.RequestCreemPay)
  80. selfRoute.POST("/aff_transfer", controller.TransferAffQuota)
  81. selfRoute.PUT("/setting", controller.UpdateUserSetting)
  82. // 2FA routes
  83. selfRoute.GET("/2fa/status", controller.Get2FAStatus)
  84. selfRoute.POST("/2fa/setup", controller.Setup2FA)
  85. selfRoute.POST("/2fa/enable", controller.Enable2FA)
  86. selfRoute.POST("/2fa/disable", controller.Disable2FA)
  87. selfRoute.POST("/2fa/backup_codes", controller.RegenerateBackupCodes)
  88. }
  89. adminRoute := userRoute.Group("/")
  90. adminRoute.Use(middleware.AdminAuth())
  91. {
  92. adminRoute.GET("/", controller.GetAllUsers)
  93. adminRoute.GET("/topup", controller.GetAllTopUps)
  94. adminRoute.POST("/topup/complete", controller.AdminCompleteTopUp)
  95. adminRoute.GET("/search", controller.SearchUsers)
  96. adminRoute.GET("/:id", controller.GetUser)
  97. adminRoute.POST("/", controller.CreateUser)
  98. adminRoute.POST("/manage", controller.ManageUser)
  99. adminRoute.PUT("/", controller.UpdateUser)
  100. adminRoute.DELETE("/:id", controller.DeleteUser)
  101. adminRoute.DELETE("/:id/reset_passkey", controller.AdminResetPasskey)
  102. // Admin 2FA routes
  103. adminRoute.GET("/2fa/stats", controller.Admin2FAStats)
  104. adminRoute.DELETE("/:id/2fa", controller.AdminDisable2FA)
  105. }
  106. }
  107. optionRoute := apiRouter.Group("/option")
  108. optionRoute.Use(middleware.RootAuth())
  109. {
  110. optionRoute.GET("/", controller.GetOptions)
  111. optionRoute.PUT("/", controller.UpdateOption)
  112. optionRoute.POST("/rest_model_ratio", controller.ResetModelRatio)
  113. optionRoute.POST("/migrate_console_setting", controller.MigrateConsoleSetting) // 用于迁移检测的旧键,下个版本会删除
  114. }
  115. ratioSyncRoute := apiRouter.Group("/ratio_sync")
  116. ratioSyncRoute.Use(middleware.RootAuth())
  117. {
  118. ratioSyncRoute.GET("/channels", controller.GetSyncableChannels)
  119. ratioSyncRoute.POST("/fetch", controller.FetchUpstreamRatios)
  120. }
  121. channelRoute := apiRouter.Group("/channel")
  122. channelRoute.Use(middleware.AdminAuth())
  123. {
  124. channelRoute.GET("/", controller.GetAllChannels)
  125. channelRoute.GET("/search", controller.SearchChannels)
  126. channelRoute.GET("/models", controller.ChannelListModels)
  127. channelRoute.GET("/models_enabled", controller.EnabledListModels)
  128. channelRoute.GET("/:id", controller.GetChannel)
  129. channelRoute.POST("/:id/key", middleware.RootAuth(), middleware.CriticalRateLimit(), middleware.DisableCache(), middleware.SecureVerificationRequired(), controller.GetChannelKey)
  130. channelRoute.GET("/test", controller.TestAllChannels)
  131. channelRoute.GET("/test/:id", controller.TestChannel)
  132. channelRoute.GET("/update_balance", controller.UpdateAllChannelsBalance)
  133. channelRoute.GET("/update_balance/:id", controller.UpdateChannelBalance)
  134. channelRoute.POST("/", controller.AddChannel)
  135. channelRoute.PUT("/", controller.UpdateChannel)
  136. channelRoute.DELETE("/disabled", controller.DeleteDisabledChannel)
  137. channelRoute.POST("/tag/disabled", controller.DisableTagChannels)
  138. channelRoute.POST("/tag/enabled", controller.EnableTagChannels)
  139. channelRoute.PUT("/tag", controller.EditTagChannels)
  140. channelRoute.DELETE("/:id", controller.DeleteChannel)
  141. channelRoute.POST("/batch", controller.DeleteChannelBatch)
  142. channelRoute.POST("/fix", controller.FixChannelsAbilities)
  143. channelRoute.GET("/fetch_models/:id", controller.FetchUpstreamModels)
  144. channelRoute.POST("/fetch_models", controller.FetchModels)
  145. channelRoute.POST("/ollama/pull", controller.OllamaPullModel)
  146. channelRoute.POST("/ollama/pull/stream", controller.OllamaPullModelStream)
  147. channelRoute.DELETE("/ollama/delete", controller.OllamaDeleteModel)
  148. channelRoute.GET("/ollama/version/:id", controller.OllamaVersion)
  149. channelRoute.POST("/batch/tag", controller.BatchSetChannelTag)
  150. channelRoute.GET("/tag/models", controller.GetTagModels)
  151. channelRoute.POST("/copy/:id", controller.CopyChannel)
  152. channelRoute.POST("/multi_key/manage", controller.ManageMultiKeys)
  153. }
  154. tokenRoute := apiRouter.Group("/token")
  155. tokenRoute.Use(middleware.UserAuth())
  156. {
  157. tokenRoute.GET("/", controller.GetAllTokens)
  158. tokenRoute.GET("/search", controller.SearchTokens)
  159. tokenRoute.GET("/:id", controller.GetToken)
  160. tokenRoute.POST("/", controller.AddToken)
  161. tokenRoute.PUT("/", controller.UpdateToken)
  162. tokenRoute.DELETE("/:id", controller.DeleteToken)
  163. tokenRoute.POST("/batch", controller.DeleteTokenBatch)
  164. }
  165. usageRoute := apiRouter.Group("/usage")
  166. usageRoute.Use(middleware.CriticalRateLimit())
  167. {
  168. tokenUsageRoute := usageRoute.Group("/token")
  169. tokenUsageRoute.Use(middleware.TokenAuth())
  170. {
  171. tokenUsageRoute.GET("/", controller.GetTokenUsage)
  172. }
  173. }
  174. redemptionRoute := apiRouter.Group("/redemption")
  175. redemptionRoute.Use(middleware.AdminAuth())
  176. {
  177. redemptionRoute.GET("/", controller.GetAllRedemptions)
  178. redemptionRoute.GET("/search", controller.SearchRedemptions)
  179. redemptionRoute.GET("/:id", controller.GetRedemption)
  180. redemptionRoute.POST("/", controller.AddRedemption)
  181. redemptionRoute.PUT("/", controller.UpdateRedemption)
  182. redemptionRoute.DELETE("/invalid", controller.DeleteInvalidRedemption)
  183. redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
  184. }
  185. logRoute := apiRouter.Group("/log")
  186. logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
  187. logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
  188. logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
  189. logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
  190. logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
  191. logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
  192. logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
  193. dataRoute := apiRouter.Group("/data")
  194. dataRoute.GET("/", middleware.AdminAuth(), controller.GetAllQuotaDates)
  195. dataRoute.GET("/self", middleware.UserAuth(), controller.GetUserQuotaDates)
  196. logRoute.Use(middleware.CORS())
  197. {
  198. logRoute.GET("/token", controller.GetLogByKey)
  199. }
  200. groupRoute := apiRouter.Group("/group")
  201. groupRoute.Use(middleware.AdminAuth())
  202. {
  203. groupRoute.GET("/", controller.GetGroups)
  204. }
  205. prefillGroupRoute := apiRouter.Group("/prefill_group")
  206. prefillGroupRoute.Use(middleware.AdminAuth())
  207. {
  208. prefillGroupRoute.GET("/", controller.GetPrefillGroups)
  209. prefillGroupRoute.POST("/", controller.CreatePrefillGroup)
  210. prefillGroupRoute.PUT("/", controller.UpdatePrefillGroup)
  211. prefillGroupRoute.DELETE("/:id", controller.DeletePrefillGroup)
  212. }
  213. mjRoute := apiRouter.Group("/mj")
  214. mjRoute.GET("/self", middleware.UserAuth(), controller.GetUserMidjourney)
  215. mjRoute.GET("/", middleware.AdminAuth(), controller.GetAllMidjourney)
  216. taskRoute := apiRouter.Group("/task")
  217. {
  218. taskRoute.GET("/self", middleware.UserAuth(), controller.GetUserTask)
  219. taskRoute.GET("/", middleware.AdminAuth(), controller.GetAllTask)
  220. }
  221. vendorRoute := apiRouter.Group("/vendors")
  222. vendorRoute.Use(middleware.AdminAuth())
  223. {
  224. vendorRoute.GET("/", controller.GetAllVendors)
  225. vendorRoute.GET("/search", controller.SearchVendors)
  226. vendorRoute.GET("/:id", controller.GetVendorMeta)
  227. vendorRoute.POST("/", controller.CreateVendorMeta)
  228. vendorRoute.PUT("/", controller.UpdateVendorMeta)
  229. vendorRoute.DELETE("/:id", controller.DeleteVendorMeta)
  230. }
  231. modelsRoute := apiRouter.Group("/models")
  232. modelsRoute.Use(middleware.AdminAuth())
  233. {
  234. modelsRoute.GET("/sync_upstream/preview", controller.SyncUpstreamPreview)
  235. modelsRoute.POST("/sync_upstream", controller.SyncUpstreamModels)
  236. modelsRoute.GET("/missing", controller.GetMissingModels)
  237. modelsRoute.GET("/", controller.GetAllModelsMeta)
  238. modelsRoute.GET("/search", controller.SearchModelsMeta)
  239. modelsRoute.GET("/:id", controller.GetModelMeta)
  240. modelsRoute.POST("/", controller.CreateModelMeta)
  241. modelsRoute.PUT("/", controller.UpdateModelMeta)
  242. modelsRoute.DELETE("/:id", controller.DeleteModelMeta)
  243. }
  244. // Deployments (model deployment management)
  245. deploymentsRoute := apiRouter.Group("/deployments")
  246. deploymentsRoute.Use(middleware.AdminAuth())
  247. {
  248. // List and search deployments
  249. deploymentsRoute.GET("/", controller.GetAllDeployments)
  250. deploymentsRoute.GET("/search", controller.SearchDeployments)
  251. // Connection utilities
  252. deploymentsRoute.POST("/test-connection", controller.TestIoNetConnection)
  253. // Resource and configuration endpoints
  254. deploymentsRoute.GET("/hardware-types", controller.GetHardwareTypes)
  255. deploymentsRoute.GET("/locations", controller.GetLocations)
  256. deploymentsRoute.GET("/available-replicas", controller.GetAvailableReplicas)
  257. deploymentsRoute.POST("/price-estimation", controller.GetPriceEstimation)
  258. deploymentsRoute.GET("/check-name", controller.CheckClusterNameAvailability)
  259. // Create new deployment
  260. deploymentsRoute.POST("/", controller.CreateDeployment)
  261. // Individual deployment operations
  262. deploymentsRoute.GET("/:id", controller.GetDeployment)
  263. deploymentsRoute.GET("/:id/logs", controller.GetDeploymentLogs)
  264. deploymentsRoute.GET("/:id/containers", controller.ListDeploymentContainers)
  265. deploymentsRoute.GET("/:id/containers/:container_id", controller.GetContainerDetails)
  266. deploymentsRoute.PUT("/:id", controller.UpdateDeployment)
  267. deploymentsRoute.PUT("/:id/name", controller.UpdateDeploymentName)
  268. deploymentsRoute.POST("/:id/extend", controller.ExtendDeployment)
  269. deploymentsRoute.DELETE("/:id", controller.DeleteDeployment)
  270. // Future batch operations:
  271. // deploymentsRoute.POST("/:id/start", controller.StartDeployment)
  272. // deploymentsRoute.POST("/:id/stop", controller.StopDeployment)
  273. // deploymentsRoute.POST("/:id/restart", controller.RestartDeployment)
  274. // deploymentsRoute.POST("/batch_delete", controller.BatchDeleteDeployments)
  275. // deploymentsRoute.POST("/batch_start", controller.BatchStartDeployments)
  276. // deploymentsRoute.POST("/batch_stop", controller.BatchStopDeployments)
  277. }
  278. }
  279. }