Skip to content

Refactor API startup into an app factory #11

@Lakes41

Description

@Lakes41

Summary

Separate Fastify app construction from process startup so the API is easier to test, reuse, and shut down cleanly.

Current Behaviour

apps/access-api/src/index.ts creates the Fastify server, registers plugins and routes, then immediately starts listening. This makes tests harder to write and couples route registration to process lifecycle.

Expected Behaviour

The API should expose a buildApp or createServer function that returns a configured Fastify instance. The entrypoint should only handle environment loading, startup, and shutdown.

Suggested Implementation

Create a new app module that registers Swagger, Swagger UI, and routes. Update index.ts to call this factory, parse environment configuration, start the server, and close resources on shutdown signals.

Files or Areas Likely Affected

  • apps/access-api/src/index.ts
  • apps/access-api/src/app.ts
  • apps/access-api/src/routes.ts
  • apps/access-api/src/services/prisma.ts
  • apps/access-api/src/**/*.test.ts

Acceptance Criteria

  • Fastify app construction is exported from a reusable function
  • index.ts remains responsible for process startup only
  • Tests can instantiate the app without opening a network port
  • Prisma is disconnected during graceful shutdown
  • Startup errors are logged and still exit with a non-zero status

Additional Notes

This pairs well with route integration tests but can be implemented independently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official Campaign

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions