Files
Brandon Lucas cdc4f47272 fix: fix REST API and HTTP server examples
- Fix string interpolation issues: escape curly braces with \{ and \}
  since unescaped { triggers string interpolation
- Fix effectful function invocation: use "let _ = run main() with {}"
  instead of bare "main()" calls
- Use inline record types instead of type aliases (structural typing)
- Fix flake.nix to show build progress instead of suppressing output

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-13 23:11:51 -05:00
..

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

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

# 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:

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