2.2 KiB
🚀 Tiny Lua HTTP Server
A lightweight, zero-framework HTTP server built using Lua and LuaSocket. This project demonstrates how to handle basic routing, query parameter parsing, and manual HTTP response construction with a minimal footprint.
✨ Features
- Zero Dependencies: Only requires
luasocket. - Minimal Footprint: The entire server logic is under 50 lines of code.
- Query Parsing: Dynamically greets users via URL parameters.
- Basic Routing: Returns a
404 Not Foundfor any path other than/hello.
🛠️ Getting Started
1. Prerequisites (macOS)
Ensure you have Homebrew installed, then set up Lua and LuaRocks:
# Install Lua and the Package Manager
brew install lua luarocks
# Add LuaRocks to your shell path
eval $(luarocks path --bin)
2. Install Dependencies
Install the networking library used by the server:
luarocks install luasocket
3. Run the Server
Clone this repository (or save the script as server.lua) and execute:
lua server.lua
The server will start at http://localhost:8080.
📡 API Reference
The server currently supports a single endpoint:
/hello
HTTP GET
?name=YourName (optional)
Returns a greeting. Defaults to "User".
Examples
Greet a specific user:
curl "http://localhost:8080/hello?name=Raycast"
Output: Hello Raycast!
Default greeting:
curl "http://localhost:8080/hello"
Output: Hello User!
📂 Project Structure
- server.lua Main server logic including socket handling and routing.
- README.md Documentation and setup instructions.
🧠 How it Works
1. Socket Binding: The server binds to localhost:8080 and waits for incoming TCP connections.
2. Request Parsing: It reads the first line of the HTTP request and uses Lua's powerful Pattern Matching to extract the URI.
3. Routing: It checks if the path matches /hello.
4. Response: It manually constructs a valid HTTP/1.1 response string, including headers like Content-Type and Content-Length.
📄 License
This project is open-source and available under the MIT License.