-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_rest_api.ruff
More file actions
50 lines (41 loc) · 1.22 KB
/
http_rest_api.ruff
File metadata and controls
50 lines (41 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# REST API Example - Todo List
# Demonstrates a complete REST API with JSON responses
# In-memory todo list
let todos := []
let next_id := 1
# Create HTTP server
server := http_server(3000)
# GET /todos - List all todos
server := server.route("GET", "/todos", func(req) {
return json_response(200, todos)
})
# POST /todos - Create a new todo
server := server.route("POST", "/todos", func(req) {
# Parse JSON body
let data := parse_json(req.body)
# Create new todo
let todo := {
"id": next_id,
"title": data["title"],
"completed": false
}
# Add to list
push(todos, todo)
next_id := next_id + 1
return json_response(201, todo)
})
# GET /todos/:id - Get a specific todo (simplified - matches any path)
server := server.route("GET", "/todo", func(req) {
if len(todos) > 0 {
return json_response(200, todos[0])
}
return json_response(404, {"error": "Not found"})
})
# Start server
print("Todo API server running on http://localhost:3000")
print("Endpoints:")
print(" GET /todos - List all todos")
print(" POST /todos - Create a todo (send JSON: {\"title\": \"...\"})")
print(" GET /todo - Get first todo")
print("")
server.listen()