o
odinpkg.dev
packages / library / httpo

httpo

638e609library

HTTP server library for Odin

No license · updated 3 months ago

HTTPO

Minimal and lightweight HTTP server library for the Odin programming language.

Inspired by Fiber and designed with simplicity and performance in mind, HTTPO provides a clean, minimal API for building HTTP services in Odin. It handles routing, request parsing, and response generation with zero external dependencies.

Features

  • Lightweight: Minimal overhead, focused on core HTTP functionality.
  • Simple API: Intuitive server setup and endpoint registration.
  • Efficient: Built on Odin's core net package with non-blocking I/O and worker threads.
  • Request Parsing: Automatic parsing of HTTP method, path, headers, and cookies.
  • Response Building: Easy creation and customization of HTTP responses.
  • Graceful Shutdown: Supports controlled server termination via atomic flags.
  • Tested: Includes a comprehensive test suite.

Quick Start

Getting started with HTTPO is easy. A complete server example with graceful shutdown handling is available in the example file.

Key Components

Server

The Server struct holds the server's state, including its port, allocator, and registered endpoints.

InitServer

InitServer :: proc(port: u16, allocator := context.allocator) -> Server

Creates a new Server instance on the specified port with an optional allocator.

Listen and ListenOnce

  • Listen(s: ^Server, stop: ^bool = nil) -> net.Network_Error: Starts the server to accept connections indefinitely or until the stop flag becomes true.
  • ListenOnce(s: ^Server) -> net.Network_Error: Accepts and handles a single connection.

Endpoint and AddEndpoint

AddEndpoint :: proc(server: ^Server, path: string, handler: RequestHandler)

Registers a handler function for a specific URL path.

Request and Response

  • Request: Contains parsed HTTP request data (method, path, headers, cookies, body).
  • Response: Used to construct the HTTP response (status code, headers, body).

Use CreateResponse(status_code: StatusCode) to initialize a Response.

Building and Testing

Building

Compile your application with Odin:

odin build .

Running Tests

HTTPO includes a test suite. Run it with:

odin test .