5.8 KiB
5.8 KiB
Proxy Detection API - Code Organization
This document describes the organized file structure of the Proxy Detection API.
File Structure
The application has been organized into separate modules for better maintainability:
Core Files
index.ts- Entry point for the applicationserver.ts- Server setup and initializationroutes.ts- Route definitions and registration
Module Files
types.ts- TypeScript type definitions and interfacesconfig.ts- Configuration and environment variablesmiddleware.ts- Authentication and other middleware functionshandlers.ts- Route handler functionsutils.ts- Utility functions for IP detection and processing
Service Files
proxyService.ts- IP extraction service (existing)
Architecture Overview
┌─────────────────────────────────────────────────────────────┐
│ index.ts │
│ (Entry Point) │
└─────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────▼───────────────────────────────────────┐
│ server.ts │
│ (Server Configuration) │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐│
│ │ config │ │ middleware │ │ routes ││
│ └─────────────┘ └─────────────┘ └─────────────────────────┘│
└─────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────▼───────────────────────────────────────┐
│ handlers.ts │
│ (Route Handlers) │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐│
│ │ utils │ │ types │ │ proxyService ││
│ └─────────────┘ └─────────────┘ └─────────────────────────┘│
└─────────────────────────────────────────────────────────────┘
Module Responsibilities
types.ts
ApiResponse- Main API response interfaceHealthResponse- Health check response interfaceIPChainResult- IP chain result interfaceDetailedDebugResponse- Debug endpoint response interface
config.ts
- Environment variable management
- Server configuration settings
- CORS and trust proxy settings
- Application constants
utils.ts
getClientIP()- Enhanced IP detection with 30+ header sourcesgetFullIPChain()- Extract full proxy chain informationnormalizeHeaders()- Normalize headers to consistent formatextractGeolocation()- Extract geolocation from headersextractProxyInfo()- Extract proxy information from headers
middleware.ts
authMiddleware()- API key authentication- Handles both header and query parameter authentication
- Skips authentication for health and debug endpoints
handlers.ts
healthHandler()- Health check endpointmainHandler()- Main proxy detection endpointrandomHandler()- Random cached result endpointdetailedDebugHandler()- Detailed debug information endpoint- In-memory result caching management
routes.ts
- Route registration and organization
- Maps endpoints to their respective handlers
- Maintains authentication bypass configuration
server.ts
- Fastify server instance creation
- CORS configuration
- Middleware registration
- Server startup and error handling
Benefits of This Organization
- Separation of Concerns - Each file has a specific responsibility
- Maintainability - Easier to locate and modify specific functionality
- Testability - Individual modules can be unit tested independently
- Reusability - Utility functions can be easily reused
- Type Safety - Centralized type definitions ensure consistency
- Configuration Management - All settings are centralized and manageable
Development Workflow
- Adding new endpoints: Add handler to
handlers.ts, register route inroutes.ts - Modifying IP detection: Update functions in
utils.ts - Changing configuration: Update
config.ts - Adding new types: Add to
types.ts - Adding middleware: Add to
middleware.ts
Running the Application
The application starts from index.ts which:
- Creates a server instance
- Configures middleware and routes
- Starts the server
- Handles startup errors
All functionality remains the same as the original monolithic version, but is now organized for better maintainability and scalability.