router.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package httpapi
  2. import (
  3. "net/http"
  4. "cmr-backend/internal/httpapi/handlers"
  5. "cmr-backend/internal/httpapi/middleware"
  6. "cmr-backend/internal/platform/jwtx"
  7. "cmr-backend/internal/service"
  8. )
  9. func NewRouter(
  10. appEnv string,
  11. jwtManager *jwtx.Manager,
  12. authService *service.AuthService,
  13. opsAuthService *service.OpsAuthService,
  14. opsSummaryService *service.OpsSummaryService,
  15. entryService *service.EntryService,
  16. entryHomeService *service.EntryHomeService,
  17. adminAssetService *service.AdminAssetService,
  18. adminResourceService *service.AdminResourceService,
  19. adminProductionService *service.AdminProductionService,
  20. adminEventService *service.AdminEventService,
  21. adminPipelineService *service.AdminPipelineService,
  22. eventService *service.EventService,
  23. eventPlayService *service.EventPlayService,
  24. publicExperienceService *service.PublicExperienceService,
  25. configService *service.ConfigService,
  26. homeService *service.HomeService,
  27. mapExperienceService *service.MapExperienceService,
  28. profileService *service.ProfileService,
  29. resultService *service.ResultService,
  30. sessionService *service.SessionService,
  31. devService *service.DevService,
  32. meService *service.MeService,
  33. ) http.Handler {
  34. mux := http.NewServeMux()
  35. healthHandler := handlers.NewHealthHandler()
  36. authHandler := handlers.NewAuthHandler(authService)
  37. opsAuthHandler := handlers.NewOpsAuthHandler(opsAuthService)
  38. opsSummaryHandler := handlers.NewOpsSummaryHandler(opsSummaryService)
  39. regionOptionsHandler := handlers.NewRegionOptionsHandler()
  40. entryHandler := handlers.NewEntryHandler(entryService)
  41. entryHomeHandler := handlers.NewEntryHomeHandler(entryHomeService)
  42. adminAssetHandler := handlers.NewAdminAssetHandler(adminAssetService)
  43. adminResourceHandler := handlers.NewAdminResourceHandler(adminResourceService)
  44. adminProductionHandler := handlers.NewAdminProductionHandler(adminProductionService)
  45. adminEventHandler := handlers.NewAdminEventHandler(adminEventService)
  46. adminPipelineHandler := handlers.NewAdminPipelineHandler(adminPipelineService)
  47. eventHandler := handlers.NewEventHandler(eventService)
  48. eventPlayHandler := handlers.NewEventPlayHandler(eventPlayService)
  49. publicExperienceHandler := handlers.NewPublicExperienceHandler(publicExperienceService)
  50. configHandler := handlers.NewConfigHandler(configService)
  51. homeHandler := handlers.NewHomeHandler(homeService)
  52. mapExperienceHandler := handlers.NewMapExperienceHandler(mapExperienceService)
  53. profileHandler := handlers.NewProfileHandler(profileService)
  54. resultHandler := handlers.NewResultHandler(resultService)
  55. sessionHandler := handlers.NewSessionHandler(sessionService)
  56. devHandler := handlers.NewDevHandler(devService)
  57. opsWorkbenchHandler := handlers.NewOpsWorkbenchHandler()
  58. meHandler := handlers.NewMeHandler(meService)
  59. authMiddleware := middleware.NewAuthMiddleware(jwtManager)
  60. opsAuthMiddleware := middleware.NewOpsAuthMiddleware(jwtManager, appEnv)
  61. mux.HandleFunc("GET /healthz", healthHandler.Get)
  62. mux.HandleFunc("GET /home", homeHandler.GetHome)
  63. mux.HandleFunc("GET /cards", homeHandler.GetCards)
  64. mux.HandleFunc("GET /experience-maps", mapExperienceHandler.ListMaps)
  65. mux.HandleFunc("GET /experience-maps/{mapAssetPublicID}", mapExperienceHandler.GetMapDetail)
  66. mux.HandleFunc("GET /public/experience-maps", publicExperienceHandler.ListMaps)
  67. mux.HandleFunc("GET /public/experience-maps/{mapAssetPublicID}", publicExperienceHandler.GetMapDetail)
  68. mux.HandleFunc("GET /entry/resolve", entryHandler.Resolve)
  69. mux.Handle("GET /admin/assets", authMiddleware(http.HandlerFunc(adminAssetHandler.ListAssets)))
  70. mux.Handle("POST /admin/assets/register-link", authMiddleware(http.HandlerFunc(adminAssetHandler.RegisterLink)))
  71. mux.Handle("POST /admin/assets/upload", authMiddleware(http.HandlerFunc(adminAssetHandler.UploadFile)))
  72. mux.Handle("GET /admin/assets/{assetPublicID}", authMiddleware(http.HandlerFunc(adminAssetHandler.GetAsset)))
  73. mux.Handle("GET /admin/maps", authMiddleware(http.HandlerFunc(adminResourceHandler.ListMaps)))
  74. mux.Handle("POST /admin/maps", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateMap)))
  75. mux.Handle("GET /admin/maps/{mapPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetMap)))
  76. mux.Handle("POST /admin/maps/{mapPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateMapVersion)))
  77. mux.Handle("GET /admin/places", authMiddleware(http.HandlerFunc(adminProductionHandler.ListPlaces)))
  78. mux.Handle("POST /admin/places", authMiddleware(http.HandlerFunc(adminProductionHandler.CreatePlace)))
  79. mux.Handle("GET /admin/places/{placePublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetPlace)))
  80. mux.Handle("GET /admin/map-assets", authMiddleware(http.HandlerFunc(adminProductionHandler.ListMapAssets)))
  81. mux.Handle("POST /admin/places/{placePublicID}/map-assets", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateMapAsset)))
  82. mux.Handle("GET /admin/map-assets/{mapAssetPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetMapAsset)))
  83. mux.Handle("PUT /admin/map-assets/{mapAssetPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.UpdateMapAsset)))
  84. mux.Handle("POST /admin/map-assets/{mapAssetPublicID}/tile-releases", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateTileRelease)))
  85. mux.Handle("POST /admin/map-assets/{mapAssetPublicID}/course-sets", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseSet)))
  86. mux.Handle("GET /admin/course-sources", authMiddleware(http.HandlerFunc(adminProductionHandler.ListCourseSources)))
  87. mux.Handle("POST /admin/course-sources", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseSource)))
  88. mux.Handle("GET /admin/course-sources/{sourcePublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetCourseSource)))
  89. mux.Handle("GET /admin/course-sets/{courseSetPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetCourseSet)))
  90. mux.Handle("POST /admin/course-sets/{courseSetPublicID}/variants", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseVariant)))
  91. mux.Handle("GET /admin/runtime-bindings", authMiddleware(http.HandlerFunc(adminProductionHandler.ListRuntimeBindings)))
  92. mux.Handle("POST /admin/runtime-bindings", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateRuntimeBinding)))
  93. mux.Handle("GET /admin/runtime-bindings/{runtimeBindingPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetRuntimeBinding)))
  94. mux.Handle("POST /admin/ops/tile-releases/import", authMiddleware(http.HandlerFunc(adminProductionHandler.ImportTileRelease)))
  95. mux.Handle("POST /admin/ops/course-sets/import-kml-batch", authMiddleware(http.HandlerFunc(adminProductionHandler.ImportCourseSetKMLBatch)))
  96. mux.Handle("GET /admin/playfields", authMiddleware(http.HandlerFunc(adminResourceHandler.ListPlayfields)))
  97. mux.Handle("POST /admin/playfields", authMiddleware(http.HandlerFunc(adminResourceHandler.CreatePlayfield)))
  98. mux.Handle("GET /admin/playfields/{playfieldPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetPlayfield)))
  99. mux.Handle("POST /admin/playfields/{playfieldPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreatePlayfieldVersion)))
  100. mux.Handle("GET /admin/resource-packs", authMiddleware(http.HandlerFunc(adminResourceHandler.ListResourcePacks)))
  101. mux.Handle("POST /admin/resource-packs", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateResourcePack)))
  102. mux.Handle("GET /admin/resource-packs/{resourcePackPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetResourcePack)))
  103. mux.Handle("POST /admin/resource-packs/{resourcePackPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateResourcePackVersion)))
  104. mux.Handle("GET /admin/events", authMiddleware(http.HandlerFunc(adminEventHandler.ListEvents)))
  105. mux.Handle("POST /admin/events", authMiddleware(http.HandlerFunc(adminEventHandler.CreateEvent)))
  106. mux.Handle("GET /admin/events/{eventPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetEvent)))
  107. mux.Handle("PUT /admin/events/{eventPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.UpdateEvent)))
  108. mux.Handle("POST /admin/events/{eventPublicID}/source", authMiddleware(http.HandlerFunc(adminEventHandler.SaveSource)))
  109. mux.Handle("GET /admin/events/{eventPublicID}/presentations", authMiddleware(http.HandlerFunc(adminEventHandler.ListPresentations)))
  110. mux.Handle("POST /admin/events/{eventPublicID}/presentations", authMiddleware(http.HandlerFunc(adminEventHandler.CreatePresentation)))
  111. mux.Handle("POST /admin/events/{eventPublicID}/presentations/import", authMiddleware(http.HandlerFunc(adminEventHandler.ImportPresentation)))
  112. mux.Handle("GET /admin/presentations/{presentationPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetPresentation)))
  113. mux.Handle("GET /admin/events/{eventPublicID}/content-bundles", authMiddleware(http.HandlerFunc(adminEventHandler.ListContentBundles)))
  114. mux.Handle("POST /admin/events/{eventPublicID}/content-bundles", authMiddleware(http.HandlerFunc(adminEventHandler.CreateContentBundle)))
  115. mux.Handle("POST /admin/events/{eventPublicID}/content-bundles/import", authMiddleware(http.HandlerFunc(adminEventHandler.ImportContentBundle)))
  116. mux.Handle("GET /admin/content-bundles/{contentBundlePublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetContentBundle)))
  117. mux.Handle("POST /admin/events/{eventPublicID}/defaults", authMiddleware(http.HandlerFunc(adminEventHandler.UpdateEventDefaults)))
  118. mux.Handle("GET /admin/events/{eventPublicID}/pipeline", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetEventPipeline)))
  119. mux.Handle("POST /admin/sources/{sourceID}/build", authMiddleware(http.HandlerFunc(adminPipelineHandler.BuildSource)))
  120. mux.Handle("GET /admin/builds/{buildID}", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetBuild)))
  121. mux.Handle("POST /admin/builds/{buildID}/publish", authMiddleware(http.HandlerFunc(adminPipelineHandler.PublishBuild)))
  122. mux.Handle("GET /admin/releases/{releasePublicID}", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetRelease)))
  123. mux.Handle("POST /admin/releases/{releasePublicID}/runtime-binding", authMiddleware(http.HandlerFunc(adminPipelineHandler.BindReleaseRuntime)))
  124. mux.Handle("POST /admin/events/{eventPublicID}/rollback", authMiddleware(http.HandlerFunc(adminPipelineHandler.RollbackRelease)))
  125. if appEnv != "production" {
  126. mux.HandleFunc("GET /dev/workbench", devHandler.Workbench)
  127. mux.HandleFunc("GET /admin/ops-workbench", opsWorkbenchHandler.Get)
  128. mux.HandleFunc("POST /dev/bootstrap-demo", devHandler.BootstrapDemo)
  129. mux.HandleFunc("POST /dev/client-logs", devHandler.CreateClientLog)
  130. mux.HandleFunc("GET /dev/client-logs", devHandler.ListClientLogs)
  131. mux.HandleFunc("DELETE /dev/client-logs", devHandler.ClearClientLogs)
  132. mux.HandleFunc("GET /dev/manifest-summary", devHandler.ManifestSummary)
  133. mux.HandleFunc("GET /dev/demo-assets/manifests/{demoKey}", devHandler.DemoGameManifest)
  134. mux.HandleFunc("GET /dev/demo-assets/presentations/{demoKey}", devHandler.DemoPresentationSchema)
  135. mux.HandleFunc("GET /dev/demo-assets/content-manifests/{demoKey}", devHandler.DemoContentManifest)
  136. mux.HandleFunc("GET /dev/config/local-files", configHandler.ListLocalFiles)
  137. mux.HandleFunc("POST /dev/events/{eventPublicID}/config-sources/import-local", configHandler.ImportLocal)
  138. mux.HandleFunc("POST /dev/config-builds/preview", configHandler.BuildPreview)
  139. mux.HandleFunc("POST /dev/config-builds/publish", configHandler.PublishBuild)
  140. }
  141. mux.Handle("GET /me/entry-home", authMiddleware(http.HandlerFunc(entryHomeHandler.Get)))
  142. mux.Handle("GET /me/profile", authMiddleware(http.HandlerFunc(profileHandler.Get)))
  143. mux.HandleFunc("GET /events/{eventPublicID}", eventHandler.GetDetail)
  144. mux.HandleFunc("GET /public/events/{eventPublicID}", publicExperienceHandler.GetEventDetail)
  145. mux.HandleFunc("GET /public/events/{eventPublicID}/play", publicExperienceHandler.GetEventPlay)
  146. mux.HandleFunc("POST /public/events/{eventPublicID}/launch", publicExperienceHandler.Launch)
  147. mux.Handle("GET /events/{eventPublicID}/play", authMiddleware(http.HandlerFunc(eventPlayHandler.Get)))
  148. mux.Handle("GET /events/{eventPublicID}/config-sources", authMiddleware(http.HandlerFunc(configHandler.ListSources)))
  149. mux.Handle("POST /events/{eventPublicID}/launch", authMiddleware(http.HandlerFunc(eventHandler.Launch)))
  150. mux.Handle("GET /config-sources/{sourceID}", authMiddleware(http.HandlerFunc(configHandler.GetSource)))
  151. mux.Handle("GET /config-builds/{buildID}", authMiddleware(http.HandlerFunc(configHandler.GetBuild)))
  152. mux.Handle("GET /sessions/{sessionPublicID}", authMiddleware(http.HandlerFunc(sessionHandler.GetDetail)))
  153. mux.Handle("GET /sessions/{sessionPublicID}/result", authMiddleware(http.HandlerFunc(resultHandler.GetSessionResult)))
  154. mux.HandleFunc("POST /sessions/{sessionPublicID}/start", sessionHandler.Start)
  155. mux.HandleFunc("POST /sessions/{sessionPublicID}/finish", sessionHandler.Finish)
  156. mux.HandleFunc("POST /auth/sms/send", authHandler.SendSMSCode)
  157. mux.HandleFunc("POST /auth/login/sms", authHandler.LoginSMS)
  158. mux.HandleFunc("POST /auth/login/wechat-mini", authHandler.LoginWechatMini)
  159. mux.HandleFunc("POST /ops/auth/sms/send", opsAuthHandler.SendSMSCode)
  160. mux.HandleFunc("POST /ops/auth/register", opsAuthHandler.Register)
  161. mux.HandleFunc("POST /ops/auth/login/sms", opsAuthHandler.LoginSMS)
  162. mux.HandleFunc("POST /ops/auth/refresh", opsAuthHandler.Refresh)
  163. mux.HandleFunc("POST /ops/auth/logout", opsAuthHandler.Logout)
  164. mux.Handle("GET /ops/me", opsAuthMiddleware(http.HandlerFunc(opsAuthHandler.Me)))
  165. mux.Handle("POST /auth/bind/mobile", authMiddleware(http.HandlerFunc(authHandler.BindMobile)))
  166. mux.HandleFunc("POST /auth/refresh", authHandler.Refresh)
  167. mux.HandleFunc("POST /auth/logout", authHandler.Logout)
  168. mux.Handle("GET /me", authMiddleware(http.HandlerFunc(meHandler.Get)))
  169. mux.Handle("GET /me/sessions", authMiddleware(http.HandlerFunc(sessionHandler.ListMine)))
  170. mux.Handle("GET /me/results", authMiddleware(http.HandlerFunc(resultHandler.ListMine)))
  171. mux.Handle("GET /ops/admin/summary", opsAuthMiddleware(http.HandlerFunc(opsSummaryHandler.GetOverview)))
  172. mux.Handle("GET /ops/admin/region-options", opsAuthMiddleware(http.HandlerFunc(regionOptionsHandler.Get)))
  173. mux.Handle("GET /ops/admin/assets", opsAuthMiddleware(http.HandlerFunc(adminAssetHandler.ListAssets)))
  174. mux.Handle("POST /ops/admin/assets/register-link", opsAuthMiddleware(http.HandlerFunc(adminAssetHandler.RegisterLink)))
  175. mux.Handle("POST /ops/admin/assets/upload", opsAuthMiddleware(http.HandlerFunc(adminAssetHandler.UploadFile)))
  176. mux.Handle("GET /ops/admin/assets/{assetPublicID}", opsAuthMiddleware(http.HandlerFunc(adminAssetHandler.GetAsset)))
  177. mux.Handle("GET /ops/admin/places", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.ListPlaces)))
  178. mux.Handle("POST /ops/admin/places", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.CreatePlace)))
  179. mux.Handle("GET /ops/admin/places/{placePublicID}", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.GetPlace)))
  180. mux.Handle("GET /ops/admin/map-assets", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.ListMapAssets)))
  181. mux.Handle("POST /ops/admin/places/{placePublicID}/map-assets", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.CreateMapAsset)))
  182. mux.Handle("GET /ops/admin/map-assets/{mapAssetPublicID}", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.GetMapAsset)))
  183. mux.Handle("PUT /ops/admin/map-assets/{mapAssetPublicID}", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.UpdateMapAsset)))
  184. mux.Handle("POST /ops/admin/map-assets/{mapAssetPublicID}/tile-releases", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.CreateTileRelease)))
  185. mux.Handle("POST /ops/admin/ops/tile-releases/import", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.ImportTileRelease)))
  186. mux.Handle("GET /ops/admin/course-sources", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.ListCourseSources)))
  187. mux.Handle("GET /ops/admin/course-sources/{sourcePublicID}", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.GetCourseSource)))
  188. mux.Handle("GET /ops/admin/course-sets/{courseSetPublicID}", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.GetCourseSet)))
  189. mux.Handle("POST /ops/admin/ops/course-sets/import-kml-batch", opsAuthMiddleware(http.HandlerFunc(adminProductionHandler.ImportCourseSetKMLBatch)))
  190. mux.Handle("GET /ops/admin/events", opsAuthMiddleware(http.HandlerFunc(adminEventHandler.ListEvents)))
  191. mux.Handle("POST /ops/admin/events", opsAuthMiddleware(http.HandlerFunc(adminEventHandler.CreateEvent)))
  192. mux.Handle("GET /ops/admin/events/{eventPublicID}", opsAuthMiddleware(http.HandlerFunc(adminEventHandler.GetEvent)))
  193. mux.Handle("PUT /ops/admin/events/{eventPublicID}", opsAuthMiddleware(http.HandlerFunc(adminEventHandler.UpdateEvent)))
  194. mux.Handle("POST /ops/admin/events/{eventPublicID}/presentations/import", opsAuthMiddleware(http.HandlerFunc(adminEventHandler.ImportPresentation)))
  195. mux.Handle("POST /ops/admin/events/{eventPublicID}/content-bundles/import", opsAuthMiddleware(http.HandlerFunc(adminEventHandler.ImportContentBundle)))
  196. mux.Handle("POST /ops/admin/events/{eventPublicID}/defaults", opsAuthMiddleware(http.HandlerFunc(adminEventHandler.UpdateEventDefaults)))
  197. mux.Handle("GET /ops/admin/events/{eventPublicID}/pipeline", opsAuthMiddleware(http.HandlerFunc(adminPipelineHandler.GetEventPipeline)))
  198. mux.Handle("POST /ops/admin/sources/{sourceID}/build", opsAuthMiddleware(http.HandlerFunc(adminPipelineHandler.BuildSource)))
  199. mux.Handle("GET /ops/admin/builds/{buildID}", opsAuthMiddleware(http.HandlerFunc(adminPipelineHandler.GetBuild)))
  200. mux.Handle("POST /ops/admin/builds/{buildID}/publish", opsAuthMiddleware(http.HandlerFunc(adminPipelineHandler.PublishBuild)))
  201. mux.Handle("GET /ops/admin/releases/{releasePublicID}", opsAuthMiddleware(http.HandlerFunc(adminPipelineHandler.GetRelease)))
  202. mux.Handle("POST /ops/admin/events/{eventPublicID}/rollback", opsAuthMiddleware(http.HandlerFunc(adminPipelineHandler.RollbackRelease)))
  203. return mux
  204. }