router.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. entryService *service.EntryService,
  14. entryHomeService *service.EntryHomeService,
  15. adminResourceService *service.AdminResourceService,
  16. adminProductionService *service.AdminProductionService,
  17. adminEventService *service.AdminEventService,
  18. adminPipelineService *service.AdminPipelineService,
  19. eventService *service.EventService,
  20. eventPlayService *service.EventPlayService,
  21. configService *service.ConfigService,
  22. homeService *service.HomeService,
  23. profileService *service.ProfileService,
  24. resultService *service.ResultService,
  25. sessionService *service.SessionService,
  26. devService *service.DevService,
  27. meService *service.MeService,
  28. ) http.Handler {
  29. mux := http.NewServeMux()
  30. healthHandler := handlers.NewHealthHandler()
  31. authHandler := handlers.NewAuthHandler(authService)
  32. entryHandler := handlers.NewEntryHandler(entryService)
  33. entryHomeHandler := handlers.NewEntryHomeHandler(entryHomeService)
  34. adminResourceHandler := handlers.NewAdminResourceHandler(adminResourceService)
  35. adminProductionHandler := handlers.NewAdminProductionHandler(adminProductionService)
  36. adminEventHandler := handlers.NewAdminEventHandler(adminEventService)
  37. adminPipelineHandler := handlers.NewAdminPipelineHandler(adminPipelineService)
  38. eventHandler := handlers.NewEventHandler(eventService)
  39. eventPlayHandler := handlers.NewEventPlayHandler(eventPlayService)
  40. configHandler := handlers.NewConfigHandler(configService)
  41. homeHandler := handlers.NewHomeHandler(homeService)
  42. profileHandler := handlers.NewProfileHandler(profileService)
  43. resultHandler := handlers.NewResultHandler(resultService)
  44. sessionHandler := handlers.NewSessionHandler(sessionService)
  45. devHandler := handlers.NewDevHandler(devService)
  46. meHandler := handlers.NewMeHandler(meService)
  47. authMiddleware := middleware.NewAuthMiddleware(jwtManager)
  48. mux.HandleFunc("GET /healthz", healthHandler.Get)
  49. mux.HandleFunc("GET /home", homeHandler.GetHome)
  50. mux.HandleFunc("GET /cards", homeHandler.GetCards)
  51. mux.HandleFunc("GET /entry/resolve", entryHandler.Resolve)
  52. mux.Handle("GET /admin/maps", authMiddleware(http.HandlerFunc(adminResourceHandler.ListMaps)))
  53. mux.Handle("POST /admin/maps", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateMap)))
  54. mux.Handle("GET /admin/maps/{mapPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetMap)))
  55. mux.Handle("POST /admin/maps/{mapPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateMapVersion)))
  56. mux.Handle("GET /admin/places", authMiddleware(http.HandlerFunc(adminProductionHandler.ListPlaces)))
  57. mux.Handle("POST /admin/places", authMiddleware(http.HandlerFunc(adminProductionHandler.CreatePlace)))
  58. mux.Handle("GET /admin/places/{placePublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetPlace)))
  59. mux.Handle("POST /admin/places/{placePublicID}/map-assets", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateMapAsset)))
  60. mux.Handle("GET /admin/map-assets/{mapAssetPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetMapAsset)))
  61. mux.Handle("POST /admin/map-assets/{mapAssetPublicID}/tile-releases", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateTileRelease)))
  62. mux.Handle("POST /admin/map-assets/{mapAssetPublicID}/course-sets", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseSet)))
  63. mux.Handle("GET /admin/course-sources", authMiddleware(http.HandlerFunc(adminProductionHandler.ListCourseSources)))
  64. mux.Handle("POST /admin/course-sources", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseSource)))
  65. mux.Handle("GET /admin/course-sources/{sourcePublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetCourseSource)))
  66. mux.Handle("GET /admin/course-sets/{courseSetPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetCourseSet)))
  67. mux.Handle("POST /admin/course-sets/{courseSetPublicID}/variants", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateCourseVariant)))
  68. mux.Handle("GET /admin/runtime-bindings", authMiddleware(http.HandlerFunc(adminProductionHandler.ListRuntimeBindings)))
  69. mux.Handle("POST /admin/runtime-bindings", authMiddleware(http.HandlerFunc(adminProductionHandler.CreateRuntimeBinding)))
  70. mux.Handle("GET /admin/runtime-bindings/{runtimeBindingPublicID}", authMiddleware(http.HandlerFunc(adminProductionHandler.GetRuntimeBinding)))
  71. mux.Handle("GET /admin/playfields", authMiddleware(http.HandlerFunc(adminResourceHandler.ListPlayfields)))
  72. mux.Handle("POST /admin/playfields", authMiddleware(http.HandlerFunc(adminResourceHandler.CreatePlayfield)))
  73. mux.Handle("GET /admin/playfields/{playfieldPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetPlayfield)))
  74. mux.Handle("POST /admin/playfields/{playfieldPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreatePlayfieldVersion)))
  75. mux.Handle("GET /admin/resource-packs", authMiddleware(http.HandlerFunc(adminResourceHandler.ListResourcePacks)))
  76. mux.Handle("POST /admin/resource-packs", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateResourcePack)))
  77. mux.Handle("GET /admin/resource-packs/{resourcePackPublicID}", authMiddleware(http.HandlerFunc(adminResourceHandler.GetResourcePack)))
  78. mux.Handle("POST /admin/resource-packs/{resourcePackPublicID}/versions", authMiddleware(http.HandlerFunc(adminResourceHandler.CreateResourcePackVersion)))
  79. mux.Handle("GET /admin/events", authMiddleware(http.HandlerFunc(adminEventHandler.ListEvents)))
  80. mux.Handle("POST /admin/events", authMiddleware(http.HandlerFunc(adminEventHandler.CreateEvent)))
  81. mux.Handle("GET /admin/events/{eventPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetEvent)))
  82. mux.Handle("PUT /admin/events/{eventPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.UpdateEvent)))
  83. mux.Handle("POST /admin/events/{eventPublicID}/source", authMiddleware(http.HandlerFunc(adminEventHandler.SaveSource)))
  84. mux.Handle("GET /admin/events/{eventPublicID}/presentations", authMiddleware(http.HandlerFunc(adminEventHandler.ListPresentations)))
  85. mux.Handle("POST /admin/events/{eventPublicID}/presentations", authMiddleware(http.HandlerFunc(adminEventHandler.CreatePresentation)))
  86. mux.Handle("POST /admin/events/{eventPublicID}/presentations/import", authMiddleware(http.HandlerFunc(adminEventHandler.ImportPresentation)))
  87. mux.Handle("GET /admin/presentations/{presentationPublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetPresentation)))
  88. mux.Handle("GET /admin/events/{eventPublicID}/content-bundles", authMiddleware(http.HandlerFunc(adminEventHandler.ListContentBundles)))
  89. mux.Handle("POST /admin/events/{eventPublicID}/content-bundles", authMiddleware(http.HandlerFunc(adminEventHandler.CreateContentBundle)))
  90. mux.Handle("POST /admin/events/{eventPublicID}/content-bundles/import", authMiddleware(http.HandlerFunc(adminEventHandler.ImportContentBundle)))
  91. mux.Handle("GET /admin/content-bundles/{contentBundlePublicID}", authMiddleware(http.HandlerFunc(adminEventHandler.GetContentBundle)))
  92. mux.Handle("POST /admin/events/{eventPublicID}/defaults", authMiddleware(http.HandlerFunc(adminEventHandler.UpdateEventDefaults)))
  93. mux.Handle("GET /admin/events/{eventPublicID}/pipeline", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetEventPipeline)))
  94. mux.Handle("POST /admin/sources/{sourceID}/build", authMiddleware(http.HandlerFunc(adminPipelineHandler.BuildSource)))
  95. mux.Handle("GET /admin/builds/{buildID}", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetBuild)))
  96. mux.Handle("POST /admin/builds/{buildID}/publish", authMiddleware(http.HandlerFunc(adminPipelineHandler.PublishBuild)))
  97. mux.Handle("GET /admin/releases/{releasePublicID}", authMiddleware(http.HandlerFunc(adminPipelineHandler.GetRelease)))
  98. mux.Handle("POST /admin/releases/{releasePublicID}/runtime-binding", authMiddleware(http.HandlerFunc(adminPipelineHandler.BindReleaseRuntime)))
  99. mux.Handle("POST /admin/events/{eventPublicID}/rollback", authMiddleware(http.HandlerFunc(adminPipelineHandler.RollbackRelease)))
  100. if appEnv != "production" {
  101. mux.HandleFunc("GET /dev/workbench", devHandler.Workbench)
  102. mux.HandleFunc("POST /dev/bootstrap-demo", devHandler.BootstrapDemo)
  103. mux.HandleFunc("POST /dev/client-logs", devHandler.CreateClientLog)
  104. mux.HandleFunc("GET /dev/client-logs", devHandler.ListClientLogs)
  105. mux.HandleFunc("DELETE /dev/client-logs", devHandler.ClearClientLogs)
  106. mux.HandleFunc("GET /dev/manifest-summary", devHandler.ManifestSummary)
  107. mux.HandleFunc("GET /dev/demo-assets/presentations/{demoKey}", devHandler.DemoPresentationSchema)
  108. mux.HandleFunc("GET /dev/demo-assets/content-manifests/{demoKey}", devHandler.DemoContentManifest)
  109. mux.HandleFunc("GET /dev/config/local-files", configHandler.ListLocalFiles)
  110. mux.HandleFunc("POST /dev/events/{eventPublicID}/config-sources/import-local", configHandler.ImportLocal)
  111. mux.HandleFunc("POST /dev/config-builds/preview", configHandler.BuildPreview)
  112. mux.HandleFunc("POST /dev/config-builds/publish", configHandler.PublishBuild)
  113. }
  114. mux.Handle("GET /me/entry-home", authMiddleware(http.HandlerFunc(entryHomeHandler.Get)))
  115. mux.Handle("GET /me/profile", authMiddleware(http.HandlerFunc(profileHandler.Get)))
  116. mux.HandleFunc("GET /events/{eventPublicID}", eventHandler.GetDetail)
  117. mux.Handle("GET /events/{eventPublicID}/play", authMiddleware(http.HandlerFunc(eventPlayHandler.Get)))
  118. mux.Handle("GET /events/{eventPublicID}/config-sources", authMiddleware(http.HandlerFunc(configHandler.ListSources)))
  119. mux.Handle("POST /events/{eventPublicID}/launch", authMiddleware(http.HandlerFunc(eventHandler.Launch)))
  120. mux.Handle("GET /config-sources/{sourceID}", authMiddleware(http.HandlerFunc(configHandler.GetSource)))
  121. mux.Handle("GET /config-builds/{buildID}", authMiddleware(http.HandlerFunc(configHandler.GetBuild)))
  122. mux.Handle("GET /sessions/{sessionPublicID}", authMiddleware(http.HandlerFunc(sessionHandler.GetDetail)))
  123. mux.Handle("GET /sessions/{sessionPublicID}/result", authMiddleware(http.HandlerFunc(resultHandler.GetSessionResult)))
  124. mux.HandleFunc("POST /sessions/{sessionPublicID}/start", sessionHandler.Start)
  125. mux.HandleFunc("POST /sessions/{sessionPublicID}/finish", sessionHandler.Finish)
  126. mux.HandleFunc("POST /auth/sms/send", authHandler.SendSMSCode)
  127. mux.HandleFunc("POST /auth/login/sms", authHandler.LoginSMS)
  128. mux.HandleFunc("POST /auth/login/wechat-mini", authHandler.LoginWechatMini)
  129. mux.Handle("POST /auth/bind/mobile", authMiddleware(http.HandlerFunc(authHandler.BindMobile)))
  130. mux.HandleFunc("POST /auth/refresh", authHandler.Refresh)
  131. mux.HandleFunc("POST /auth/logout", authHandler.Logout)
  132. mux.Handle("GET /me", authMiddleware(http.HandlerFunc(meHandler.Get)))
  133. mux.Handle("GET /me/sessions", authMiddleware(http.HandlerFunc(sessionHandler.ListMine)))
  134. mux.Handle("GET /me/results", authMiddleware(http.HandlerFunc(resultHandler.ListMine)))
  135. return mux
  136. }