Files
lux/projects/rest-api/README.md
Brandon Lucas 705bd57e81 docs: add demos and update documentation for new features
Documentation:
- Update IMPLEMENTATION_PLAN.md with current status (222 tests)
- Update feature comparison table (Schema Evolution, Behavioral Types: )
- Add HttpServer to built-in effects list
- Update OVERVIEW.md with working behavioral types examples

Demo Programs:
- examples/schema_evolution.lux - Version annotations, constraints, runtime ops
- examples/behavioral_types.lux - pure, deterministic, commutative, idempotent, total

Sample Project:
- projects/rest-api/ - Full REST API demo with:
  - Task CRUD endpoints
  - Pattern matching router
  - JSON serialization
  - Effect-tracked request handling

Tests:
- Add behavioral type tests (pure, deterministic, commutative, idempotent, total)
- 227 tests passing

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-13 22:14:55 -05:00

85 lines
2.2 KiB
Markdown

# 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