# REST API Demo A simple task management REST API demonstrating Lux's HTTP server effect and effect tracking. ## Features Demonstrated - **HttpServer Effect**: Built-in HTTP server with effect tracking - **Pattern Matching**: Route handling via pattern matching - **JSON**: Serialization and parsing - **ADTs**: `ApiResponse` type with Success/Error variants - **Effect Signatures**: All side effects explicitly declared ## Running ```bash cargo run -- projects/rest-api/main.lux ``` ## API Endpoints | Method | Endpoint | Description | |--------|---------------|-------------------| | GET | / | API info | | GET | /tasks | List all tasks | | GET | /tasks/:id | Get task by ID | | POST | /tasks | Create new task | | PUT | /tasks/:id | Update task | | DELETE | /tasks/:id | Delete task | ## Example Usage ```bash # Get API info curl http://localhost:8080/ # List tasks curl http://localhost:8080/tasks # Get specific task curl http://localhost:8080/tasks/1 # Create task curl -X POST -d '{"title":"New task","done":false}' http://localhost:8080/tasks # Update task curl -X PUT -d '{"done":true}' http://localhost:8080/tasks/1 # Delete task curl -X DELETE http://localhost:8080/tasks/1 ``` ## Code Structure ``` main.lux ├── Data Types (Task, ApiResponse) ├── JSON Serialization (taskToJson, tasksToJson) ├── Route Handlers (handleGetTasks, handleCreateTask, etc.) ├── Router (route function with pattern matching) ├── Request Handler (handleRequest) ├── Server Loop (serveRequests - recursive) └── Main Entry Point ``` ## Effect Tracking All functions declare their effects explicitly: ```lux fn handleRequest(req: Request): Unit with {Console, HttpServer} = ... fn serveRequests(count: Int, max: Int): Unit with {Console, HttpServer} = ... fn main(): Unit with {Console, HttpServer} = ... ``` This ensures: - Side effects are visible in function signatures - Testing is easier (swap effects for mocks) - Compiler verifies effect usage ## Notes - Server handles 10 requests then stops (for demo purposes) - Uses in-memory "database" (hardcoded tasks) - Simplified JSON parsing for demonstration