Skip to content

Quick Start ​

This guide will help you get started with UVHTTP in just a few minutes.

Prerequisites ​

  • Operating System: Linux
    • Future Plans: macOS, Windows, FreeBSD, WebAssembly (WASM), and other Unix-like systems
  • C compiler (GCC or Clang)
  • CMake 3.10 or higher
  • libuv (will be downloaded automatically)

Note: UVHTTP currently supports Linux platforms only. We plan to expand support to other operating systems in future releases.

Installation ​

Clone the Repository ​

bash
git clone --recurse-submodules https://github.com/adam-ikari/uvhttp.git
cd uvhttp

Note: The --recurse-submodules flag automatically clones all dependencies. If you forget this flag, you can run git submodule update --init --recursive afterward.

Build the Library ​

bash
make build

Run Tests ​

bash
make test

Your First Server ​

Create a file hello.c:

c
#include <uvhttp.h>
#include <string.h>

int main() {
    // Create event loop
    uv_loop_t* loop = uv_default_loop();
    
    // Create server
    uvhttp_server_t* server;
    uvhttp_error_t result = uvhttp_server_new(loop, &server);
    if (result != UVHTTP_OK) {
        fprintf(stderr, "Failed to create server: %s\n", uvhttp_error_string(result));
        return 1;
    }
    
    // Create router
    uvhttp_router_t* router = NULL;
    result = uvhttp_router_new(&router);
    if (result != UVHTTP_OK) {
        fprintf(stderr, "Failed to create router: %s\n", uvhttp_error_string(result));
        return 1;
    }
    result = uvhttp_server_set_router(server, router);
    if (result != UVHTTP_OK) {
        fprintf(stderr, "Failed to set router: %s\n", uvhttp_error_string(result));
        return 1;
    }

    // Add a route handler. Handlers take (request, response) and return int.
    int hello_handler(uvhttp_request_t* req, uvhttp_response_t* res) {
        // Set status code
        uvhttp_response_set_status(res, 200);

        // Set headers
        uvhttp_response_set_header(res, "Content-Type", "text/plain");

        // Set body
        uvhttp_response_set_body(res, "Hello, World!", strlen("Hello, World!"));

        // Send response
        return uvhttp_response_send(res);
    }

    result = uvhttp_router_add_route(router, "/hello", hello_handler);
    if (result != UVHTTP_OK) {
        fprintf(stderr, "Failed to add route: %s\n", uvhttp_error_string(result));
        return 1;
    }
    
    // Start server
    result = uvhttp_server_listen(server, "0.0.0.0", 8080);
    if (result != UVHTTP_OK) {
        fprintf(stderr, "Failed to start server: %s\n", uvhttp_error_string(result));
        return 1;
    }
    
    printf("Server running on http://0.0.0.0:8080\n");
    
    // Run event loop
    uv_run(loop, UV_RUN_DEFAULT);
    
    return 0;
}

Compile and Run ​

bash
gcc hello.c -Iinclude -Lbuild/dist/lib -luvhttp -lpthread -luv -o hello
./hello

Visit http://localhost:8080/hello in your browser!

Project Structure ​

uvhttp/
├── include/           # Public headers
├── src/               # Source implementation
├── docs/              # Documentation
├── examples/          # Example programs
├── test/              # Tests
└── build/             # Build output directory

Configuration Options ​

Build options can be configured by editing option() entries in CMakeLists.txt, then rebuilding:

bash
# Rebuild after editing CMakeLists.txt options
make build

Next Steps ​

Need Help? ​

Released under MIT License