Class Main

java.lang.Object
com.ns.webserver.Main

public class Main extends Object
Main application entry point for the HTTP web server.

This class bootstraps and initializes the complete HTTP server infrastructure, including:

  • Configuration loading from YAML files based on the environment
  • Logging system initialization with SSE broadcasting support
  • Virtual host management for multi-domain hosting
  • Thread pool configuration using virtual threads for high concurrency
  • TCP server setup and startup

The server supports multiple deployment environments (development, staging, production) with environment-specific configurations loaded from YAML files in the config directory. The environment is specified as a command-line argument, defaulting to "prod" if not provided.

Initialization sequence:

  1. Initialize ServerLogger with DEBUG level (will be overridden by config)
  2. Load environment-specific configuration from config/config.{environment}.yaml
  3. Update logger configuration with the loaded log level
  4. Initialize VirtualHostManager with the default host from configuration
  5. Register all configured virtual hosts for name-based virtual hosting
  6. Create HTTPHandler with the virtual host manager
  7. Initialize virtual thread executor for concurrent request handling
  8. Configure ServerLogger with SSE handler for real-time log broadcasting
  9. Create and start TCPServer on the configured port

Virtual Threads: The server uses Java's virtual threads (Project Loom) via Executors.newVirtualThreadPerTaskExecutor(), enabling high-concurrency request handling with minimal resource overhead compared to platform threads.

Server-Sent Events: If an SSE handler is configured in any virtual host, the server logger will broadcast log messages to all connected SSE clients in real-time, enabling live monitoring and debugging capabilities.

Example usage:

// Start server in development mode
java -jar webserver.jar dev

// Start server in production mode (default)
java -jar webserver.jar
// or
java -jar webserver.jar prod

Error handling: If the server fails to start (e.g., port already in use, configuration error), an error is logged via ServerLogger and the application continues to run without the TCP server. Consider adding System.exit(1) for production deployments to ensure failed startups are properly reported.

See Also:
  • Constructor Details

    • Main

      public Main()
  • Method Details

    • main

      public static void main(String[] args) throws IOException
      Main entry point for the HTTP web server application.

      This method orchestrates the complete server initialization and startup process. It loads configuration, sets up virtual hosting, initializes the thread pool, configures logging with SSE support, and starts the TCP server.

      Command-line arguments:

      • args[0] (optional): Environment name (e.g., "dev", "staging", "prod"). Defaults to "prod" if not specified. This determines which configuration file is loaded from config/config.{environment}.yaml

      Configuration files: The server expects YAML configuration files in the following locations:

      • External: config/config.{environment}.yaml (checked first)
      • Classpath: config/config.{environment}.yaml (fallback)

      Initialization steps performed:

      1. Initialize ServerLogger with temporary DEBUG level
      2. Load ServerConfig from environment-specific YAML file
      3. Apply configured log level from ServerConfig
      4. Create VirtualHostManager with default host from config
      5. Register all virtual hosts from configuration
      6. Initialize HTTPHandler for request processing
      7. Create virtual thread executor for request concurrency
      8. Configure SSE handler for real-time log broadcasting
      9. Create and start TCPServer on configured port

      Thread model: The server uses virtual threads for request handling, allowing thousands of concurrent connections without the overhead of platform threads. Each request is processed in its own virtual thread, providing natural blocking I/O patterns without performance penalties.

      Error handling: Exceptions during server startup are caught and logged via ServerLogger. The application continues running even if the TCP server fails to start, which may not be desirable in production environments.

      Example invocations:

      // Development environment with debug logging
      java com.ns.webserver.Main dev
      
      // Production environment (default)
      java com.ns.webserver.Main
      java com.ns.webserver.Main prod
      
      // Staging environment
      java com.ns.webserver.Main staging
      
      Parameters:
      args - Command-line arguments. The first argument (if present) specifies the environment name for configuration loading. Defaults to "prod" if no arguments are provided.
      Throws:
      IOException - If an I/O error occurs during configuration loading. This is currently not handled and will terminate the application.
      See Also: