How kartofi/choki works
Project Type & Purpose
• This is a Rust-based HTTP server library inspired by Express.js.
• Its purpose is to allow users to easily define RESTful endpoints (GET, POST, PUT, DELETE), static file handling, and response management (including chunked responses, file downloads, headers, cookies, etc.) using a minimal set of external dependencies (threadpool, num_cpus, flate2, bumpalo).
• It’s meant to be lightweight and give direct control over request parsing and response generation.
Analyzing the File Structure
• Top-Level Files and Directories:
• Cargo.toml, Cargo.lock, and Dockerfile indicate a package that is built and can be containerized.
• README.md gives usage examples and describes features.
• Source Code:
• The “src” folder contains both a library file (lib.rs) and an example “main.rs” demonstrating usage.
• Inside “src/src” you have modules like mod.rs (for module scanning), request.rs and response.rs which define the HTTP request/response structures and logic.
• The “src/src/structs.rs” contains core data types.
• The “src/src/utils” folder (with logger.rs and utils.rs) contains supporting utilities like logging and additional helper functions.
• Testing:
• The “tests/static” directory provides sample static files (like an HTML page, a test file, and a video) to validate static endpoint functionality.
Main Components
a. Client Interaction:
• Clients (browsers, API consumers) send HTTP requests.
b. HTTP Server:
• A listener that accepts incoming HTTP requests.
• It uses a threadpool (from the threadpool and num_cpus crates) to handle concurrent requests.
c. Router / Endpoint Manager:
• Routes requests to appropriate handlers (GET, POST, PUT, DELETE, or STATIC endpoints).
• Supports endpoints with parameters and query parsing.
d. Request Parser & Data Structures:
• The Request module (src/src/request.rs) parses incoming HTTP data into a Request object which encapsulates queries, URL parameters, headers, cookies, etc.
e. Response Builder:
• The Response module (src/src/response.rs) provides various methods (send_string, send_json, etc.) for building HTTP responses.
f. Static File Handler:
• Logic for serving static files (using a directory like tests/static as a source).
g. Utilities & Logging:
• Utility modules (src/src/utils/) including logger support, to assist with debugging and diagnostics.
h. External Components/Dependencies:
• Thread pool management, compression (using flate2), bump allocation for efficient memory use, etc.
Relationships & Data Flow
• Upon receiving a request, the server hands it off to the “Threadpool Manager” which picks a worker thread.
• Each worker thread uses the “Request Parser” to convert raw data into a Request object and interacts with the “Endpoint Router” to find the proper endpoint handler.
• The corresponding Handler (based on the defined GET, POST, etc. endpoints) processes the request.
• The handler then uses the “Response Builder” to prepare the output (sending strings, JSON, files, or streams).
• If the endpoint is static, the “Static File Handler” takes over to locate and serve files from the file system.
• Throughout this flow, utility modules (including Logger) record key events and errors.
• Finally, the response is sent back to the client.
Architectural Patterns & Technologies
• Concurrency using thread pools for managing multiple simultaneous requests.
• Separation of concerns: Dedicated modules for HTTP processing (request/response), routing, and utility operations.
• Inspired by Express.js, the API design is modular and declarative for defining endpoints.
• Leverage Rust’s efficient memory management and external crates (bumpalo, flate2, etc.) for performance and low-level control.
Final Steps
• Every module (especially those with a direct impact on request/response processing) is included.