Refactor main.lua: Extract health routes and reduce to <200 lines #96

Closed
opened 2025-08-31 08:08:07 +02:00 by michael · 1 comment
Owner

Problem

main.lua has grown to 342 lines and violates our <200 lines per file principle.

Current Structure Analysis

  • Server setup/configuration: ~50 lines
  • Request parsing/handling: ~80 lines
  • CORS/response creation: ~60 lines
  • Route registration: ~40 lines
  • Health endpoints: ~60 lines
  • Version integration: ~30 lines

Proposed Refactoring

1. Extract Health Routes Module

Create src/routes/health.lua following existing pattern:

  • /health endpoint (basic health check)
  • /health/config endpoint (config drift)
  • Future health endpoints (metrics, status)

2. Extract Server Core

Create src/http_server.lua for:

  • HTTP parsing/response creation
  • CORS handling
  • Client connection management

3. Slim main.lua Target

Reduce to core orchestration (~150 lines):

  • Configuration loading
  • Route registration
  • Server startup
  • Version info integration

Target Structure

src/
├── main.lua              (~150 lines - orchestration)
├── http_server.lua       (~120 lines - HTTP core)
└── routes/
    ├── health.lua         (~80 lines - health endpoints)
    ├── mail.lua          (existing)
    └── auth.lua          (existing)

Success Criteria

  • main.lua under 200 lines
  • Health routes follow existing pattern
  • All functionality preserved
  • No breaking changes to API
  • Clean separation of concerns
## Problem main.lua has grown to 342 lines and violates our <200 lines per file principle. ## Current Structure Analysis - Server setup/configuration: ~50 lines - Request parsing/handling: ~80 lines - CORS/response creation: ~60 lines - Route registration: ~40 lines - Health endpoints: ~60 lines - Version integration: ~30 lines ## Proposed Refactoring ### 1. Extract Health Routes Module Create **src/routes/health.lua** following existing pattern: - /health endpoint (basic health check) - /health/config endpoint (config drift) - Future health endpoints (metrics, status) ### 2. Extract Server Core Create **src/http_server.lua** for: - HTTP parsing/response creation - CORS handling - Client connection management ### 3. Slim main.lua Target Reduce to core orchestration (~150 lines): - Configuration loading - Route registration - Server startup - Version info integration ## Target Structure ``` src/ ├── main.lua (~150 lines - orchestration) ├── http_server.lua (~120 lines - HTTP core) └── routes/ ├── health.lua (~80 lines - health endpoints) ├── mail.lua (existing) └── auth.lua (existing) ``` ## Success Criteria - [X] main.lua under 200 lines - [X] Health routes follow existing pattern - [X] All functionality preserved - [X] No breaking changes to API - [X] Clean separation of concerns
michael added the
effort
medium
priority
high
type/refactor
labels 2025-08-31 08:08:07 +02:00
michael added this to the v0.1.2 - Gateway Basics milestone 2025-08-31 19:10:40 +02:00
Author
Owner

Refactoring Complete

Files created:

  • `src/routes/health.lua` (80 lines) - Health monitoring endpoints
  • `src/http_server.lua` (256 lines) - HTTP server core functionality

Main refactoring:

  • `src/main.lua`: 342 → 27 lines (92% reduction)
  • Pure orchestration: imports, route registration, server start
  • Zero breaking changes - all functionality preserved

Testing completed:

  • Health endpoint: `curl localhost:7811/health`
  • Test endpoint: `curl -X POST localhost:7811/test`
  • Server startup: All configs and logging working

Ready for service self-registration architecture.

## ✅ Refactoring Complete **Files created:** - \`src/routes/health.lua\` (80 lines) - Health monitoring endpoints - \`src/http_server.lua\` (256 lines) - HTTP server core functionality **Main refactoring:** - \`src/main.lua\`: 342 → 27 lines (92% reduction) - Pure orchestration: imports, route registration, server start - Zero breaking changes - all functionality preserved **Testing completed:** - Health endpoint: \`curl localhost:7811/health\` ✅ - Test endpoint: \`curl -X POST localhost:7811/test\` ✅ - Server startup: All configs and logging working ✅ **Ready for service self-registration architecture.**
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: DAW/furt#96
No description provided.