# Getting Started with WebServ

## Installation

WebServ is built with modern C++20 and requires a recent compiler and CMake for building.

### Prerequisites

- C++20 compatible compiler (GCC 10+, Clang 12+)
- CMake 3.22 or higher
- Make build system
- Linux operating system (epoll-based)

### Building from Source

    # Clone the repository
    git clone https://github.com/WHaffmans/webserv.git
    cd webserv

    # Build the project
    make

    # Or build with specific configuration
    make build_type=release
    make build_type=debug
    make build_type=asan

💡 Build Types:
• `debug`: Debug symbols, no optimization
• `release`: Optimized build for production
• `asan`: Address sanitizer for memory debugging

## Basic Configuration

WebServ uses nginx-style configuration files for maximum flexibility and familiarity.

### Minimal Configuration

    # webserv.conf
    server {
        listen 8080;
        server_name localhost;
        root ./www;
        index index.html;

        location / {
            try_files $uri $uri/ =404;
        }
    }

### Advanced Configuration

    # Advanced webserv.conf
    server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/html;
        index index.html index.htm;

        # Error pages
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;

        # Static files with caching
        location /static/ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }

        # API endpoints
        location /api/ {
            proxy_pass http://127.0.0.1:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }

        # Directory listing
        location /files/ {
            autoindex on;
            autoindex_exact_size off;
        }
    }

    # HTTPS server
    server {
        listen 443 ssl;
        server_name example.com;

        ssl_certificate /path/to/cert.pem;
        ssl_certificate_key /path/to/key.pem;

        root /var/www/ssl;
        index index.html;
    }

## Running the Server

### Basic Usage

    # Run with default configuration
    ./build/webserv

    # Run with custom configuration file
    ./build/webserv config/webserv.conf

    # Run with make target (rebuilds if needed)
    make run

### Command Line Options

    Usage: webserv [config_file]

    Arguments:
      config_file    Path to configuration file (default: webserv.conf)

    Examples:
      ./webserv                    # Use default config
      ./webserv /etc/webserv.conf  # Use specific config
      ./webserv --help             # Show help message

⚠️ Important: Make sure the specified ports are not already in use. WebServ will fail to start if the ports are occupied.

## Testing Your Setup

### Basic Connectivity Test

    # Test with curl
    curl http://localhost:8080/

    # Test with specific headers
    curl -H "Host: example.com" http://localhost:8080/

    # Test POST request
    curl -X POST -d "test=data" http://localhost:8080/api/test

### Performance Testing

    # Apache Bench
    ab -n 1000 -c 10 http://localhost:8080/

    # wrk load testing
    wrk -t12 -c400 -d30s http://localhost:8080/

    # siege testing
    siege -c 50 -t 30s http://localhost:8080/

### Running Unit Tests

    # Build and run tests
    make test

    # Run specific test suite
    cd build && ctest -V

    # Run with Google Test directly
    ./build/webserv_tests

## Troubleshooting

### Common Issues

#### Port Already in Use

    # Check what's using the port
    sudo netstat -tlnp | grep :8080
    sudo lsof -i :8080

    # Kill process using the port
    sudo kill -9 <PID>

#### Permission Denied

    # For ports below 1024, run as root
    sudo ./build/webserv

    # Or use a port above 1024
    # Edit config: listen 8080;

#### Configuration Errors

    # Check configuration syntax
    ./build/webserv -t config/webserv.conf

    # Debug mode for detailed logging
    ./build/webserv_debug config/webserv.conf

### Log Files

WebServ logs are written to both console and log files:

    # Default log locations
    ./logs/access.log    # Access logs
    ./logs/error.log     # Error logs
    ./logs/debug.log     # Debug logs (debug build only)

    # Tail logs in real-time
    tail -f logs/access.log
    tail -f logs/error.log

### Debug Build

For development and troubleshooting, use the debug build:

    # Build debug version
    make build_type=debug

    # Run with debug logging
    ./build/webserv_debug

    # Use with GDB for debugging
    gdb ./build/webserv_debug
    (gdb) run config/webserv.conf

💡 Pro Tip: Use the address sanitizer build (`make build_type=asan`) to catch memory errors during development.

Need more help? Check out the Configuration Guide or API Reference.