Skip to content

Karl-Michaud/CExpress

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

67 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ CExpress

Because C deserves frameworks as simple as Express.js

A lightweight HTTP server framework for C that brings the simplicity of Express.js to the power and performance of C.

CExpress is a minimal, high-performance HTTP server framework written in C that provides Express.js-like functionality for building web applications and APIs. Perfect for microservices, APIs, static file serving, and rapid prototyping without the overhead of larger frameworks.

πŸ“– Full documentation available at: CExpress Docs

πŸ“š Table of Contents

πŸ“– Overview

CExpress brings the familiar Express.js patterns to C development:

  • Rapid Prototyping: Build and test instantly
  • Simple routing: GET, POST, PUT, DELETE
  • Handler-based architecture: Modular, clean code
  • Static file serving: Out of the box
  • JSON API support: Built-in response formatting
  • Cross-platform: Linux and macOS

✨ Features

🎯 Core Features

  • Lightweight: Minimal memory footprint and dependencies
  • Fast: Built on efficient C socket programming
  • Simple: Express.js-inspired API design
  • Flexible: Support for various use cases and patterns

πŸ”§ Technical Features

  • HTTP/1.1 compliant request/response handling
  • Dynamic routing with method and path matching
  • Memory management with automatic cleanup
  • Error handling with proper HTTP status codes
  • Signal handling for graceful shutdown
  • Concurrent client support with select() multiplexing

🌐 Supported Use Cases

  • REST APIs and microservices
  • Static file hosting
  • Web application backends
  • Rapid development servers
  • Prototyping and testing
  • Embedded web interfaces

Installation

CExpress can be installed as a shared C library in two ways:

1. Install from GitHub (macOS & Linux)

Prerequisites

  • GCC or Clang compiler
  • Make utility

Install

git clone https://github.com/Karl-Michaud/CExpress.git
cd CExpress
make install

This will:

  • Copy the shared library (libCExpress.dylib on macOS, or libCExpress.so on Linux) to /usr/local/lib/
  • Copy the header files to /usr/local/include/CExpress/

Uninstall

To remove CExpress, run:

make uninstall

Notes

  • To install to a different location, set the PREFIX variable:
    make install PREFIX=/custom/path
  • For Linux, the shared library extension is .so; for macOS, it is .dylib.

2. Install via Homebrew (macOS)

Install

brew tap Karl-Michaud/cexpress
brew install cexpress

(Optional) Verify installation:

gcc -o test test.c -lCExpress
./test

Uninstall

brew uninstall cexpress

(Optional) Remove the tap and clear cached downloads:

brew untap Karl-Michaud/cexpress
brew cleanup

This will remove the CExpress library and headers installed by Homebrew.


πŸš€ Quick Start

#include <CExpress/server.h>
#include <stdio.h>
#include <stdlib.h>

char *hello_handler(void) {
    char *response = malloc(50);
    strcpy(response, "Hello from CExpress!");
    return response;
}

int main(void) {
    // Initialize server
    Server *server = server_init(8080, 10, 5, DEV);
    if (!server) {
        fprintf(stderr, "Failed to initialize server\n");
        return 1;
    }
    
    // Add route
    server_add_route(server, GET, "/hello", hello_handler);
    
    // Start server
    server_start(server);
    
    // Cleanup (automatic on shutdown)
    return 0;
}

Compile and Run

gcc -o server main.c -lCExpress
./server

Test with:

curl http://localhost:8080/hello

πŸ“ Examples

Found in examples/

  1. Hello World β†’ minimal GET route
  2. JSON API β†’ Multi-method API with JSON responses, error handling, and data management.
  3. Static Files β†’ serve HTML, CSS, JS
  4. REST API β†’ Full CRUD operations with proper REST patterns and status codes.

πŸ“– API Reference

Server Management

// Initialize server
Server *server_init(int port, int max_clients, int backlog, Mode mode);

// Start server (blocks until shutdown)
int server_start(Server *server);

// Clean up resources
void server_free(Server *server);

Routing

// Add route
int server_add_route(Server *server, method_t method, path_t path, HandlerFunc handler);

// Remove route
int server_remove_route(Server *server, method_t method, path_t path);

Handler Functions

// Handler function signature
typedef char *(*HandlerFunc)(void);

// Example handler
char *my_handler(void) {
    char *response = malloc(100);
    strcpy(response, "Hello World!");
    return response;
}

HTTP Methods

typedef enum {GET, POST, PUT, DELETE, FAIL} method_t;

Server Modes

typedef enum {DEV, PROD} Mode;
// DEV  - localhost only (127.0.0.1)
// PROD - all interfaces (0.0.0.0)

Architecture

CExpress follows a modular architecture:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Public API    β”‚  ← server.h
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Server Core   β”‚  ← server.c
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Routing       β”‚  ← routers.c/h
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Handlers      β”‚  ← handlers.c/h
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Utilities     β”‚  ← utils.c/h
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Request Flow

  1. Client Request β†’ Server Socket
  2. Accept Connection β†’ Client List
  3. Read Data β†’ Buffer
  4. Parse Header β†’ Extract Method/Path
  5. Find Route β†’ Router List
  6. Execute Handler β†’ Generate Response
  7. Send Response β†’ Client
  8. Cleanup β†’ Memory Management

⚑ Performance

CExpress is designed for performance:

  • Low Latency: Direct socket programming
  • Memory Efficient: Minimal allocations and automatic cleanup
  • Concurrent: Multiple client support
  • Lightweight: Small binary size and memory footprint
  • Rapid Prototyping: Instantly build and test web applications without the overhead of large frameworks

🀝 Contributing

I welcome contributions! Here's how to get started:

Development Setup

  1. Fork the repository & create a feature branch
  2. Make your changes & add tests if applicable
  3. Submit a PR πŸš€

Areas for Contribution

  • Additional HTTP methods
  • Middleware support
  • WebSocket support
  • Enhanced error handling
  • Performance optimizations
  • Additional examples

Made by Karl-Alexandre Michaud

Because C deserves frameworks as simple as Express.js