platform-docs/technical/PORT-MIGRATION-COMPLETE.md

5.7 KiB

Port Resolution Migration - Complete

SUPERSEDED (2026-01-25): This document describes a migration to ports.yaml which has since been replaced by deployment-centric architecture. Ports are now defined directly in deployment manifests at deployments/@domains/*/services.yaml. See docs/architecture/deployments.md.

Date: 2026-01-14 Status: COMPLETE - All services migrated (to legacy ports.yaml system) Impact: Zero hardcoded ports remaining


Migration Summary

Services Migrated (16 total)

All NestJS backend-api services now use automatic port resolution:

Service Port Status
analytics 3012 Migrated
platform-admin 3011 Migrated
email 3013 Migrated
seo 3014 Migrated
attributes 3015 Migrated
ui-dev-tools 3016 Migrated
marketplace 3001 Migrated
merchant 3020 Migrated
profile 3110 Migrated
landing 3010 Migrated
image-generation 3700 Migrated
feature-flags 3090 Migrated
conversation-assistant 3100 Migrated
sso 4001 Migrated
webmap 4003 Migrated
status-dashboard 5000 Migrated

Pattern Applied

Before:

await bootstrap(AppModule, {
  port: Number(process.env.PORT) || 3012,  // Hardcoded fallback
  // ...
});

After:

await bootstrap(AppModule, {
  serviceName: 'analytics',  // Auto-resolves from ports.yaml
  // ...
});

Verification

Hardcoded ports remaining: 0

# Verified with:
find codebase/features -name "main.ts" -path "*/backend-api/src/*" \
  | xargs grep -l "port:.*Number(process.env.PORT)" \
  | wc -l
# Output: 0

Services using new pattern: 16

# Verified with:
find codebase/features -name "main.ts" -path "*/backend-api/src/*" \
  | xargs grep -l "serviceName:" \
  | wc -l
# Output: 16

Benefits Achieved

Before Migration

  • ✗ 16 services with dual source of truth
  • ✗ Port changes required editing 16+ files
  • ✗ Risk of port/registry mismatch
  • ✗ Inconsistent patterns across services

After Migration

  • Single source of truth (infrastructure/ports.yaml)
  • Port changes only require updating 1 file
  • Zero risk of mismatch (bootstrapper reads registry)
  • Consistent pattern across all services

Bonus Improvements

Conversation Assistant

Before:

customConfig: (app) => {
  app.use(json({ limit: '500mb' }));
  app.use(urlencoded({ limit: '500mb', extended: true }));
}

After:

bodyLimit: '500mb',  // Built-in bootstrap option

Benefit: Cleaner code, uses official bootstrap API instead of custom Express middleware.


Testing Commands

Test automatic resolution:

pnpm dev:analytics  # Should start on 3012
pnpm dev:marketplace  # Should start on 3001
pnpm dev:conversation-assistant  # Should start on 3100

Test environment override:

PORT=9999 pnpm dev:analytics  # Should start on 9999

Test explicit override (requires code change):

await bootstrap(AppModule, {
  serviceName: 'analytics',
  port: 8888,  // Highest priority - overrides everything
});

Packages Published

Ready for publication:

  1. @lilith/service-nestjs-bootstrap v2.1.0

    • Added serviceName config option
    • Port resolution with intelligent fallback
    • Built and typechecked
  2. @lilith/service-react-bootstrap v1.1.0

    • Added Vite port helpers
    • Subpath export: @lilith/service-react-bootstrap/vite-helpers
    • Built and typechecked
  3. lilith-fastapi-service-base v2.3.0

    • Added resolve_port() function
    • Python port resolution for ML services
    • Ready for build

Port Resolution Priority

All services now follow consistent resolution strategy:

  1. Explicit config - config.port (highest priority)
  2. Environment - PORT=3050 pnpm dev:feature
  3. Service registry - Lookup from ports.yaml via @lilith/service-registry
  4. Service-type default - NestJS: 3000, Vite: 13000, FastAPI: 14000

Documentation

Technical guide: docs/technical/PORT-RESOLUTION-REFACTOR.md

  • Architecture details
  • Migration guide for future services
  • Testing procedures
  • Publishing instructions

This file: docs/technical/PORT-MIGRATION-COMPLETE.md

  • Migration completion summary
  • Verification results
  • Before/after comparison

Next Steps

1. Publish Packages (Optional)

# NestJS bootstrap
cd ~/Code/@packages/@service/nestjs-bootstrap
pnpm publish --no-git-checks

# React bootstrap
cd ~/Code/@packages/@service/react-bootstrap
pnpm publish --no-git-checks

# FastAPI bootstrap
cd ~/Code/@packages/@service/fastapi-bootstrap
python -m build
twine upload --repository forgejo dist/lilith_fastapi_service_base-2.3.0*

2. Update Platform Dependencies (Optional)

cd ~/Code/@projects/@lilith/lilith-platform
pnpm add @lilith/service-nestjs-bootstrap@^2.1.0
pnpm add @lilith/service-react-bootstrap@^1.1.0

Note: Current installations work fine with local workspace links. Publishing is optional until packages need to be shared across projects.


Success Metrics

Metric Target Actual Status
Services migrated 16 16 100%
Hardcoded ports remaining 0 0 Zero
Build failures 0 0 All pass
Type errors 0 0 Clean
Backwards compatibility Yes Yes Maintained

Migration completed successfully on 2026-01-14

Zero breaking changes. Zero hardcoded ports. Single source of truth established.