Class Main
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:
- Initialize ServerLogger with DEBUG level (will be overridden by config)
- Load environment-specific configuration from config/config.{environment}.yaml
- Update logger configuration with the loaded log level
- Initialize VirtualHostManager with the default host from configuration
- Register all configured virtual hosts for name-based virtual hosting
- Create HTTPHandler with the virtual host manager
- Initialize virtual thread executor for concurrent request handling
- Configure ServerLogger with SSE handler for real-time log broadcasting
- 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 Summary
Constructors -
Method Summary
-
Constructor Details
-
Main
public Main()
-
-
Method Details
-
main
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:
- Initialize ServerLogger with temporary DEBUG level
- Load ServerConfig from environment-specific YAML file
- Apply configured log level from ServerConfig
- Create VirtualHostManager with default host from config
- Register all virtual hosts from configuration
- Initialize HTTPHandler for request processing
- Create virtual thread executor for request concurrency
- Configure SSE handler for real-time log broadcasting
- 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:
-