Skip to content

NatanR-dev/webserver-in-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webserver in C

A Minimal cross-platform HTTP server built in C.

demo.mp4
  • Opens a 8080 port.
    • Provides a Webserver API with routing system.
    • Root path with hateoas listing:
      • GET /api/machine
      • POST /api/machine
      • GET /api/os
      • GET /api/system
  • Uses sockets (sys/socket.h, netinet/in.h).
    • Threads.
    • JSON formatting.
    • Platform-specific implementations.
  • Supports Client connections in a loop (handle requests).
    • MAX ROUTES
    • BUFFER_SIZE
    • MAX_CLIENTS
    • PORT
    • Logs
  • Responds with a JSON or HTML.
    • GET returns (ok)
      • HTTP 200 status code.
    • POST returns (created)
      • HTTP 201 status code.
    • GET returns (not found)
      • HTTP 404 status code.
    • GET returns (bad request)
      • HTTP 400 status code.
    • GET/POST returns (internal server error)
      • HTTP 500 status code.


The Challenge:

A limited Procedure language adapted to modular-structure
like modern frameworks: Nest, Express.

  • Idea: Use include headers to import functions as componentized-style and build a makefile to construct the procedure line logic to compile the project.

Project Arch

webserver-in-C/
├── src/
│   ├── root/                    # Root module 
│   │   ├── root.controller.*    # Route routes / handlers
│   │   ├── root.module.*        # Module initialization
│   │   └── root.service.*       # Service logic
│   │
│   ├── system/                  # System specs module 
│   │   ├── system.controller.*  # System routes / handlers
│   │   ├── system.module.*      # Module initialization
│   │   └── system.service.*     # System info logic
│   │
│   ├── shared/                  # Shared utilities and components
│   │   ├── formats/             # Data formatting (JSON, etc.)
│   │   ├── http/                # HTTP protocol handling
│   │   │   ├── network/         # Network utilities
│   │   │   ├── response/        # HTTP response handling
│   │   │   ├── router/          # Routing system and decorators
│   │   │   └── server/          # Core HTTP server implementation
│   │   └── platform/            # Platform-specific implementations
│   │
│   └── main.c                   # Initialize the webserver
│
├── client/                      # Simple Frontend to consume the API
│   ├── css/
│   ├── js/
│   └── index.html
│
├── obj/                         # Compiled objects
└── Makefile                     # Build configuration

Routes:
-> GET / - Welcome (Root Path with Hateoas)
-> GET /api - Example API response
-> GET /api/machine - Machine status
-> POST /api/machine - Add machine name
-> GET /api/os - OS info
-> GET /api/system - System Specs

Plataforms

Quick Start

Windows

Environment:

  1. Install MSYS2 (recommended):
    • Download from: MSYS2 Installer
    • Follow installation instructions
    • In MSYS2 terminal, run:
      pacman -Syu
      pacman -S --needed base-devel mingw-w64-x86_64-toolchain
    • Add to PATH: C:\msys64\mingw64\bin

Build and Run:

# Build the project
make

# Build and run the server
make run

# Clean build artifacts
make clean

Tips: Run in Background (PowerShell):

# Start server in background
Start-Job -ScriptBlock { .\webserver.exe }

# Check if the job is running
Get-Job

# Stop server
Stop-Job -Id <JOB_ID>
Remove-Job -Id <JOB_ID>

Linux (Debian-based)

Environment:

  1. Install build tools:
    sudo apt update
    sudo apt install build-essential

Build and Run:

# Build the project
make

# Build and run the server
make run

# Clean build artifacts
make clean

Tips: Run in Background:

# Start server in background
./webserver &

# Check running processes
ps aux | grep webserver

# To stop the server
kill $(pgrep webserver)

macOS

Environment:

  1. Install Homebrew: brew install
  2. Install build tools:
    brew install gcc make

Build and Run:

# Build the project
make

# Build and run the server
make run

# Clean build artifacts
make clean

Hey, if you find this project useful, please consider giving it a ⭐ on GitHub!

Contributing

Contributions are welcome! Please see CONTRIBUTING for guidelines.

Feel free to fork the repository and submit pull requests with your improvements.

License

GNU GPLv3

This project is licensed under the GNU GPLv3 © 2025



🤘Hɐıan Sʇanɯau