@stackonward/cli 0.0.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +4588 -205
- package/dist/index.js.map +1 -1
- package/dist/resources/apple/.claude/docs/architecture-template.md +54 -0
- package/dist/resources/apple/.claude/docs/architecture.md +52 -0
- package/dist/resources/apple/.claude/docs/index.md +13 -0
- package/dist/resources/apple/.claude/docs/security-lifecycle.md +57 -0
- package/dist/resources/apple/.claude/docs/stack.md +93 -0
- package/dist/resources/apple/.claude/docs/verification.md +63 -0
- package/dist/resources/apple/AGENTS.md +397 -0
- package/dist/resources/apple/CLAUDE.md +397 -0
- package/dist/resources/apple/Gemfile +2 -0
- package/dist/resources/apple/Gemfile.lock +365 -0
- package/dist/resources/apple/_gitignore +14 -0
- package/dist/resources/apple/fastlane/Fastfile +123 -0
- package/dist/resources/components/nuxt/site-source/app/app.vue +42 -0
- package/dist/resources/components/nuxt/site-source/app/assets/css/site-shell.css +1035 -0
- package/dist/resources/components/nuxt/site-source/app/assets/icons/brand/.gitkeep +0 -0
- package/dist/resources/components/nuxt/site-source/app/assets/icons/common/.gitkeep +0 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ApplicationHeader.vue +35 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ApplicationShell.vue +56 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ApplicationSidebar.vue +40 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/BackToTop.vue +42 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ContentArticlePage.vue +40 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ContentListPage.vue +30 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/CookieConsent.vue +18 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/MarketingFooter.vue +52 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/MarketingHeader.vue +106 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/MarketingShell.vue +35 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/SiteBrand.vue +10 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/StaticPage.vue +21 -0
- package/dist/resources/components/nuxt/site-source/app/components/site/ToastViewport.vue +25 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BaseButton.vue +86 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BaseGradientBorder.vue +59 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BaseModal.vue +131 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BasePopover.vue +150 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/BaseText.vue +138 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/CheckoutStatus.vue +112 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/CheckoutSummary.vue +172 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/GoogleSignInButton.vue +113 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/InfiniteScroll.vue +123 -0
- package/dist/resources/components/nuxt/site-source/app/components/ui/YoutubeEmbed.vue +126 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useAnalytics.ts +24 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useCheckout.ts +121 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useConsent.ts +31 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useGoogleIdentity.ts +163 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useNavigation.ts +11 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useSeo.ts +33 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useSiteShell.ts +58 -0
- package/dist/resources/components/nuxt/site-source/app/composables/useToasts.ts +48 -0
- package/dist/resources/components/nuxt/site-source/app/layouts/app.vue +5 -0
- package/dist/resources/components/nuxt/site-source/app/layouts/default.vue +5 -0
- package/dist/resources/components/nuxt/site-source/app/plugins/analytics.client.ts +121 -0
- package/dist/resources/components/nuxt/site-source/app/plugins/google-identity.client.ts +24 -0
- package/dist/resources/components/nuxt/site-source/app/theme/accordion.ts +9 -0
- package/dist/resources/components/nuxt/site-source/app/theme/breadcrumb.ts +8 -0
- package/dist/resources/components/nuxt/site-source/app/theme/button.ts +92 -0
- package/dist/resources/components/nuxt/site-source/app/theme/carousel.ts +11 -0
- package/dist/resources/components/nuxt/site-source/app/theme/checkbox.ts +12 -0
- package/dist/resources/components/nuxt/site-source/app/theme/index.ts +30 -0
- package/dist/resources/components/nuxt/site-source/app/theme/input.ts +47 -0
- package/dist/resources/components/nuxt/site-source/app/theme/modal.ts +9 -0
- package/dist/resources/components/nuxt/site-source/app/theme/pagination.ts +6 -0
- package/dist/resources/components/nuxt/site-source/app/theme/select.ts +23 -0
- package/dist/resources/components/nuxt/site-source/app/theme/stepper.ts +10 -0
- package/dist/resources/components/nuxt/site-source/app/theme/toast.ts +8 -0
- package/dist/resources/components/nuxt/site-source/app/theme/tooltip.ts +6 -0
- package/dist/resources/components/nuxt/site-source/app/types/site-shell.ts +85 -0
- package/dist/resources/components/react/ui/Button.tsx +73 -0
- package/dist/resources/components/react/ui/Input.tsx +44 -0
- package/dist/resources/components/registry.json +355 -0
- package/dist/resources/components/vue/feedback/Toast.vue +120 -0
- package/dist/resources/components/vue/feedback/useToast.ts +70 -0
- package/dist/resources/components/vue/media/FileUpload.vue +229 -0
- package/dist/resources/components/vue/media/VideoPlayer.vue +157 -0
- package/dist/resources/components/vue/ui/AppImage.vue +147 -0
- package/dist/resources/components/vue/ui/BottomSheet.vue +217 -0
- package/dist/resources/components/vue/ui/Button.vue +154 -0
- package/dist/resources/components/vue/ui/Card.vue +111 -0
- package/dist/resources/components/vue/ui/Input.vue +113 -0
- package/dist/resources/components/vue/ui/Modal.vue +193 -0
- package/dist/resources/components/vue/ui/ScrollArea.vue +163 -0
- package/dist/resources/components/vue/ui/Tooltip.vue +115 -0
- package/dist/resources/registry.json +158 -0
- package/dist/resources/snippets/node/cache.ts +116 -0
- package/dist/resources/snippets/node/date.ts +126 -0
- package/dist/resources/snippets/node/logger.ts +104 -0
- package/dist/resources/snippets/node/validation.ts +132 -0
- package/dist/resources/snippets/react/useDebounce.ts +90 -0
- package/dist/resources/snippets/registry.json +65 -0
- package/dist/resources/snippets/vue/useAsync.ts +96 -0
- package/dist/resources/snippets/vue/usePagination.ts +102 -0
- package/dist/resources/templates/base/.claude/docs/architecture-template.md +459 -0
- package/dist/resources/templates/base/.claude/docs/architecture.md +137 -0
- package/dist/resources/templates/base/.claude/docs/index.md +42 -0
- package/dist/resources/templates/base/.claude/docs/security-lifecycle.md +129 -0
- package/dist/resources/templates/base/.claude/docs/verification.md +121 -0
- package/dist/resources/templates/base/AGENTS.md +415 -0
- package/dist/resources/templates/base/CLAUDE.md +415 -0
- package/dist/resources/templates/go-backend/.claude/docs/api-design.md +486 -0
- package/dist/resources/templates/go-backend/.claude/docs/architecture-template.md +449 -0
- package/dist/resources/templates/go-backend/.claude/docs/check-list.md +155 -0
- package/dist/resources/templates/go-backend/.claude/docs/database.md +369 -0
- package/dist/resources/templates/go-backend/.claude/docs/e2e-standards.md +161 -0
- package/dist/resources/templates/go-backend/.claude/docs/error-handling.md +558 -0
- package/dist/resources/templates/go-backend/.claude/docs/event-runtime-boundary.md +124 -0
- package/dist/resources/templates/go-backend/.claude/docs/identifier-contract.md +195 -0
- package/dist/resources/templates/go-backend/.claude/docs/index.md +22 -0
- package/dist/resources/templates/go-backend/.claude/docs/logging.md +314 -0
- package/dist/resources/templates/go-backend/.claude/docs/mediagen-provider-onboarding-sop.md +917 -0
- package/dist/resources/templates/go-backend/.claude/docs/mediagen-provider-quick-reference.md +195 -0
- package/dist/resources/templates/go-backend/.claude/docs/module-standards.md +309 -0
- package/dist/resources/templates/go-backend/.claude/docs/openapi-contract.md +141 -0
- package/dist/resources/templates/go-backend/.claude/docs/payment-gateway-onboarding.md +283 -0
- package/dist/resources/templates/go-backend/.claude/docs/platform-boundary.md +191 -0
- package/dist/resources/templates/go-backend/.claude/docs/quality-gates.md +112 -0
- package/dist/resources/templates/go-backend/.dockerignore +6 -0
- package/dist/resources/templates/go-backend/.env.example +13 -0
- package/dist/resources/templates/go-backend/.gitattributes +2 -0
- package/dist/resources/templates/go-backend/AGENTS.md +1139 -0
- package/dist/resources/templates/go-backend/CLAUDE.md +1139 -0
- package/dist/resources/templates/go-backend/Dockerfile +14 -0
- package/dist/resources/templates/go-backend/Makefile +71 -0
- package/dist/resources/templates/go-backend/README.md +66 -0
- package/dist/resources/templates/go-backend/_gitignore +10 -0
- package/dist/resources/templates/go-backend/api/openapi/README.md +29 -0
- package/dist/resources/templates/go-backend/api/openapi/dist/artifact-manifest.json +19 -0
- package/dist/resources/templates/go-backend/api/openapi/dist/conformance-matrix.json +276 -0
- package/dist/resources/templates/go-backend/api/openapi/document.go +30 -0
- package/dist/resources/templates/go-backend/api/openapi/package.json +28 -0
- package/dist/resources/templates/go-backend/api/openapi/pnpm-lock.yaml +3052 -0
- package/dist/resources/templates/go-backend/api/openapi/redocly-source.yaml +18 -0
- package/dist/resources/templates/go-backend/api/openapi/redocly.yaml +8 -0
- package/dist/resources/templates/go-backend/api/openapi/sdk-generators.json +16 -0
- package/dist/resources/templates/go-backend/api/openapi/service-registry.json +39 -0
- package/dist/resources/templates/go-backend/api/openapi/services/product-api/conformance/runtime.arazzo.yaml +65 -0
- package/dist/resources/templates/go-backend/api/openapi/services/product-api/dist/internal.openapi.json +327 -0
- package/dist/resources/templates/go-backend/api/openapi/services/product-api/dist/runtime-contract.json +56 -0
- package/dist/resources/templates/go-backend/api/openapi/services/product-api/openapi.yaml +222 -0
- package/dist/resources/templates/go-backend/api/openapi/services/worker/conformance/runtime.arazzo.yaml +65 -0
- package/dist/resources/templates/go-backend/api/openapi/services/worker/dist/internal.openapi.json +327 -0
- package/dist/resources/templates/go-backend/api/openapi/services/worker/dist/runtime-contract.json +56 -0
- package/dist/resources/templates/go-backend/api/openapi/services/worker/openapi.yaml +222 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/breaking.mjs +416 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/build.mjs +103 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/conformance_coverage.mjs +111 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/contract_compiler.mjs +62 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/contract_policy.mjs +208 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/delegated_contract.mjs +302 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/deterministic_archive.mjs +69 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/documentation_example_privacy.mjs +93 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/documentation_projection.mjs +498 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/documentation_quality.mjs +346 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/git_artifact_history.mjs +188 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/manifest.mjs +170 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/overlay.mjs +98 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/project.mjs +549 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/route_contract.mjs +156 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/runtime_conformance.mjs +193 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/scalar_asset_localization.mjs +164 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/sdk_generator.mjs +229 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/semantic_version.mjs +64 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/service_contract_inputs.mjs +16 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/breaking.test.mjs +365 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/build.test.mjs +42 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/conformance_coverage.test.mjs +88 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/contract_policy.test.mjs +257 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/delegated_contract.test.mjs +205 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/documentation_example_privacy.test.mjs +51 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/documentation_projection.test.mjs +368 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/documentation_quality.test.mjs +151 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/git_artifact_history.test.mjs +60 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/head_policy.test.mjs +8 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/overlay.test.mjs +44 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/projection.test.mjs +270 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/redocly_documentation_rules.test.mjs +146 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/release_manifest.test.mjs +76 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/route_contract.test.mjs +119 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/runtime_conformance.test.mjs +75 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/scalar_asset_localization.test.mjs +50 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/sdk_generator.test.mjs +31 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/semantic_version.test.mjs +60 -0
- package/dist/resources/templates/go-backend/api/openapi/tooling/test/service_contract_inputs.test.mjs +27 -0
- package/dist/resources/templates/go-backend/cmd/internal/runtime/resources.go +125 -0
- package/dist/resources/templates/go-backend/cmd/internal/runtime/runtime.go +102 -0
- package/dist/resources/templates/go-backend/cmd/internal/runtime/runtime_test.go +70 -0
- package/dist/resources/templates/go-backend/cmd/product-api/lifecycle_test.go +58 -0
- package/dist/resources/templates/go-backend/cmd/product-api/main.go +32 -0
- package/dist/resources/templates/go-backend/cmd/product-api/wire.go +13 -0
- package/dist/resources/templates/go-backend/cmd/product-api/wire_gen.go +94 -0
- package/dist/resources/templates/go-backend/cmd/worker/main.go +47 -0
- package/dist/resources/templates/go-backend/cmd/worker/wire.go +14 -0
- package/dist/resources/templates/go-backend/cmd/worker/wire_gen.go +111 -0
- package/dist/resources/templates/go-backend/configs/config.yaml +37 -0
- package/dist/resources/templates/go-backend/deployments/compose.yaml +71 -0
- package/dist/resources/templates/go-backend/docs/openapi-provenance.json +489 -0
- package/dist/resources/templates/go-backend/docs/runtime-provenance.json +175 -0
- package/dist/resources/templates/go-backend/docs/source-provenance.json +3568 -0
- package/dist/resources/templates/go-backend/docs/standards-provenance.json +1624 -0
- package/dist/resources/templates/go-backend/go.mod +256 -0
- package/dist/resources/templates/go-backend/go.sum +805 -0
- package/dist/resources/templates/go-backend/internal/common/canonicaljson/canonical.go +370 -0
- package/dist/resources/templates/go-backend/internal/common/canonicaljson/canonical_test.go +223 -0
- package/dist/resources/templates/go-backend/internal/common/checkoutbff/contracts.go +125 -0
- package/dist/resources/templates/go-backend/internal/common/checkoutbff/platform_client.go +212 -0
- package/dist/resources/templates/go-backend/internal/common/checkoutbff/platform_client_test.go +319 -0
- package/dist/resources/templates/go-backend/internal/common/contentaccess/contentaccess.go +50 -0
- package/dist/resources/templates/go-backend/internal/common/contracts/contact.go +85 -0
- package/dist/resources/templates/go-backend/internal/common/ctxutil/identity.go +20 -0
- package/dist/resources/templates/go-backend/internal/common/ctxutil/product.go +53 -0
- package/dist/resources/templates/go-backend/internal/common/enumdomain/payment.go +12 -0
- package/dist/resources/templates/go-backend/internal/common/enums/dto_admin.go +9 -0
- package/dist/resources/templates/go-backend/internal/common/enums/enum.go +1247 -0
- package/dist/resources/templates/go-backend/internal/common/enums/handler_admin.go +66 -0
- package/dist/resources/templates/go-backend/internal/common/enums/module.go +46 -0
- package/dist/resources/templates/go-backend/internal/common/enums/provider.go +10 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register.go +11 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_audit.go +274 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_command_replay.go +12 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_config.go +27 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_locale.go +37 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_ops.go +14 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_outbox.go +15 -0
- package/dist/resources/templates/go-backend/internal/common/enums/register_rule_expression.go +63 -0
- package/dist/resources/templates/go-backend/internal/common/enums/registry.go +77 -0
- package/dist/resources/templates/go-backend/internal/common/httporigin/origin.go +62 -0
- package/dist/resources/templates/go-backend/internal/common/httporigin/origin_test.go +38 -0
- package/dist/resources/templates/go-backend/internal/common/iamcredential/plaintext.go +35 -0
- package/dist/resources/templates/go-backend/internal/common/iamcredential/plaintext_test.go +29 -0
- package/dist/resources/templates/go-backend/internal/common/iamscope/scope.go +212 -0
- package/dist/resources/templates/go-backend/internal/common/iamscope/scope_binding.go +26 -0
- package/dist/resources/templates/go-backend/internal/common/iamscope/scope_filter.go +173 -0
- package/dist/resources/templates/go-backend/internal/common/iamscope/scope_filter_test.go +279 -0
- package/dist/resources/templates/go-backend/internal/common/internalapi/authorization_details.go +10 -0
- package/dist/resources/templates/go-backend/internal/common/internalapi/route_permission_policies.go +660 -0
- package/dist/resources/templates/go-backend/internal/common/internalapi/route_permission_policies_test.go +331 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/audience_token_source.go +166 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/bootstrap_token_exchanger.go +183 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/bound_service_token_signer.go +52 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/delegated_access_token_exchanger.go +189 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/delegated_access_token_exchanger_test.go +159 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/product_scoped_service_token_signer.go +92 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/provider.go +25 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/request_signer.go +86 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/resolver.go +69 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/resolver_test.go +42 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/route_scope.go +95 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/route_scope_test.go +78 -0
- package/dist/resources/templates/go-backend/internal/common/internalservice/service_identity_test.go +387 -0
- package/dist/resources/templates/go-backend/internal/common/jsonschema/compiler.go +100 -0
- package/dist/resources/templates/go-backend/internal/common/jsonschema/compiler_test.go +41 -0
- package/dist/resources/templates/go-backend/internal/common/jwksapi/handler.go +83 -0
- package/dist/resources/templates/go-backend/internal/common/jwksapi/handler_test.go +104 -0
- package/dist/resources/templates/go-backend/internal/common/locale/locale.go +145 -0
- package/dist/resources/templates/go-backend/internal/common/locale/locale_test.go +75 -0
- package/dist/resources/templates/go-backend/internal/common/mediaref/mediaref.go +54 -0
- package/dist/resources/templates/go-backend/internal/common/mediaref/mediaref_test.go +77 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/apikey_auth.go +320 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/apikey_throttle.go +158 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/auth.go +40 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/auth_test.go +50 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/client_context.go +86 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/cors.go +68 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/event_context.go +18 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/gateway_identity.go +279 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/helpers.go +159 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_delegated_route_permission.go +171 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_delegated_token_auth.go +151 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_delegated_token_auth_test.go +99 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_service_identity.go +138 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/iam_service_token_auth.go +74 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/idempotent.go +273 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/idempotent_test.go +83 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/internal_route_permission.go +45 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/internal_route_permission_test.go +186 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/logging.go +191 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/metrics.go +45 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/middleware_test.go +1516 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/principal_context.go +160 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/principal_context_test.go +355 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/product_context.go +100 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/recovery.go +40 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/referral_attribution.go +178 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/requestid.go +48 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/resource_permission.go +790 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/resource_permission_test.go +440 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/service_credential_auth.go +67 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/tenant.go +148 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/throttle.go +310 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/trusted_client_ip.go +23 -0
- package/dist/resources/templates/go-backend/internal/common/middleware/trusted_client_ip_test.go +60 -0
- package/dist/resources/templates/go-backend/internal/common/money/currency.go +25 -0
- package/dist/resources/templates/go-backend/internal/common/money/errors.go +23 -0
- package/dist/resources/templates/go-backend/internal/common/money/format.go +123 -0
- package/dist/resources/templates/go-backend/internal/common/money/format_test.go +204 -0
- package/dist/resources/templates/go-backend/internal/common/money/gateway.go +144 -0
- package/dist/resources/templates/go-backend/internal/common/money/gateway_test.go +165 -0
- package/dist/resources/templates/go-backend/internal/common/money/rounding.go +56 -0
- package/dist/resources/templates/go-backend/internal/common/money/rounding_test.go +51 -0
- package/dist/resources/templates/go-backend/internal/common/principal/context.go +56 -0
- package/dist/resources/templates/go-backend/internal/common/principal/principal.go +49 -0
- package/dist/resources/templates/go-backend/internal/common/productcode/code.go +24 -0
- package/dist/resources/templates/go-backend/internal/common/productcode/code_test.go +35 -0
- package/dist/resources/templates/go-backend/internal/common/providerconfig/schema.go +577 -0
- package/dist/resources/templates/go-backend/internal/common/providerconfig/schema_test.go +47 -0
- package/dist/resources/templates/go-backend/internal/common/requestprivacy/request.go +246 -0
- package/dist/resources/templates/go-backend/internal/common/requestprivacy/request_test.go +170 -0
- package/dist/resources/templates/go-backend/internal/common/response/pagination.go +60 -0
- package/dist/resources/templates/go-backend/internal/common/response/response.go +110 -0
- package/dist/resources/templates/go-backend/internal/common/response/response_test.go +463 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/ast.go +141 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/ast_test.go +81 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/canonical.go +184 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/canonical_test.go +319 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/decoder.go +555 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/decoder_test.go +260 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/errors.go +55 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/evaluator.go +96 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/evaluator_test.go +220 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/fuzz_test.go +116 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/limits.go +331 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/schema.go +412 -0
- package/dist/resources/templates/go-backend/internal/common/ruleexpression/schema_test.go +164 -0
- package/dist/resources/templates/go-backend/internal/common/runtimeinput/runtime_input.go +627 -0
- package/dist/resources/templates/go-backend/internal/common/runtimeinput/runtime_input_test.go +245 -0
- package/dist/resources/templates/go-backend/internal/common/semver/errors.go +6 -0
- package/dist/resources/templates/go-backend/internal/common/semver/semver.go +125 -0
- package/dist/resources/templates/go-backend/internal/common/semver/semver_test.go +205 -0
- package/dist/resources/templates/go-backend/internal/common/serviceidentity/audience.go +48 -0
- package/dist/resources/templates/go-backend/internal/common/serviceidentity/audience_test.go +35 -0
- package/dist/resources/templates/go-backend/internal/common/serviceidentity/resource_identifier.go +33 -0
- package/dist/resources/templates/go-backend/internal/common/serviceidentity/resource_identifier_test.go +32 -0
- package/dist/resources/templates/go-backend/internal/common/tradeno/generator.go +99 -0
- package/dist/resources/templates/go-backend/internal/common/tradeno/generator_test.go +396 -0
- package/dist/resources/templates/go-backend/internal/common/tradeno/prefix.go +247 -0
- package/dist/resources/templates/go-backend/internal/common/types/asset_url.go +11 -0
- package/dist/resources/templates/go-backend/internal/common/types/billing.go +89 -0
- package/dist/resources/templates/go-backend/internal/common/types/billing_test.go +77 -0
- package/dist/resources/templates/go-backend/internal/common/types/country.go +74 -0
- package/dist/resources/templates/go-backend/internal/common/types/country_test.go +28 -0
- package/dist/resources/templates/go-backend/internal/common/types/currency.go +60 -0
- package/dist/resources/templates/go-backend/internal/common/types/identifiers.go +41 -0
- package/dist/resources/templates/go-backend/internal/common/types/operator.go +28 -0
- package/dist/resources/templates/go-backend/internal/common/types/subscription_entitlement.go +24 -0
- package/dist/resources/templates/go-backend/internal/common/types/subscription_entitlement_test.go +35 -0
- package/dist/resources/templates/go-backend/internal/common/types/tier.go +72 -0
- package/dist/resources/templates/go-backend/internal/common/types/types.go +411 -0
- package/dist/resources/templates/go-backend/internal/common/types/types_test.go +295 -0
- package/dist/resources/templates/go-backend/internal/common/utils/constants.go +100 -0
- package/dist/resources/templates/go-backend/internal/common/utils/crypto.go +158 -0
- package/dist/resources/templates/go-backend/internal/common/utils/crypto_test.go +345 -0
- package/dist/resources/templates/go-backend/internal/common/utils/duration.go +25 -0
- package/dist/resources/templates/go-backend/internal/common/utils/helpers.go +18 -0
- package/dist/resources/templates/go-backend/internal/common/utils/json.go +62 -0
- package/dist/resources/templates/go-backend/internal/common/utils/json_test.go +76 -0
- package/dist/resources/templates/go-backend/internal/common/utils/pagination.go +51 -0
- package/dist/resources/templates/go-backend/internal/common/utils/pagination_test.go +45 -0
- package/dist/resources/templates/go-backend/internal/common/utils/pointer.go +107 -0
- package/dist/resources/templates/go-backend/internal/common/utils/pointer_test.go +15 -0
- package/dist/resources/templates/go-backend/internal/common/utils/slice.go +24 -0
- package/dist/resources/templates/go-backend/internal/common/utils/snowflake.go +110 -0
- package/dist/resources/templates/go-backend/internal/common/utils/snowflake_test.go +316 -0
- package/dist/resources/templates/go-backend/internal/common/utils/string.go +16 -0
- package/dist/resources/templates/go-backend/internal/common/utils/timefmt.go +42 -0
- package/dist/resources/templates/go-backend/internal/common/validator/validator.go +136 -0
- package/dist/resources/templates/go-backend/internal/common/validator/validator_test.go +240 -0
- package/dist/resources/templates/go-backend/internal/common/youtubeapi/client.go +224 -0
- package/dist/resources/templates/go-backend/internal/core/apicontract/document.go +269 -0
- package/dist/resources/templates/go-backend/internal/core/apicontract/document_test.go +104 -0
- package/dist/resources/templates/go-backend/internal/core/apicontract/route_validator.go +78 -0
- package/dist/resources/templates/go-backend/internal/core/apicontract/route_validator_test.go +108 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/assets/scalar-api-reference.js +2385 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/assets/scalar-api-reference.metadata.json +10 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/route.go +107 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/route_test.go +200 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/scalar.go +117 -0
- package/dist/resources/templates/go-backend/internal/core/apidocs/scalar_test.go +99 -0
- package/dist/resources/templates/go-backend/internal/core/audit/access_log_policy.go +91 -0
- package/dist/resources/templates/go-backend/internal/core/audit/access_log_policy_test.go +51 -0
- package/dist/resources/templates/go-backend/internal/core/audit/actor_resolver.go +36 -0
- package/dist/resources/templates/go-backend/internal/core/audit/constants.go +30 -0
- package/dist/resources/templates/go-backend/internal/core/audit/context.go +119 -0
- package/dist/resources/templates/go-backend/internal/core/audit/context_test.go +310 -0
- package/dist/resources/templates/go-backend/internal/core/audit/dto.go +468 -0
- package/dist/resources/templates/go-backend/internal/core/audit/dto_admin.go +172 -0
- package/dist/resources/templates/go-backend/internal/core/audit/enum.go +460 -0
- package/dist/resources/templates/go-backend/internal/core/audit/enum_test.go +314 -0
- package/dist/resources/templates/go-backend/internal/core/audit/errors.go +9 -0
- package/dist/resources/templates/go-backend/internal/core/audit/handler.go +568 -0
- package/dist/resources/templates/go-backend/internal/core/audit/listener.go +319 -0
- package/dist/resources/templates/go-backend/internal/core/audit/listener_test.go +98 -0
- package/dist/resources/templates/go-backend/internal/core/audit/middleware.go +669 -0
- package/dist/resources/templates/go-backend/internal/core/audit/middleware_test.go +268 -0
- package/dist/resources/templates/go-backend/internal/core/audit/model.go +306 -0
- package/dist/resources/templates/go-backend/internal/core/audit/partition_job.go +316 -0
- package/dist/resources/templates/go-backend/internal/core/audit/ports.go +9 -0
- package/dist/resources/templates/go-backend/internal/core/audit/provider.go +30 -0
- package/dist/resources/templates/go-backend/internal/core/audit/repository.go +1007 -0
- package/dist/resources/templates/go-backend/internal/core/audit/repository_test.go +934 -0
- package/dist/resources/templates/go-backend/internal/core/audit/router.go +85 -0
- package/dist/resources/templates/go-backend/internal/core/audit/service.go +770 -0
- package/dist/resources/templates/go-backend/internal/core/audit/service_test.go +331 -0
- package/dist/resources/templates/go-backend/internal/core/audit/vo_admin.go +475 -0
- package/dist/resources/templates/go-backend/internal/core/cache/config.go +30 -0
- package/dist/resources/templates/go-backend/internal/core/cache/key.go +41 -0
- package/dist/resources/templates/go-backend/internal/core/cache/key_test.go +62 -0
- package/dist/resources/templates/go-backend/internal/core/cache/l1.go +153 -0
- package/dist/resources/templates/go-backend/internal/core/cache/l1_test.go +212 -0
- package/dist/resources/templates/go-backend/internal/core/cache/manager.go +278 -0
- package/dist/resources/templates/go-backend/internal/core/cache/manager_provider.go +24 -0
- package/dist/resources/templates/go-backend/internal/core/cache/metrics.go +126 -0
- package/dist/resources/templates/go-backend/internal/core/cache/namespace.go +46 -0
- package/dist/resources/templates/go-backend/internal/core/cache/namespace_test.go +90 -0
- package/dist/resources/templates/go-backend/internal/core/cache/options.go +39 -0
- package/dist/resources/templates/go-backend/internal/core/cache/options_test.go +96 -0
- package/dist/resources/templates/go-backend/internal/core/cache/redis.go +311 -0
- package/dist/resources/templates/go-backend/internal/core/cache/redis_test.go +51 -0
- package/dist/resources/templates/go-backend/internal/core/cache/serializer.go +31 -0
- package/dist/resources/templates/go-backend/internal/core/cache/serializer_test.go +139 -0
- package/dist/resources/templates/go-backend/internal/core/cache/singleflight.go +53 -0
- package/dist/resources/templates/go-backend/internal/core/cache/singleflight_test.go +211 -0
- package/dist/resources/templates/go-backend/internal/core/cache/warming.go +86 -0
- package/dist/resources/templates/go-backend/internal/core/clock/clock.go +66 -0
- package/dist/resources/templates/go-backend/internal/core/clock/clock_test.go +26 -0
- package/dist/resources/templates/go-backend/internal/core/clock/runtime_clock.go +283 -0
- package/dist/resources/templates/go-backend/internal/core/clock/runtime_clock_test.go +98 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/errors.go +6 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/model.go +90 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/model_test.go +79 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/namespace.go +7 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/persistence.go +269 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/persistence_test.go +185 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/provider.go +9 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/repository.go +22 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/service.go +853 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/service_persistence_test.go +432 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/service_test.go +891 -0
- package/dist/resources/templates/go-backend/internal/core/commandreplay/types.go +121 -0
- package/dist/resources/templates/go-backend/internal/core/config/config.go +2069 -0
- package/dist/resources/templates/go-backend/internal/core/config/config_test.go +1122 -0
- package/dist/resources/templates/go-backend/internal/core/config/contract/contract_test.go +388 -0
- package/dist/resources/templates/go-backend/internal/core/config/contract/docs.go +198 -0
- package/dist/resources/templates/go-backend/internal/core/config/contract/verify.go +424 -0
- package/dist/resources/templates/go-backend/internal/core/config/enum.go +46 -0
- package/dist/resources/templates/go-backend/internal/core/config/metadata.go +717 -0
- package/dist/resources/templates/go-backend/internal/core/config/metadata_test.go +228 -0
- package/dist/resources/templates/go-backend/internal/core/config/product_admin_config_test.go +210 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/aes_gcm.go +111 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/aes_gcm_test.go +364 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/encryptor.go +12 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/envelope_encryptor.go +134 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/envelope_encryptor_test.go +118 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/pem.go +112 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/pem_test.go +44 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/provider.go +130 -0
- package/dist/resources/templates/go-backend/internal/core/crypto/provider_test.go +92 -0
- package/dist/resources/templates/go-backend/internal/core/database/base_model.go +24 -0
- package/dist/resources/templates/go-backend/internal/core/database/clickhouse.go +121 -0
- package/dist/resources/templates/go-backend/internal/core/database/database.go +112 -0
- package/dist/resources/templates/go-backend/internal/core/database/database_logger_test.go +95 -0
- package/dist/resources/templates/go-backend/internal/core/database/errors.go +25 -0
- package/dist/resources/templates/go-backend/internal/core/database/metrics_plugin.go +105 -0
- package/dist/resources/templates/go-backend/internal/core/database/pool_collector.go +71 -0
- package/dist/resources/templates/go-backend/internal/core/database/read_replica.go +520 -0
- package/dist/resources/templates/go-backend/internal/core/database/transaction.go +170 -0
- package/dist/resources/templates/go-backend/internal/core/database/transaction_test.go +282 -0
- package/dist/resources/templates/go-backend/internal/core/distlock/lock.go +353 -0
- package/dist/resources/templates/go-backend/internal/core/distlock/lock_test.go +106 -0
- package/dist/resources/templates/go-backend/internal/core/errors/handler.go +189 -0
- package/dist/resources/templates/go-backend/internal/core/errors/handler_test.go +515 -0
- package/dist/resources/templates/go-backend/internal/core/eventbus/eventbus.go +667 -0
- package/dist/resources/templates/go-backend/internal/core/eventbus/eventbus_test.go +792 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/noop.go +22 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/provider.go +20 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/publisher.go +38 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/redis.go +109 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/routing.go +104 -0
- package/dist/resources/templates/go-backend/internal/core/eventstream/routing_test.go +71 -0
- package/dist/resources/templates/go-backend/internal/core/geoip/model.go +19 -0
- package/dist/resources/templates/go-backend/internal/core/geoip/service.go +88 -0
- package/dist/resources/templates/go-backend/internal/core/health/health.go +333 -0
- package/dist/resources/templates/go-backend/internal/core/health/health_test.go +27 -0
- package/dist/resources/templates/go-backend/internal/core/httpclient/client.go +772 -0
- package/dist/resources/templates/go-backend/internal/core/httpclient/client_test.go +287 -0
- package/dist/resources/templates/go-backend/internal/core/httpclient/download.go +578 -0
- package/dist/resources/templates/go-backend/internal/core/httpclient/download_test.go +374 -0
- package/dist/resources/templates/go-backend/internal/core/jwtkeyring/jwk.go +38 -0
- package/dist/resources/templates/go-backend/internal/core/jwtkeyring/keyring.go +144 -0
- package/dist/resources/templates/go-backend/internal/core/jwtkeyring/keyring_test.go +128 -0
- package/dist/resources/templates/go-backend/internal/core/jwtkeyring/registry.go +60 -0
- package/dist/resources/templates/go-backend/internal/core/logger/logger.go +53 -0
- package/dist/resources/templates/go-backend/internal/core/logger/logger_test.go +194 -0
- package/dist/resources/templates/go-backend/internal/core/observability/metrics.go +358 -0
- package/dist/resources/templates/go-backend/internal/core/observability/sentry.go +445 -0
- package/dist/resources/templates/go-backend/internal/core/observability/sentry_privacy_test.go +175 -0
- package/dist/resources/templates/go-backend/internal/core/observability/tracing.go +187 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/model.go +314 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/model_test.go +77 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/persistence.go +51 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/provider.go +23 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/relay.go +1138 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/relay_postgres_test.go +257 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/relay_test.go +690 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/rescheduler.go +164 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/rescheduler_test.go +209 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/writer.go +249 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/writer_postgres_test.go +170 -0
- package/dist/resources/templates/go-backend/internal/core/outbox/writer_test.go +123 -0
- package/dist/resources/templates/go-backend/internal/core/provider.go +198 -0
- package/dist/resources/templates/go-backend/internal/core/queue/metrics_middleware.go +38 -0
- package/dist/resources/templates/go-backend/internal/core/queue/metrics_middleware_test.go +12 -0
- package/dist/resources/templates/go-backend/internal/core/queue/queue.go +288 -0
- package/dist/resources/templates/go-backend/internal/core/queue/queue_test.go +159 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/bulkhead.go +514 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/bulkhead_test.go +984 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/circuitbreaker.go +358 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/circuitbreaker_test.go +788 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/retry.go +318 -0
- package/dist/resources/templates/go-backend/internal/core/resilience/retry_test.go +735 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/recorder.go +305 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/run_context.go +124 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/run_context_test.go +85 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/scheduler.go +1589 -0
- package/dist/resources/templates/go-backend/internal/core/schedule/scheduler_control_test.go +808 -0
- package/dist/resources/templates/go-backend/internal/core/storage/oss_provider.go +398 -0
- package/dist/resources/templates/go-backend/internal/core/storage/oss_provider_test.go +89 -0
- package/dist/resources/templates/go-backend/internal/core/storage/path.go +110 -0
- package/dist/resources/templates/go-backend/internal/core/storage/path_test.go +301 -0
- package/dist/resources/templates/go-backend/internal/core/storage/s3_provider.go +489 -0
- package/dist/resources/templates/go-backend/internal/core/storage/s3_provider_test.go +60 -0
- package/dist/resources/templates/go-backend/internal/core/storage/storage.go +268 -0
- package/dist/resources/templates/go-backend/internal/core/storage/validation.go +225 -0
- package/dist/resources/templates/go-backend/internal/core/storage/validation_test.go +495 -0
- package/dist/resources/templates/go-backend/internal/core/temporal/logger.go +38 -0
- package/dist/resources/templates/go-backend/internal/core/temporal/provider.go +126 -0
- package/dist/resources/templates/go-backend/internal/core/temporal/provider_test.go +67 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/client.go +282 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/client_test.go +81 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/config.go +58 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/handler.go +53 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/hub.go +269 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/message.go +273 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/notifier.go +92 -0
- package/dist/resources/templates/go-backend/internal/core/websocket/provider.go +33 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/action.go +77 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/action_test.go +238 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/condition.go +107 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/decision.go +256 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/enum.go +643 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/errors.go +102 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/principal_record.go +21 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/product_context.go +486 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/resource.go +215 -0
- package/dist/resources/templates/go-backend/internal/integrations/onex/iam/resource_test.go +242 -0
- package/dist/resources/templates/go-backend/internal/products/README.md +5 -0
- package/dist/resources/templates/go-backend/internal/sharedevents/README.md +5 -0
- package/dist/resources/templates/go-backend/migrations/000001_infrastructure.down.sql +13 -0
- package/dist/resources/templates/go-backend/migrations/000001_infrastructure.up.sql +886 -0
- package/dist/resources/templates/go-backend/pkg/apierror/error.go +327 -0
- package/dist/resources/templates/go-backend/pkg/apierror/error_code.go +65 -0
- package/dist/resources/templates/go-backend/pkg/apierror/error_test.go +41 -0
- package/dist/resources/templates/go-backend/pkg/apierror/error_type.go +14 -0
- package/dist/resources/templates/go-backend/pkg/apierror/http_response.go +216 -0
- package/dist/resources/templates/go-backend/pkg/apierror/http_response_test.go +216 -0
- package/dist/resources/templates/go-backend/scripts/check-openapi-clean-deletion.sh +54 -0
- package/dist/resources/templates/go-backend/scripts/test-migrations.sh +32 -0
- package/dist/resources/templates/go-backend/tests/infrastructure.sql +69 -0
- package/dist/resources/templates/mcp-server/.claude/docs/stack.md +338 -0
- package/dist/resources/templates/mcp-server/.editorconfig +13 -0
- package/dist/resources/templates/mcp-server/.env.example +8 -0
- package/dist/resources/templates/mcp-server/.github/ISSUE_TEMPLATE/bug_report.yml +104 -0
- package/dist/resources/templates/mcp-server/.github/ISSUE_TEMPLATE/feature_request.yml +54 -0
- package/dist/resources/templates/mcp-server/.github/PULL_REQUEST_TEMPLATE.md +36 -0
- package/dist/resources/templates/mcp-server/.github/workflows/ci.yml +78 -0
- package/dist/resources/templates/mcp-server/.husky/commit-msg +1 -0
- package/dist/resources/templates/mcp-server/.husky/pre-commit +1 -0
- package/dist/resources/templates/mcp-server/.lintstagedrc.json +4 -0
- package/dist/resources/templates/mcp-server/.nvmrc +1 -0
- package/dist/resources/templates/mcp-server/.prettierrc +8 -0
- package/dist/resources/templates/mcp-server/CONTRIBUTING.md +184 -0
- package/dist/resources/templates/mcp-server/Dockerfile +23 -0
- package/dist/resources/templates/mcp-server/LICENSE +21 -0
- package/dist/resources/templates/mcp-server/README.md +123 -0
- package/dist/resources/templates/mcp-server/_gitignore +26 -0
- package/dist/resources/templates/mcp-server/commitlint.config.js +24 -0
- package/dist/resources/templates/mcp-server/eslint.config.js +30 -0
- package/dist/resources/templates/mcp-server/package.json +72 -0
- package/dist/resources/templates/mcp-server/src/index.ts +27 -0
- package/dist/resources/templates/mcp-server/src/resources.ts +33 -0
- package/dist/resources/templates/mcp-server/src/services/.gitkeep +0 -0
- package/dist/resources/templates/mcp-server/src/tools.ts +43 -0
- package/dist/resources/templates/mcp-server/src/types/index.ts +19 -0
- package/dist/resources/templates/mcp-server/src/utils/index.ts +31 -0
- package/dist/resources/templates/mcp-server/tsconfig.json +22 -0
- package/dist/resources/templates/mcp-server/tsup.config.ts +14 -0
- package/dist/resources/templates/mcp-server/vitest.config.ts +15 -0
- package/dist/resources/templates/nuxt/.claude/docs/stack.md +666 -0
- package/dist/resources/templates/nuxt/.editorconfig +15 -0
- package/dist/resources/templates/nuxt/.env.example +22 -0
- package/dist/resources/templates/nuxt/.github/workflows/ci.yml +34 -0
- package/dist/resources/templates/nuxt/.husky/commit-msg +1 -0
- package/dist/resources/templates/nuxt/.husky/pre-commit +1 -0
- package/dist/resources/templates/nuxt/.nvmrc +1 -0
- package/dist/resources/templates/nuxt/.prettierignore +9 -0
- package/dist/resources/templates/nuxt/.prettierrc +1 -0
- package/dist/resources/templates/nuxt/README.md +23 -0
- package/dist/resources/templates/nuxt/_gitignore +7 -0
- package/dist/resources/templates/nuxt/app/app.config.ts +105 -0
- package/dist/resources/templates/nuxt/app/i18n/locales/en.json +5 -0
- package/dist/resources/templates/nuxt/app/pages/app/index.vue +224 -0
- package/dist/resources/templates/nuxt/app/pages/authors/[slug].vue +37 -0
- package/dist/resources/templates/nuxt/app/pages/blog/[slug].vue +55 -0
- package/dist/resources/templates/nuxt/app/pages/blog/category/[category].vue +38 -0
- package/dist/resources/templates/nuxt/app/pages/blog/index.vue +46 -0
- package/dist/resources/templates/nuxt/app/pages/index.vue +626 -0
- package/dist/resources/templates/nuxt/app/pages/legal/privacy.vue +21 -0
- package/dist/resources/templates/nuxt/app/pages/legal/terms.vue +21 -0
- package/dist/resources/templates/nuxt/app/pages/ui/index.vue +397 -0
- package/dist/resources/templates/nuxt/commitlint.config.mjs +1 -0
- package/dist/resources/templates/nuxt/eslint.config.mjs +4 -0
- package/dist/resources/templates/nuxt/lint-staged.config.mjs +8 -0
- package/dist/resources/templates/nuxt/nuxt.config.ts +71 -0
- package/dist/resources/templates/nuxt/package.json +67 -0
- package/dist/resources/templates/nuxt/public/apple-touch-icon.png +0 -0
- package/dist/resources/templates/nuxt/public/favicon.ico +0 -0
- package/dist/resources/templates/nuxt/public/favicon.svg +5 -0
- package/dist/resources/templates/nuxt/public/icon-16x16.png +0 -0
- package/dist/resources/templates/nuxt/public/icon-192x192.png +0 -0
- package/dist/resources/templates/nuxt/public/icon-32x32.png +0 -0
- package/dist/resources/templates/nuxt/public/icon-512x512.png +0 -0
- package/dist/resources/templates/nuxt/public/logo.svg +6 -0
- package/dist/resources/templates/nuxt/public/media/hero-product-gallery.png +0 -0
- package/dist/resources/templates/nuxt/server/api/content/sitemap-urls.get.ts +12 -0
- package/dist/resources/templates/nuxt/server/api/preview.get.ts +9 -0
- package/dist/resources/templates/nuxt/stylelint.config.mjs +3 -0
- package/dist/resources/templates/nuxt/test/e2e/rendering.test.ts +13 -0
- package/dist/resources/templates/nuxt/tsconfig.json +3 -0
- package/dist/resources/templates/nuxt/vitest.config.ts +8 -0
- package/package.json +8 -5
|
@@ -0,0 +1,1138 @@
|
|
|
1
|
+
package outbox
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"encoding/json"
|
|
6
|
+
"fmt"
|
|
7
|
+
"os"
|
|
8
|
+
"sort"
|
|
9
|
+
"strings"
|
|
10
|
+
"sync"
|
|
11
|
+
"time"
|
|
12
|
+
|
|
13
|
+
"github.com/google/uuid"
|
|
14
|
+
"github.com/jackc/pgx/v5/stdlib"
|
|
15
|
+
"go.uber.org/zap"
|
|
16
|
+
"gorm.io/gorm"
|
|
17
|
+
"gorm.io/gorm/clause"
|
|
18
|
+
|
|
19
|
+
"{{module}}/internal/core/database"
|
|
20
|
+
"{{module}}/internal/core/eventbus"
|
|
21
|
+
"{{module}}/internal/core/eventstream"
|
|
22
|
+
"{{module}}/internal/core/queue"
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
// ═══════════════════════════════════════════════════════════════
|
|
26
|
+
// 跨进程事件常量
|
|
27
|
+
// ═══════════════════════════════════════════════════════════════
|
|
28
|
+
|
|
29
|
+
const (
|
|
30
|
+
// TaskTypeEventRelay Asynq 跨进程事件转发任务类型
|
|
31
|
+
TaskTypeEventRelay = "event:relay"
|
|
32
|
+
// QueueEvents 跨进程事件转发专用队列
|
|
33
|
+
QueueEvents = "events"
|
|
34
|
+
// defaultRelayMaxRetries Relay 默认最大重试次数
|
|
35
|
+
defaultRelayMaxRetries = 10
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
// ═══════════════════════════════════════════════════════════════
|
|
39
|
+
// 跨进程事件载荷
|
|
40
|
+
// ═══════════════════════════════════════════════════════════════
|
|
41
|
+
|
|
42
|
+
// CrossProcessEventPayload Asynq 跨进程事件载荷
|
|
43
|
+
type CrossProcessEventPayload struct {
|
|
44
|
+
// OutboxRef 是来源 outbox 记录引用。
|
|
45
|
+
OutboxRef string `json:"outbox_ref"`
|
|
46
|
+
// 事件类型
|
|
47
|
+
EventType string `json:"event_type"`
|
|
48
|
+
// 聚合根类型
|
|
49
|
+
AggregateType string `json:"aggregate_type"`
|
|
50
|
+
// 聚合根 ID
|
|
51
|
+
AggregateID string `json:"aggregate_id"`
|
|
52
|
+
// AggregateVersion 聚合版本;没有版本合同的事件不传该字段。
|
|
53
|
+
AggregateVersion *int64 `json:"aggregate_version,omitempty"`
|
|
54
|
+
// SourceProductCode 是事件来源产品;平台来源事件为空。
|
|
55
|
+
SourceProductCode string `json:"source_product_code,omitempty"`
|
|
56
|
+
// AudienceScope 是事件创建时冻结的受众范围。
|
|
57
|
+
AudienceScope AudienceScope `json:"audience_scope"`
|
|
58
|
+
// AudienceProductCodes 是 products 受众的有序 Product 集合。
|
|
59
|
+
AudienceProductCodes []string `json:"audience_product_codes"`
|
|
60
|
+
// 事件载荷(原始 JSON)
|
|
61
|
+
Payload json.RawMessage `json:"payload"`
|
|
62
|
+
// 事件元数据(原始 JSON)
|
|
63
|
+
Metadata json.RawMessage `json:"metadata"`
|
|
64
|
+
// 审计变更信封(原始 JSON)
|
|
65
|
+
AuditChanges json.RawMessage `json:"audit_changes,omitempty"`
|
|
66
|
+
// 审计元信息信封(原始 JSON)
|
|
67
|
+
AuditMeta json.RawMessage `json:"audit_meta,omitempty"`
|
|
68
|
+
// 事件发生时间
|
|
69
|
+
OccurredAt time.Time `json:"occurred_at"`
|
|
70
|
+
// 事件唯一标识
|
|
71
|
+
EventID string `json:"event_id"`
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ═══════════════════════════════════════════════════════════════
|
|
75
|
+
// Relay 配置
|
|
76
|
+
// ═══════════════════════════════════════════════════════════════
|
|
77
|
+
|
|
78
|
+
// RelayConfig Outbox Relay 配置
|
|
79
|
+
type RelayConfig struct {
|
|
80
|
+
// 轮询间隔(默认 2s)
|
|
81
|
+
PollInterval time.Duration
|
|
82
|
+
// 每次批量处理大小(默认 100)
|
|
83
|
+
BatchSize int
|
|
84
|
+
// 锁超时时间,超时后回收(默认 5m)
|
|
85
|
+
LockTimeout time.Duration
|
|
86
|
+
// 最大重试次数(默认 10)
|
|
87
|
+
MaxRetries int
|
|
88
|
+
// 清理已处理事件的间隔(默认 1h)
|
|
89
|
+
CleanupInterval time.Duration
|
|
90
|
+
// 已处理事件保留时长(默认 72h)
|
|
91
|
+
RetentionDuration time.Duration
|
|
92
|
+
// 需要投递到 Asynq 跨进程任务的事件类型模式(支持通配符:identity.* 匹配 identity.created)
|
|
93
|
+
AsynqEventPatterns []string
|
|
94
|
+
// 需要投递到 Redis Stream 的事件类型模式(支持通配符:identity.* 匹配 identity.created)
|
|
95
|
+
RedisStreamEventPatterns []string
|
|
96
|
+
// 限定本 Relay 实例只处理的聚合类型(为空表示处理所有类型)
|
|
97
|
+
// 多服务共享同一 outbox 表时,通过此字段实现事件路由隔离(类似 Kafka topic partition)
|
|
98
|
+
AggregateTypes []string
|
|
99
|
+
// 必须持续重试的合规关键事件类型模式;失败时使用封顶退避而不进入死信。
|
|
100
|
+
PersistentRetryEvents []string
|
|
101
|
+
// 事件载荷反序列化函数(将 JSON 还原为具体 struct 指针,供 EventBus 消费方使用)
|
|
102
|
+
//
|
|
103
|
+
// 返回 (payload, nil) 表示成功;返回 (nil, err) 表示未注册事件类型或 JSON 格式错误,
|
|
104
|
+
// Relay 侧必须按错误路径处理(记录 Outbox 重试或 DLQ),禁止退化成 map[string]any。
|
|
105
|
+
PayloadDeserializer func(eventType string, data []byte) (any, error)
|
|
106
|
+
// 当前 Relay 实例标识(默认自动生成)
|
|
107
|
+
RelayID string
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// DefaultRelayConfig 返回默认 Relay 配置
|
|
111
|
+
func DefaultRelayConfig() RelayConfig {
|
|
112
|
+
return RelayConfig{
|
|
113
|
+
PollInterval: 2 * time.Second,
|
|
114
|
+
BatchSize: 100,
|
|
115
|
+
LockTimeout: 5 * time.Minute,
|
|
116
|
+
MaxRetries: defaultRelayMaxRetries,
|
|
117
|
+
CleanupInterval: time.Hour,
|
|
118
|
+
RetentionDuration: 72 * time.Hour,
|
|
119
|
+
RelayID: generateRelayID(),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ═══════════════════════════════════════════════════════════════
|
|
124
|
+
// Relay 核心结构
|
|
125
|
+
// ═══════════════════════════════════════════════════════════════
|
|
126
|
+
|
|
127
|
+
// Relay Outbox 事件中继器
|
|
128
|
+
//
|
|
129
|
+
// 从 sys_outbox_events 表轮询待处理事件,分发到本地 EventBus,
|
|
130
|
+
// 可选转发到 Asynq 实现跨进程事件传递。
|
|
131
|
+
// 支持 PostgreSQL LISTEN/NOTIFY 低延迟唤醒。
|
|
132
|
+
type Relay struct {
|
|
133
|
+
db *gorm.DB
|
|
134
|
+
logger *zap.Logger
|
|
135
|
+
eventBus *eventbus.EventBus
|
|
136
|
+
queueClient *queue.Client
|
|
137
|
+
streamPublisher eventstream.EventStreamPublisher // nil 表示不启用 Stream 分发
|
|
138
|
+
txManager *database.TransactionManager
|
|
139
|
+
config RelayConfig
|
|
140
|
+
wakeup chan struct{}
|
|
141
|
+
done chan struct{}
|
|
142
|
+
wg sync.WaitGroup
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// NewRelay 创建 Outbox Relay 实例
|
|
146
|
+
//
|
|
147
|
+
// streamPublisher 可传 nil,表示不启用 Redis Stream 分发通道。
|
|
148
|
+
func NewRelay(
|
|
149
|
+
db *gorm.DB,
|
|
150
|
+
logger *zap.Logger,
|
|
151
|
+
eventBus *eventbus.EventBus,
|
|
152
|
+
queueClient *queue.Client,
|
|
153
|
+
streamPublisher eventstream.EventStreamPublisher,
|
|
154
|
+
config RelayConfig,
|
|
155
|
+
) *Relay {
|
|
156
|
+
if config.RelayID == "" {
|
|
157
|
+
config.RelayID = generateRelayID()
|
|
158
|
+
}
|
|
159
|
+
return &Relay{
|
|
160
|
+
db: db,
|
|
161
|
+
logger: logger.Named("OutboxRelay"),
|
|
162
|
+
eventBus: eventBus,
|
|
163
|
+
queueClient: queueClient,
|
|
164
|
+
streamPublisher: streamPublisher,
|
|
165
|
+
txManager: database.NewTransactionManager(db),
|
|
166
|
+
config: config,
|
|
167
|
+
wakeup: make(chan struct{}, 1),
|
|
168
|
+
done: make(chan struct{}),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ═══════════════════════════════════════════════════════════════
|
|
173
|
+
// 生命周期
|
|
174
|
+
// ═══════════════════════════════════════════════════════════════
|
|
175
|
+
|
|
176
|
+
// Start 启动 Relay 后台 goroutine(轮询 + 维护 + LISTEN/NOTIFY)
|
|
177
|
+
func (r *Relay) Start(ctx context.Context) {
|
|
178
|
+
r.wg.Add(1)
|
|
179
|
+
go r.runPollLoop(ctx)
|
|
180
|
+
|
|
181
|
+
r.wg.Add(1)
|
|
182
|
+
go r.runMaintenanceLoop(ctx)
|
|
183
|
+
|
|
184
|
+
r.wg.Add(1)
|
|
185
|
+
go r.listenNotify(ctx)
|
|
186
|
+
|
|
187
|
+
r.logger.Info("outbox relay started",
|
|
188
|
+
zap.String("relayId", r.config.RelayID),
|
|
189
|
+
zap.Duration("pollInterval", r.config.PollInterval),
|
|
190
|
+
zap.Int("batchSize", r.config.BatchSize),
|
|
191
|
+
zap.Strings("aggregateTypes", r.config.AggregateTypes),
|
|
192
|
+
zap.Strings("asynqEventPatterns", r.config.AsynqEventPatterns),
|
|
193
|
+
zap.Strings("redisStreamEventPatterns", r.config.RedisStreamEventPatterns),
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Stop 优雅关闭 Relay,等待所有 goroutine 退出
|
|
198
|
+
func (r *Relay) Stop() {
|
|
199
|
+
close(r.done)
|
|
200
|
+
r.wg.Wait()
|
|
201
|
+
r.logger.Info("outbox relay stopped", zap.String("relayId", r.config.RelayID))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ═══════════════════════════════════════════════════════════════
|
|
205
|
+
// 轮询循环
|
|
206
|
+
// ═══════════════════════════════════════════════════════════════
|
|
207
|
+
|
|
208
|
+
// runPollLoop 主轮询循环,通过定时器和 NOTIFY 唤醒信号触发
|
|
209
|
+
func (r *Relay) runPollLoop(ctx context.Context) {
|
|
210
|
+
defer r.wg.Done()
|
|
211
|
+
|
|
212
|
+
ticker := time.NewTicker(r.config.PollInterval)
|
|
213
|
+
defer ticker.Stop()
|
|
214
|
+
|
|
215
|
+
for {
|
|
216
|
+
select {
|
|
217
|
+
case <-r.done:
|
|
218
|
+
return
|
|
219
|
+
case <-ctx.Done():
|
|
220
|
+
return
|
|
221
|
+
case <-ticker.C:
|
|
222
|
+
r.pollUntilEmpty(ctx)
|
|
223
|
+
case <-r.wakeup:
|
|
224
|
+
r.pollUntilEmpty(ctx)
|
|
225
|
+
// 重置定时器避免紧接着再次轮询
|
|
226
|
+
ticker.Reset(r.config.PollInterval)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// pollUntilEmpty 持续轮询直到没有待处理事件
|
|
232
|
+
func (r *Relay) pollUntilEmpty(ctx context.Context) {
|
|
233
|
+
for {
|
|
234
|
+
select {
|
|
235
|
+
case <-r.done:
|
|
236
|
+
return
|
|
237
|
+
case <-ctx.Done():
|
|
238
|
+
return
|
|
239
|
+
default:
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
dispatched := r.poll(ctx)
|
|
243
|
+
if dispatched == 0 {
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// poll 单次轮询:锁定一批事件并逐个分发
|
|
250
|
+
// 返回成功分发的事件数
|
|
251
|
+
func (r *Relay) poll(ctx context.Context) int {
|
|
252
|
+
var events []OutboxEvent
|
|
253
|
+
now := time.Now()
|
|
254
|
+
|
|
255
|
+
err := r.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
|
256
|
+
tx := database.GetDB(txCtx, r.db)
|
|
257
|
+
// SELECT ... FOR UPDATE SKIP LOCKED 确保多实例安全
|
|
258
|
+
// 按 AggregateTypes 过滤,实现多服务事件路由隔离
|
|
259
|
+
args := []any{StatusPending, StatusRetryPending, now}
|
|
260
|
+
aggregateFilter := ""
|
|
261
|
+
if len(r.config.AggregateTypes) > 0 {
|
|
262
|
+
aggregateFilter = "AND aggregate_type IN ?"
|
|
263
|
+
args = append(args, r.config.AggregateTypes)
|
|
264
|
+
}
|
|
265
|
+
args = append(args, r.config.BatchSize)
|
|
266
|
+
query := fmt.Sprintf(`
|
|
267
|
+
SELECT id, outbox_ref, aggregate_type, aggregate_id, aggregate_version, source_product_code,
|
|
268
|
+
audience_scope, audience_product_codes, event_type, schema_version, payload, metadata,
|
|
269
|
+
audit_changes, audit_meta,
|
|
270
|
+
occurred_at, created_at, status, locked_at, locked_by,
|
|
271
|
+
retry_count, max_retries, next_retry_at, last_error
|
|
272
|
+
FROM sys_outbox_events
|
|
273
|
+
WHERE (status = ? OR (status = ? AND next_retry_at <= ?))
|
|
274
|
+
%s
|
|
275
|
+
ORDER BY created_at ASC
|
|
276
|
+
LIMIT ?
|
|
277
|
+
FOR UPDATE SKIP LOCKED
|
|
278
|
+
`, aggregateFilter)
|
|
279
|
+
if err := tx.Raw(query, args...).Scan(&events).Error; err != nil {
|
|
280
|
+
return fmt.Errorf("select pending outbox events: %w", err)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if len(events) == 0 {
|
|
284
|
+
return nil
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// 批量标记为锁定状态
|
|
288
|
+
ids := make([]string, len(events))
|
|
289
|
+
for i := range events {
|
|
290
|
+
ids[i] = events[i].ID
|
|
291
|
+
}
|
|
292
|
+
relayID := r.config.RelayID
|
|
293
|
+
// 租约必须使用数据库持久化后的时间精度,后续 CAS 精确比较 locked_at。
|
|
294
|
+
if err := tx.Model(&events).
|
|
295
|
+
Clauses(clause.Returning{}).
|
|
296
|
+
Where("id IN ?", ids).
|
|
297
|
+
Updates(map[string]any{
|
|
298
|
+
"status": StatusLocked,
|
|
299
|
+
"locked_at": now,
|
|
300
|
+
"locked_by": relayID,
|
|
301
|
+
}).Error; err != nil {
|
|
302
|
+
return fmt.Errorf("lock outbox events: %w", err)
|
|
303
|
+
}
|
|
304
|
+
// UPDATE RETURNING 不保证返回顺序,恢复批次的创建时间顺序。
|
|
305
|
+
sort.SliceStable(events, func(i, j int) bool {
|
|
306
|
+
return events[i].CreatedAt.Before(events[j].CreatedAt)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
return nil
|
|
310
|
+
})
|
|
311
|
+
if err != nil {
|
|
312
|
+
r.logger.Error("outbox poll transaction failed", zap.Error(err))
|
|
313
|
+
return 0
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if len(events) == 0 {
|
|
317
|
+
return 0
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// 在事务外逐个分发事件
|
|
321
|
+
dispatched := 0
|
|
322
|
+
for i := range events {
|
|
323
|
+
event := &events[i]
|
|
324
|
+
if dispatchErr := r.dispatchEvent(ctx, event); dispatchErr != nil {
|
|
325
|
+
r.logger.Error("outbox event dispatch failed",
|
|
326
|
+
zap.String("eventId", event.ID),
|
|
327
|
+
zap.String("eventType", event.EventType),
|
|
328
|
+
zap.Error(dispatchErr),
|
|
329
|
+
)
|
|
330
|
+
continue
|
|
331
|
+
}
|
|
332
|
+
dispatched++
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
r.logger.Debug("outbox poll completed",
|
|
336
|
+
zap.Int("total", len(events)),
|
|
337
|
+
zap.Int("dispatched", dispatched),
|
|
338
|
+
zap.Int("failed", len(events)-dispatched),
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
return dispatched
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// ═══════════════════════════════════════════════════════════════
|
|
345
|
+
// 事件分发
|
|
346
|
+
// ═══════════════════════════════════════════════════════════════
|
|
347
|
+
|
|
348
|
+
// dispatchEvent 分发单个 outbox 事件到按目标拆分的投递通道
|
|
349
|
+
func (r *Relay) dispatchEvent(ctx context.Context, event *OutboxEvent) error {
|
|
350
|
+
targets := r.requiredDeliveryTargets(event.EventType)
|
|
351
|
+
if err := r.ensureDeliveryTargets(ctx, event, targets); err != nil {
|
|
352
|
+
return err
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
deliveries, err := r.claimDueDeliveries(ctx, event.ID, targets, time.Now())
|
|
356
|
+
if err != nil {
|
|
357
|
+
return err
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
var firstErr error
|
|
361
|
+
for i := range deliveries {
|
|
362
|
+
delivery := &deliveries[i]
|
|
363
|
+
if err := r.dispatchDeliveryTarget(ctx, event, delivery.Target); err != nil {
|
|
364
|
+
if markErr := r.markDeliveryRetry(ctx, event, delivery, err); markErr != nil {
|
|
365
|
+
return markErr
|
|
366
|
+
}
|
|
367
|
+
if firstErr == nil {
|
|
368
|
+
firstErr = fmt.Errorf("%s delivery failed: %w", delivery.Target, err)
|
|
369
|
+
}
|
|
370
|
+
continue
|
|
371
|
+
}
|
|
372
|
+
if err := r.markDeliveryDispatched(ctx, event.ID, delivery); err != nil {
|
|
373
|
+
return err
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if _, err := r.refreshParentStatus(ctx, event, targets); err != nil {
|
|
378
|
+
return err
|
|
379
|
+
}
|
|
380
|
+
return firstErr
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
func (r *Relay) dispatchDeliveryTarget(ctx context.Context, event *OutboxEvent, target string) error {
|
|
384
|
+
switch target {
|
|
385
|
+
case TargetLocalEventBus:
|
|
386
|
+
return r.dispatchLocalEventBus(ctx, event)
|
|
387
|
+
case TargetAsynq:
|
|
388
|
+
return r.dispatchAsynq(ctx, event)
|
|
389
|
+
case TargetRedisStream:
|
|
390
|
+
return r.dispatchRedisStream(ctx, event)
|
|
391
|
+
default:
|
|
392
|
+
return fmt.Errorf("unsupported outbox delivery target: %s", target)
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
func (r *Relay) dispatchLocalEventBus(ctx context.Context, event *OutboxEvent) error {
|
|
397
|
+
domainEvent, err := r.toDomainEvent(event)
|
|
398
|
+
if err != nil {
|
|
399
|
+
return err
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
r.logger.Debug("dispatching outbox event to local eventbus",
|
|
403
|
+
zap.String("eventId", event.ID),
|
|
404
|
+
zap.String("eventType", event.EventType),
|
|
405
|
+
zap.String("aggregateId", event.AggregateID),
|
|
406
|
+
zap.String("payloadType", fmt.Sprintf("%T", domainEvent.Payload)),
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
// 发布到本地 EventBus
|
|
410
|
+
if err := r.eventBus.Publish(ctx, domainEvent); err != nil {
|
|
411
|
+
return fmt.Errorf("publish to local eventbus: %w", err)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
r.logger.Debug("outbox event dispatched to local eventbus",
|
|
415
|
+
zap.String("eventId", event.ID),
|
|
416
|
+
zap.String("eventType", event.EventType),
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
return nil
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
func (r *Relay) dispatchAsynq(ctx context.Context, event *OutboxEvent) error {
|
|
423
|
+
if r.queueClient == nil {
|
|
424
|
+
return fmt.Errorf("asynq target is not configured")
|
|
425
|
+
}
|
|
426
|
+
taskPayload := CrossProcessEventPayload{
|
|
427
|
+
OutboxRef: event.OutboxRef,
|
|
428
|
+
EventType: event.EventType,
|
|
429
|
+
AggregateType: event.AggregateType,
|
|
430
|
+
AggregateID: event.AggregateID,
|
|
431
|
+
AggregateVersion: event.AggregateVersion,
|
|
432
|
+
SourceProductCode: optionalStringValue(event.SourceProductCode),
|
|
433
|
+
AudienceScope: event.AudienceScope,
|
|
434
|
+
AudienceProductCodes: cloneProductCodes(event.AudienceProductCodes),
|
|
435
|
+
Payload: event.Payload,
|
|
436
|
+
Metadata: event.Metadata,
|
|
437
|
+
AuditChanges: event.AuditChanges,
|
|
438
|
+
AuditMeta: event.AuditMeta,
|
|
439
|
+
OccurredAt: event.OccurredAt,
|
|
440
|
+
EventID: event.ID,
|
|
441
|
+
}
|
|
442
|
+
_, err := r.queueClient.Enqueue(ctx, TaskTypeEventRelay, taskPayload, queue.EnqueueOptions{
|
|
443
|
+
Queue: QueueEvents,
|
|
444
|
+
UniqueTTL: 24 * time.Hour,
|
|
445
|
+
})
|
|
446
|
+
if err != nil {
|
|
447
|
+
return fmt.Errorf("enqueue cross-process event: %w", err)
|
|
448
|
+
}
|
|
449
|
+
return nil
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
func (r *Relay) dispatchRedisStream(ctx context.Context, event *OutboxEvent) error {
|
|
453
|
+
if r.streamPublisher == nil {
|
|
454
|
+
return fmt.Errorf("redis_stream target is not configured")
|
|
455
|
+
}
|
|
456
|
+
streamEvt := eventstream.StreamEvent{
|
|
457
|
+
EventID: event.ID,
|
|
458
|
+
EventType: event.EventType,
|
|
459
|
+
AggregateID: event.AggregateID,
|
|
460
|
+
OccurredAt: event.OccurredAt,
|
|
461
|
+
ProductCode: optionalStringValue(event.SourceProductCode),
|
|
462
|
+
Payload: event.Payload,
|
|
463
|
+
Metadata: event.Metadata,
|
|
464
|
+
SchemaVersion: optionalStringValue(event.SchemaVersion),
|
|
465
|
+
}
|
|
466
|
+
if err := r.streamPublisher.Publish(ctx, streamEvt); err != nil {
|
|
467
|
+
return fmt.Errorf("publish to event stream: %w", err)
|
|
468
|
+
}
|
|
469
|
+
return nil
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
func (r *Relay) requiredDeliveryTargets(eventType string) []string {
|
|
473
|
+
targets := []string{TargetLocalEventBus}
|
|
474
|
+
if r.queueClient != nil && matchAnyEventPattern(r.config.AsynqEventPatterns, eventType) {
|
|
475
|
+
targets = append(targets, TargetAsynq)
|
|
476
|
+
}
|
|
477
|
+
if r.streamPublisher != nil && matchAnyEventPattern(r.config.RedisStreamEventPatterns, eventType) {
|
|
478
|
+
targets = append(targets, TargetRedisStream)
|
|
479
|
+
}
|
|
480
|
+
return targets
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
func (r *Relay) ensureDeliveryTargets(ctx context.Context, event *OutboxEvent, targets []string) error {
|
|
484
|
+
if len(targets) == 0 {
|
|
485
|
+
return nil
|
|
486
|
+
}
|
|
487
|
+
now := time.Now()
|
|
488
|
+
deliveries := make([]OutboxEventDelivery, 0, len(targets))
|
|
489
|
+
for _, target := range targets {
|
|
490
|
+
deliveries = append(deliveries, OutboxEventDelivery{
|
|
491
|
+
EventID: event.ID,
|
|
492
|
+
Target: target,
|
|
493
|
+
Status: StatusPending,
|
|
494
|
+
MaxRetries: r.deliveryMaxRetries(event),
|
|
495
|
+
CreatedAt: now,
|
|
496
|
+
UpdatedAt: now,
|
|
497
|
+
})
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return r.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
|
501
|
+
db := database.GetDB(txCtx, r.db)
|
|
502
|
+
err := db.Clauses(clause.OnConflict{
|
|
503
|
+
Columns: []clause.Column{{Name: "event_id"}, {Name: "target"}},
|
|
504
|
+
DoNothing: true,
|
|
505
|
+
}).Create(&deliveries).Error
|
|
506
|
+
if err != nil {
|
|
507
|
+
return fmt.Errorf("ensure outbox delivery targets: %w", err)
|
|
508
|
+
}
|
|
509
|
+
return nil
|
|
510
|
+
})
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
func (r *Relay) claimDueDeliveries(ctx context.Context, eventID string, targets []string, now time.Time) ([]OutboxEventDelivery, error) {
|
|
514
|
+
if len(targets) == 0 {
|
|
515
|
+
return nil, nil
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
var deliveries []OutboxEventDelivery
|
|
519
|
+
err := r.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
|
520
|
+
db := database.GetDB(txCtx, r.db)
|
|
521
|
+
if err := db.
|
|
522
|
+
Clauses(clause.Locking{Strength: "UPDATE"}).
|
|
523
|
+
Select("event_id", "target", "status", "retry_count", "max_retries", "next_retry_at", "last_error", "dispatched_at", "locked_at", "locked_by", "created_at", "updated_at").
|
|
524
|
+
Where("event_id = ? AND target IN ? AND (status = ? OR (status = ? AND (next_retry_at IS NULL OR next_retry_at <= ?)))",
|
|
525
|
+
eventID, targets, StatusPending, StatusRetryPending, now).
|
|
526
|
+
Order("created_at ASC, target ASC").
|
|
527
|
+
Find(&deliveries).Error; err != nil {
|
|
528
|
+
return fmt.Errorf("select due outbox delivery targets: %w", err)
|
|
529
|
+
}
|
|
530
|
+
if len(deliveries) == 0 {
|
|
531
|
+
return nil
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
claimedTargets := make([]string, 0, len(deliveries))
|
|
535
|
+
for i := range deliveries {
|
|
536
|
+
claimedTargets = append(claimedTargets, deliveries[i].Target)
|
|
537
|
+
}
|
|
538
|
+
// RETURNING 回填实际租约,避免内存纳秒时间与持久化值不一致。
|
|
539
|
+
if err := db.Model(&deliveries).
|
|
540
|
+
Clauses(clause.Returning{}).
|
|
541
|
+
Where("event_id = ? AND target IN ?", eventID, claimedTargets).
|
|
542
|
+
Updates(map[string]any{
|
|
543
|
+
"locked_at": now,
|
|
544
|
+
"locked_by": r.config.RelayID,
|
|
545
|
+
"updated_at": now,
|
|
546
|
+
}).Error; err != nil {
|
|
547
|
+
return fmt.Errorf("lock outbox delivery targets: %w", err)
|
|
548
|
+
}
|
|
549
|
+
sort.SliceStable(deliveries, func(i, j int) bool {
|
|
550
|
+
if deliveries[i].CreatedAt.Equal(deliveries[j].CreatedAt) {
|
|
551
|
+
return deliveries[i].Target < deliveries[j].Target
|
|
552
|
+
}
|
|
553
|
+
return deliveries[i].CreatedAt.Before(deliveries[j].CreatedAt)
|
|
554
|
+
})
|
|
555
|
+
return nil
|
|
556
|
+
})
|
|
557
|
+
if err != nil {
|
|
558
|
+
return nil, err
|
|
559
|
+
}
|
|
560
|
+
return deliveries, nil
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
func (r *Relay) markDeliveryDispatched(ctx context.Context, eventID string, delivery *OutboxEventDelivery) error {
|
|
564
|
+
if delivery == nil || delivery.LockedAt == nil || delivery.LockedBy == nil {
|
|
565
|
+
return fmt.Errorf("outbox delivery lease is incomplete")
|
|
566
|
+
}
|
|
567
|
+
now := time.Now()
|
|
568
|
+
return r.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
|
569
|
+
result := database.GetDB(txCtx, r.db).Model(&OutboxEventDelivery{}).
|
|
570
|
+
Where("event_id = ? AND target = ? AND locked_by = ? AND locked_at = ?", eventID, delivery.Target, *delivery.LockedBy, *delivery.LockedAt).
|
|
571
|
+
Updates(map[string]any{
|
|
572
|
+
"status": StatusDispatched,
|
|
573
|
+
"next_retry_at": nil,
|
|
574
|
+
"last_error": nil,
|
|
575
|
+
"dispatched_at": now,
|
|
576
|
+
"locked_at": nil,
|
|
577
|
+
"locked_by": nil,
|
|
578
|
+
"updated_at": now,
|
|
579
|
+
})
|
|
580
|
+
if result.Error != nil {
|
|
581
|
+
return fmt.Errorf("mark outbox delivery target dispatched: %w", result.Error)
|
|
582
|
+
}
|
|
583
|
+
if result.RowsAffected == 0 {
|
|
584
|
+
return fmt.Errorf("outbox delivery lease was superseded: %s", delivery.Target)
|
|
585
|
+
}
|
|
586
|
+
return nil
|
|
587
|
+
})
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
func (r *Relay) markDeliveryRetry(ctx context.Context, event *OutboxEvent, delivery *OutboxEventDelivery, dispatchErr error) error {
|
|
591
|
+
if delivery == nil || delivery.LockedAt == nil || delivery.LockedBy == nil {
|
|
592
|
+
return fmt.Errorf("outbox delivery lease is incomplete")
|
|
593
|
+
}
|
|
594
|
+
now := time.Now()
|
|
595
|
+
retryCount := nextRelayRetryCount(delivery.RetryCount)
|
|
596
|
+
maxRetries := delivery.MaxRetries
|
|
597
|
+
if maxRetries <= 0 {
|
|
598
|
+
maxRetries = r.deliveryMaxRetries(event)
|
|
599
|
+
}
|
|
600
|
+
errMsg := dispatchErr.Error()
|
|
601
|
+
permanent := IsPermanentDeliveryError(dispatchErr)
|
|
602
|
+
persistent := r.shouldRetryPersistently(event.EventType)
|
|
603
|
+
|
|
604
|
+
updates := map[string]any{
|
|
605
|
+
"retry_count": retryCount,
|
|
606
|
+
"max_retries": maxRetries,
|
|
607
|
+
"last_error": errMsg,
|
|
608
|
+
"locked_at": nil,
|
|
609
|
+
"locked_by": nil,
|
|
610
|
+
"updated_at": now,
|
|
611
|
+
}
|
|
612
|
+
if !persistent && (permanent || retryCount >= maxRetries) {
|
|
613
|
+
updates["status"] = StatusDeadLetter
|
|
614
|
+
updates["next_retry_at"] = nil
|
|
615
|
+
r.logger.Warn("outbox delivery target moved to dead letter",
|
|
616
|
+
zap.String("eventId", event.ID),
|
|
617
|
+
zap.String("eventType", event.EventType),
|
|
618
|
+
zap.String("target", delivery.Target),
|
|
619
|
+
zap.Int("retryCount", retryCount),
|
|
620
|
+
zap.Bool("permanent", permanent),
|
|
621
|
+
zap.String("lastError", errMsg),
|
|
622
|
+
)
|
|
623
|
+
} else {
|
|
624
|
+
updates["status"] = StatusRetryPending
|
|
625
|
+
updates["next_retry_at"] = now.Add(relayRetryBackoff(retryCount))
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
return r.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
|
629
|
+
result := database.GetDB(txCtx, r.db).Model(&OutboxEventDelivery{}).
|
|
630
|
+
Where("event_id = ? AND target = ? AND locked_by = ? AND locked_at = ?", event.ID, delivery.Target, *delivery.LockedBy, *delivery.LockedAt).
|
|
631
|
+
Updates(updates)
|
|
632
|
+
if result.Error != nil {
|
|
633
|
+
return fmt.Errorf("mark outbox delivery target retry: %w", result.Error)
|
|
634
|
+
}
|
|
635
|
+
if result.RowsAffected == 0 {
|
|
636
|
+
return fmt.Errorf("outbox delivery lease was superseded: %s", delivery.Target)
|
|
637
|
+
}
|
|
638
|
+
return nil
|
|
639
|
+
})
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
func (r *Relay) refreshParentStatus(ctx context.Context, event *OutboxEvent, targets []string) (string, error) {
|
|
643
|
+
if event == nil || event.LockedAt == nil || event.LockedBy == nil {
|
|
644
|
+
return "", fmt.Errorf("outbox event lease is incomplete")
|
|
645
|
+
}
|
|
646
|
+
if len(targets) == 0 {
|
|
647
|
+
return StatusDispatched, nil
|
|
648
|
+
}
|
|
649
|
+
eventID := event.ID
|
|
650
|
+
|
|
651
|
+
now := time.Now()
|
|
652
|
+
parentStatus := StatusDispatched
|
|
653
|
+
err := r.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
|
654
|
+
db := database.GetDB(txCtx, r.db)
|
|
655
|
+
var deliveries []OutboxEventDelivery
|
|
656
|
+
if err := db.
|
|
657
|
+
Select("event_id", "target", "status", "retry_count", "next_retry_at", "last_error").
|
|
658
|
+
Where("event_id = ? AND target IN ?", eventID, targets).
|
|
659
|
+
Find(&deliveries).Error; err != nil {
|
|
660
|
+
return fmt.Errorf("select outbox delivery target summary: %w", err)
|
|
661
|
+
}
|
|
662
|
+
if len(deliveries) != len(targets) {
|
|
663
|
+
return fmt.Errorf("outbox delivery target ledger incomplete for event %s", eventID)
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
allDispatched := true
|
|
667
|
+
hasDeadLetter := false
|
|
668
|
+
maxRetryCount := 0
|
|
669
|
+
var nextRetryAt *time.Time
|
|
670
|
+
var lastError string
|
|
671
|
+
hasLastError := false
|
|
672
|
+
|
|
673
|
+
for i := range deliveries {
|
|
674
|
+
delivery := &deliveries[i]
|
|
675
|
+
if delivery.Status != StatusDispatched {
|
|
676
|
+
allDispatched = false
|
|
677
|
+
}
|
|
678
|
+
if delivery.Status == StatusDeadLetter {
|
|
679
|
+
hasDeadLetter = true
|
|
680
|
+
}
|
|
681
|
+
if delivery.RetryCount > maxRetryCount {
|
|
682
|
+
maxRetryCount = delivery.RetryCount
|
|
683
|
+
}
|
|
684
|
+
if delivery.NextRetryAt != nil && (nextRetryAt == nil || delivery.NextRetryAt.Before(*nextRetryAt)) {
|
|
685
|
+
nextRetry := *delivery.NextRetryAt
|
|
686
|
+
nextRetryAt = &nextRetry
|
|
687
|
+
}
|
|
688
|
+
if delivery.LastError != nil {
|
|
689
|
+
lastError = *delivery.LastError
|
|
690
|
+
hasLastError = true
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
updates := map[string]any{
|
|
695
|
+
"retry_count": maxRetryCount,
|
|
696
|
+
"locked_at": nil,
|
|
697
|
+
"locked_by": nil,
|
|
698
|
+
}
|
|
699
|
+
switch {
|
|
700
|
+
case allDispatched:
|
|
701
|
+
parentStatus = StatusDispatched
|
|
702
|
+
updates["status"] = StatusDispatched
|
|
703
|
+
updates["next_retry_at"] = nil
|
|
704
|
+
updates["last_error"] = nil
|
|
705
|
+
case hasDeadLetter:
|
|
706
|
+
parentStatus = StatusDeadLetter
|
|
707
|
+
updates["status"] = StatusDeadLetter
|
|
708
|
+
updates["next_retry_at"] = nil
|
|
709
|
+
if hasLastError {
|
|
710
|
+
updates["last_error"] = lastError
|
|
711
|
+
} else {
|
|
712
|
+
updates["last_error"] = nil
|
|
713
|
+
}
|
|
714
|
+
default:
|
|
715
|
+
parentStatus = StatusRetryPending
|
|
716
|
+
updates["status"] = StatusRetryPending
|
|
717
|
+
if nextRetryAt == nil {
|
|
718
|
+
nextRetry := now
|
|
719
|
+
nextRetryAt = &nextRetry
|
|
720
|
+
}
|
|
721
|
+
updates["next_retry_at"] = nextRetryAt
|
|
722
|
+
if hasLastError {
|
|
723
|
+
updates["last_error"] = lastError
|
|
724
|
+
} else {
|
|
725
|
+
updates["last_error"] = nil
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
result := db.Model(&OutboxEvent{}).
|
|
730
|
+
Where("id = ? AND status = ? AND locked_by = ? AND locked_at = ?", eventID, StatusLocked, *event.LockedBy, *event.LockedAt).
|
|
731
|
+
Updates(updates)
|
|
732
|
+
if result.Error != nil {
|
|
733
|
+
return fmt.Errorf("refresh outbox parent status: %w", result.Error)
|
|
734
|
+
}
|
|
735
|
+
if result.RowsAffected == 0 {
|
|
736
|
+
return fmt.Errorf("outbox event lease was superseded")
|
|
737
|
+
}
|
|
738
|
+
return nil
|
|
739
|
+
})
|
|
740
|
+
if err != nil {
|
|
741
|
+
return "", err
|
|
742
|
+
}
|
|
743
|
+
return parentStatus, nil
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
func (r *Relay) deliveryMaxRetries(event *OutboxEvent) int {
|
|
747
|
+
if event.MaxRetries > 0 {
|
|
748
|
+
return event.MaxRetries
|
|
749
|
+
}
|
|
750
|
+
if r.config.MaxRetries > 0 {
|
|
751
|
+
return r.config.MaxRetries
|
|
752
|
+
}
|
|
753
|
+
return defaultRelayMaxRetries
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
func retryBackoff(retryCount int) time.Duration {
|
|
757
|
+
return relayRetryBackoff(retryCount)
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
func nextRelayRetryCount(current int) int {
|
|
761
|
+
const maxSmallInt = 32_767
|
|
762
|
+
if current >= maxSmallInt {
|
|
763
|
+
return maxSmallInt
|
|
764
|
+
}
|
|
765
|
+
return current + 1
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
func (r *Relay) shouldRetryPersistently(eventType string) bool {
|
|
769
|
+
return matchAnyEventPattern(r.config.PersistentRetryEvents, eventType)
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
func relayRetryBackoff(retryCount int) time.Duration {
|
|
773
|
+
const maxBackoff = 15 * time.Minute
|
|
774
|
+
if retryCount < 1 {
|
|
775
|
+
return time.Second
|
|
776
|
+
}
|
|
777
|
+
if retryCount >= 10 {
|
|
778
|
+
return maxBackoff
|
|
779
|
+
}
|
|
780
|
+
return time.Duration(1<<uint(retryCount)) * time.Second //nolint:gosec // retryCount 在位移前限制为 1..9。
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// toDomainEvent 将 OutboxEvent 转换为 EventBus DomainEvent
|
|
784
|
+
func (r *Relay) toDomainEvent(event *OutboxEvent) (eventbus.DomainEvent, error) {
|
|
785
|
+
// 还原事件载荷为具体类型(避免 listener unmarshalPayload 时 json.RawMessage double-encoding)
|
|
786
|
+
var payload any
|
|
787
|
+
if r.config.PayloadDeserializer != nil {
|
|
788
|
+
deserialized, err := r.config.PayloadDeserializer(event.EventType, event.Payload)
|
|
789
|
+
if err != nil {
|
|
790
|
+
return eventbus.DomainEvent{}, fmt.Errorf("deserialize outbox event payload %s (%s): %w", event.ID, event.EventType, err)
|
|
791
|
+
}
|
|
792
|
+
payload = deserialized
|
|
793
|
+
} else {
|
|
794
|
+
payload = json.RawMessage(event.Payload)
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
domainEvent := eventbus.DomainEvent{
|
|
798
|
+
EventName: event.EventType,
|
|
799
|
+
EventID: event.ID,
|
|
800
|
+
OccurredAt: event.OccurredAt,
|
|
801
|
+
AggregateID: event.AggregateID,
|
|
802
|
+
Version: AggregateVersionValue(event.AggregateVersion),
|
|
803
|
+
Payload: payload,
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// 还原元数据
|
|
807
|
+
if len(event.Metadata) > 0 {
|
|
808
|
+
var meta eventbus.EventMetadata
|
|
809
|
+
if err := json.Unmarshal(event.Metadata, &meta); err != nil {
|
|
810
|
+
r.logger.Warn("failed to unmarshal outbox event metadata",
|
|
811
|
+
zap.String("eventId", event.ID),
|
|
812
|
+
zap.Error(err),
|
|
813
|
+
)
|
|
814
|
+
} else {
|
|
815
|
+
domainEvent.Metadata = &meta
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
domainEvent.Metadata = eventbus.MetadataWithProductCode(
|
|
819
|
+
domainEvent.Metadata,
|
|
820
|
+
optionalStringValue(event.SourceProductCode),
|
|
821
|
+
)
|
|
822
|
+
|
|
823
|
+
// 从顶层信封列还原审计数据(对齐 AWS CloudTrail),失败只告警不阻塞分发。
|
|
824
|
+
if len(event.AuditChanges) > 0 {
|
|
825
|
+
changes := &eventbus.AuditChangesPayload{}
|
|
826
|
+
if err := json.Unmarshal(event.AuditChanges, changes); err != nil {
|
|
827
|
+
r.logger.Warn("failed to unmarshal outbox audit_changes",
|
|
828
|
+
zap.String("eventId", event.ID),
|
|
829
|
+
zap.Error(err),
|
|
830
|
+
)
|
|
831
|
+
} else {
|
|
832
|
+
domainEvent.AuditChanges = changes
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
if len(event.AuditMeta) > 0 {
|
|
836
|
+
meta := &eventbus.AuditMetadataPayload{}
|
|
837
|
+
if err := json.Unmarshal(event.AuditMeta, meta); err != nil {
|
|
838
|
+
r.logger.Warn("failed to unmarshal outbox audit_meta",
|
|
839
|
+
zap.String("eventId", event.ID),
|
|
840
|
+
zap.Error(err),
|
|
841
|
+
)
|
|
842
|
+
} else {
|
|
843
|
+
domainEvent.AuditMeta = meta
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
return domainEvent, nil
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
func matchAnyEventPattern(patterns []string, eventType string) bool {
|
|
851
|
+
for _, pattern := range patterns {
|
|
852
|
+
if matchEventPattern(pattern, eventType) {
|
|
853
|
+
return true
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return false
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
// optionalStringValue 返回可选字符串的规范化值。
|
|
860
|
+
func optionalStringValue(value *string) string {
|
|
861
|
+
if value == nil {
|
|
862
|
+
return ""
|
|
863
|
+
}
|
|
864
|
+
return strings.TrimSpace(*value)
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// AggregateVersionValue 将持久化信封中的可选聚合版本还原为领域事件版本。
|
|
868
|
+
func AggregateVersionValue(version *int64) int {
|
|
869
|
+
if version == nil {
|
|
870
|
+
return 0
|
|
871
|
+
}
|
|
872
|
+
return int(*version)
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// matchEventPattern 事件类型通配符匹配
|
|
876
|
+
// 支持 * 匹配单段,** 匹配任意后缀
|
|
877
|
+
func matchEventPattern(pattern, eventType string) bool {
|
|
878
|
+
if pattern == eventType {
|
|
879
|
+
return true
|
|
880
|
+
}
|
|
881
|
+
if pattern == "*" || pattern == "**" {
|
|
882
|
+
return true
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
patternParts := strings.Split(pattern, ".")
|
|
886
|
+
eventParts := strings.Split(eventType, ".")
|
|
887
|
+
|
|
888
|
+
// ** 结尾匹配任意后续段
|
|
889
|
+
if len(patternParts) > 0 && patternParts[len(patternParts)-1] == "**" {
|
|
890
|
+
prefix := patternParts[:len(patternParts)-1]
|
|
891
|
+
if len(eventParts) < len(prefix) {
|
|
892
|
+
return false
|
|
893
|
+
}
|
|
894
|
+
for i, p := range prefix {
|
|
895
|
+
if p != "*" && p != eventParts[i] {
|
|
896
|
+
return false
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
return true
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
if len(patternParts) != len(eventParts) {
|
|
903
|
+
return false
|
|
904
|
+
}
|
|
905
|
+
for i, p := range patternParts {
|
|
906
|
+
if p == "*" {
|
|
907
|
+
continue
|
|
908
|
+
}
|
|
909
|
+
if p != eventParts[i] {
|
|
910
|
+
return false
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return true
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// ═══════════════════════════════════════════════════════════════
|
|
917
|
+
// 维护循环
|
|
918
|
+
// ═══════════════════════════════════════════════════════════════
|
|
919
|
+
|
|
920
|
+
// runMaintenanceLoop 后台维护循环:回收超时锁 + 清理旧事件
|
|
921
|
+
func (r *Relay) runMaintenanceLoop(ctx context.Context) {
|
|
922
|
+
defer r.wg.Done()
|
|
923
|
+
|
|
924
|
+
reclaimTicker := time.NewTicker(r.config.LockTimeout / 2)
|
|
925
|
+
defer reclaimTicker.Stop()
|
|
926
|
+
|
|
927
|
+
cleanupTicker := time.NewTicker(r.config.CleanupInterval)
|
|
928
|
+
defer cleanupTicker.Stop()
|
|
929
|
+
|
|
930
|
+
for {
|
|
931
|
+
select {
|
|
932
|
+
case <-r.done:
|
|
933
|
+
return
|
|
934
|
+
case <-ctx.Done():
|
|
935
|
+
return
|
|
936
|
+
case <-reclaimTicker.C:
|
|
937
|
+
r.reclaimStaleLocks(ctx)
|
|
938
|
+
case <-cleanupTicker.C:
|
|
939
|
+
r.cleanupDispatched(ctx)
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
// reclaimStaleLocks 回收超时的锁定事件,重新设为 pending
|
|
945
|
+
func (r *Relay) reclaimStaleLocks(ctx context.Context) {
|
|
946
|
+
cutoff := time.Now().Add(-r.config.LockTimeout)
|
|
947
|
+
now := time.Now()
|
|
948
|
+
var reclaimed int64
|
|
949
|
+
err := r.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
|
950
|
+
db := database.GetDB(txCtx, r.db)
|
|
951
|
+
var eventIDs []string
|
|
952
|
+
if err := db.Model(&OutboxEvent{}).
|
|
953
|
+
Where("status = ? AND locked_at < ?", StatusLocked, cutoff).
|
|
954
|
+
Pluck("id", &eventIDs).Error; err != nil {
|
|
955
|
+
return fmt.Errorf("select stale outbox locks: %w", err)
|
|
956
|
+
}
|
|
957
|
+
if len(eventIDs) == 0 {
|
|
958
|
+
return nil
|
|
959
|
+
}
|
|
960
|
+
result := db.Model(&OutboxEvent{}).
|
|
961
|
+
Where("id IN ?", eventIDs).
|
|
962
|
+
Updates(map[string]any{
|
|
963
|
+
"status": StatusPending,
|
|
964
|
+
"locked_at": nil,
|
|
965
|
+
"locked_by": nil,
|
|
966
|
+
})
|
|
967
|
+
if result.Error != nil {
|
|
968
|
+
return fmt.Errorf("reclaim stale outbox locks: %w", result.Error)
|
|
969
|
+
}
|
|
970
|
+
reclaimed = result.RowsAffected
|
|
971
|
+
if err := db.Model(&OutboxEventDelivery{}).
|
|
972
|
+
Where("event_id IN ?", eventIDs).
|
|
973
|
+
Updates(map[string]any{
|
|
974
|
+
"locked_at": nil,
|
|
975
|
+
"locked_by": nil,
|
|
976
|
+
"updated_at": now,
|
|
977
|
+
}).Error; err != nil {
|
|
978
|
+
return fmt.Errorf("reclaim stale outbox delivery locks: %w", err)
|
|
979
|
+
}
|
|
980
|
+
return nil
|
|
981
|
+
})
|
|
982
|
+
if err != nil {
|
|
983
|
+
r.logger.Error("failed to reclaim stale outbox locks", zap.Error(err))
|
|
984
|
+
return
|
|
985
|
+
}
|
|
986
|
+
if reclaimed > 0 {
|
|
987
|
+
r.logger.Info("reclaimed stale outbox locks",
|
|
988
|
+
zap.Int64("count", reclaimed),
|
|
989
|
+
zap.Time("cutoff", cutoff),
|
|
990
|
+
)
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// cleanupDispatched 清理已处理的旧事件
|
|
995
|
+
func (r *Relay) cleanupDispatched(ctx context.Context) {
|
|
996
|
+
cutoff := time.Now().Add(-r.config.RetentionDuration)
|
|
997
|
+
var cleaned int64
|
|
998
|
+
err := r.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
|
999
|
+
db := database.GetDB(txCtx, r.db)
|
|
1000
|
+
var eventIDs []string
|
|
1001
|
+
if err := db.Model(&OutboxEvent{}).
|
|
1002
|
+
Where("status = ? AND created_at < ?", StatusDispatched, cutoff).
|
|
1003
|
+
Pluck("id", &eventIDs).Error; err != nil {
|
|
1004
|
+
return fmt.Errorf("select dispatched outbox events for cleanup: %w", err)
|
|
1005
|
+
}
|
|
1006
|
+
if len(eventIDs) == 0 {
|
|
1007
|
+
return nil
|
|
1008
|
+
}
|
|
1009
|
+
if err := db.Where("event_id IN ?", eventIDs).
|
|
1010
|
+
Delete(&OutboxEventDelivery{}).Error; err != nil {
|
|
1011
|
+
return fmt.Errorf("cleanup outbox delivery targets: %w", err)
|
|
1012
|
+
}
|
|
1013
|
+
result := db.Where("id IN ?", eventIDs).Delete(&OutboxEvent{})
|
|
1014
|
+
if result.Error != nil {
|
|
1015
|
+
return fmt.Errorf("cleanup dispatched outbox events: %w", result.Error)
|
|
1016
|
+
}
|
|
1017
|
+
cleaned = result.RowsAffected
|
|
1018
|
+
return nil
|
|
1019
|
+
})
|
|
1020
|
+
if err != nil {
|
|
1021
|
+
r.logger.Error("failed to cleanup dispatched outbox events", zap.Error(err))
|
|
1022
|
+
return
|
|
1023
|
+
}
|
|
1024
|
+
if cleaned > 0 {
|
|
1025
|
+
r.logger.Info("cleaned up dispatched outbox events",
|
|
1026
|
+
zap.Int64("count", cleaned),
|
|
1027
|
+
zap.Time("cutoff", cutoff),
|
|
1028
|
+
)
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1033
|
+
// PostgreSQL LISTEN/NOTIFY
|
|
1034
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1035
|
+
|
|
1036
|
+
// listenNotify 监听 PostgreSQL NOTIFY 通道实现低延迟唤醒
|
|
1037
|
+
//
|
|
1038
|
+
// 使用 pgx 驱动原生连接执行 LISTEN 和等待通知。
|
|
1039
|
+
// 连接断开时自动重连。
|
|
1040
|
+
func (r *Relay) listenNotify(ctx context.Context) {
|
|
1041
|
+
defer r.wg.Done()
|
|
1042
|
+
|
|
1043
|
+
for {
|
|
1044
|
+
select {
|
|
1045
|
+
case <-r.done:
|
|
1046
|
+
return
|
|
1047
|
+
case <-ctx.Done():
|
|
1048
|
+
return
|
|
1049
|
+
default:
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
if err := r.listenOnce(ctx); err != nil {
|
|
1053
|
+
if ctx.Err() != nil {
|
|
1054
|
+
return
|
|
1055
|
+
}
|
|
1056
|
+
select {
|
|
1057
|
+
case <-r.done:
|
|
1058
|
+
return
|
|
1059
|
+
default:
|
|
1060
|
+
}
|
|
1061
|
+
r.logger.Warn("LISTEN/NOTIFY connection lost, reconnecting",
|
|
1062
|
+
zap.Error(err),
|
|
1063
|
+
)
|
|
1064
|
+
// 重连延迟,避免紧密循环
|
|
1065
|
+
select {
|
|
1066
|
+
case <-time.After(3 * time.Second):
|
|
1067
|
+
case <-r.done:
|
|
1068
|
+
return
|
|
1069
|
+
case <-ctx.Done():
|
|
1070
|
+
return
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
// listenOnce 获取一个原生 pgx 连接,执行 LISTEN 并阻塞等待通知
|
|
1077
|
+
func (r *Relay) listenOnce(ctx context.Context) error {
|
|
1078
|
+
sqlDB, err := r.db.DB()
|
|
1079
|
+
if err != nil {
|
|
1080
|
+
return fmt.Errorf("get sql.DB: %w", err)
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
conn, err := sqlDB.Conn(ctx)
|
|
1084
|
+
if err != nil {
|
|
1085
|
+
return fmt.Errorf("acquire connection: %w", err)
|
|
1086
|
+
}
|
|
1087
|
+
defer conn.Close()
|
|
1088
|
+
|
|
1089
|
+
// 通过 Raw 访问底层 pgx 连接执行 LISTEN
|
|
1090
|
+
// any: database/sql.Conn.Raw 回调参数为底层驱动连接
|
|
1091
|
+
return conn.Raw(func(driverConn any) error {
|
|
1092
|
+
stdlibConn, ok := driverConn.(*stdlib.Conn)
|
|
1093
|
+
if !ok {
|
|
1094
|
+
return fmt.Errorf("unsupported driver for LISTEN/NOTIFY: %T", driverConn)
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
pgxConn := stdlibConn.Conn()
|
|
1098
|
+
|
|
1099
|
+
// 执行 LISTEN
|
|
1100
|
+
if _, execErr := pgxConn.Exec(ctx, "LISTEN outbox_events"); execErr != nil {
|
|
1101
|
+
return fmt.Errorf("LISTEN: %w", execErr)
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
r.logger.Debug("LISTEN outbox_events established")
|
|
1105
|
+
|
|
1106
|
+
// 阻塞等待通知(pgx.Conn.WaitForNotification 返回 *pgconn.Notification, error)
|
|
1107
|
+
for {
|
|
1108
|
+
notification, waitErr := pgxConn.WaitForNotification(ctx)
|
|
1109
|
+
if waitErr != nil {
|
|
1110
|
+
return fmt.Errorf("wait for notification: %w", waitErr)
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
r.logger.Debug("pg_notify received",
|
|
1114
|
+
zap.String("channel", notification.Channel),
|
|
1115
|
+
zap.Int("payloadBytes", len(notification.Payload)),
|
|
1116
|
+
)
|
|
1117
|
+
// 非阻塞唤醒轮询循环
|
|
1118
|
+
select {
|
|
1119
|
+
case r.wakeup <- struct{}{}:
|
|
1120
|
+
default:
|
|
1121
|
+
// 已有待处理的唤醒信号,跳过
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
})
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1128
|
+
// 辅助函数
|
|
1129
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1130
|
+
|
|
1131
|
+
// generateRelayID 生成 Relay 实例标识(hostname + 短 UUID)
|
|
1132
|
+
func generateRelayID() string {
|
|
1133
|
+
hostname, err := os.Hostname()
|
|
1134
|
+
if err != nil {
|
|
1135
|
+
hostname = "unknown"
|
|
1136
|
+
}
|
|
1137
|
+
return fmt.Sprintf("%s-%s", hostname, uuid.New().String()[:8])
|
|
1138
|
+
}
|