Skip to content

Refactor: Extract server.js setup into focused modules #118

@melvincarvalho

Description

@melvincarvalho

Problem

src/server.js is 572 lines / 24KB mixing multiple concerns:

  • Lines 1-20: Imports (17 imports)
  • Lines 44-114: Option extraction/defaults (70 lines)
  • Lines 129-170: Config distribution to request
  • Lines 172-214: Plugin registration (8 plugins)
  • Lines 216-277: Security & authorization hooks
  • Lines 279-442: Route setup & middleware
  • Lines 461-572: Single-user pod creation logic

Issues:

  1. 45 route definitions mixed with middleware setup
  2. 8+ plugins tightly coupled via direct imports
  3. Single-user pod setup mixes startup logic with route setup
  4. Plugin registration scattered across 30+ lines

Proposed Solution

Create focused modules:

// server.js - ONLY create and return Fastify instance
export function createServer(options) {
  const fastify = Fastify(fastifyOptions);
  setupContentParsing(fastify);
  setupRequestConfig(fastify, options);  // from ./setup/config.js
  setupSecurityHooks(fastify, options);  // from ./setup/security.js
  registerPlugins(fastify, options);     // from ./setup/plugins.js
  setupRoutes(fastify, options);         // from ./setup/routes.js
  setupSingleUserMode(fastify, options); // from ./setup/single-user.js
  return fastify;
}

New file structure:

src/
  ├── server.js          (slim orchestrator)
  └── setup/
      ├── config.js      (request config decoration)
      ├── security.js    (auth hooks, dotfile blocking)
      ├── plugins.js     (IdP, Nostr, AP, notifications)
      ├── routes.js      (LDP routes)
      └── single-user.js (pod creation on startup)

Benefits

  • server.js becomes ~50 lines
  • Each setup module testable independently
  • Plugins loosely coupled via dynamic imports
  • Clear initialization order

Priority

P1 - High impact on readability and testability

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: highP1 - Do soon, significant improvementrefactorCode refactoring

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions