-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (43 loc) · 1.15 KB
/
app.py
File metadata and controls
55 lines (43 loc) · 1.15 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
51
52
53
54
55
from flask import Flask, jsonify
app = Flask(__name__)
ITEMS = [
{"id": 1, "name": "Widget", "price": 9.99},
{"id": 2, "name": "Gadget", "price": 24.99},
{"id": 3, "name": "Doohickey", "price": 4.99},
]
@app.route("/health")
def health():
return jsonify({"status": "ok"})
@app.route("/api/items")
def get_items():
return jsonify(ITEMS)
@app.route("/api/items/<int:item_id>")
def get_item(item_id):
item = next((i for i in ITEMS if i["id"] == item_id), None)
if item is None:
return jsonify({"error": "not found"}), 404
return jsonify(item)
@app.route("/")
def index():
return """<!DOCTYPE html>
<html>
<head><title>Item Store</title></head>
<body>
<h1>Item Store</h1>
<ul id="items"></ul>
<script>
fetch('/api/items')
.then(r => r.json())
.then(items => {
const ul = document.getElementById('items');
items.forEach(item => {
const li = document.createElement('li');
li.textContent = `${item.name} - $${item.price.toFixed(2)}`;
ul.appendChild(li);
});
});
</script>
</body>
</html>"""
if __name__ == "__main__":
app.run(port=5111)