How cablehead/http-nu works
Project Type & Purpose
• http-nu is a Rust-based command-line web server that dynamically executes Nushell closures as HTTP handlers.
• Its purpose is to bridge an HTTP interface with Nushell closures, offering features such as TLS support, UNIX domain socket listening, serving static files, streaming responses, and even potential server-sent events.
• The README also highlights usage examples and commands showing how closures (either passed inline or via files/STDIN) are integrated into the HTTP handling process.
Key Components Derived from File Tree & README
• Core Engine (src/engine.rs): Likely manages the execution of Nushell closures. This component is central to how HTTP requests are turned into Nushell command executions.
• Request Handler (src/handler.rs): Responsible for mapping incoming HTTP requests to the closure logic. It may process request metadata and handle response metadata configuration (including static content and streaming).
• Listener (src/listener.rs): Manages different types of network listeners, whether it’s on a TCP port or via a UNIX domain socket, and routes incoming connections to the handler/engine.
• Main Entry (src/main.rs): Orchestrates setting up the server, parsing command-line arguments (like specifying the closure, TLS files, etc.), and initializing the engine and listener.
• Library abstraction (src/lib.rs): Likely provides shared functionality used across the engine, handler, and listener.
• Tests (src/test_engine.rs, src/test_handler.rs): Although not diagrammed in a production architecture, understanding that there are modular tests shows that the engine and handler are designed independently and are unit-testable.
Relationships & Interactions
• The Listener accepts incoming HTTP (or UNIX socket) connections.
• The HTTP Request is forwarded to the Handler, which extracts and prepares request metadata.
• The Engine takes the prepared request and runs the Nushell closure, effectively executing user-defined code to generate a response.
• Optionally, TLS handling is integrated by processing certificate files if TLS is enabled.
• The external interfaces (like curl or browsers) connect to this backend service, while GitHub Actions and Cargo packaging manage CI/CD and build/testing workflows.
Architectural Patterns and Design Principles
• Modular Design: Separation of concerns is evident (listener, handler, engine) allowing independent development/testing.
• Layered Architecture: A clear division between network I/O (listener), request transformation (handler), and business logic (engine executing closures).
• Extensibility: By receiving closures at runtime, the engine is highly flexible in handling various processing pipelines.