|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +from collections.abc import Awaitable, Callable, Coroutine |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from uvicorn import Config |
| 9 | +from uvicorn.server import Server |
| 10 | + |
| 11 | +Receive = Callable[[], Awaitable[dict[str, Any]]] |
| 12 | +Send = Callable[[dict[str, Any]], Coroutine[None, None, None]] |
| 13 | + |
| 14 | +_PRODUCTS = { |
| 15 | + '1': {'name': 'Widget A', 'price': '$19.99', 'description': 'A basic widget for everyday use'}, |
| 16 | + '2': {'name': 'Widget B', 'price': '$29.99', 'description': 'An advanced widget with extra features'}, |
| 17 | + '3': {'name': 'Widget C', 'price': '$39.99', 'description': 'A premium widget for professionals'}, |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +async def _send_html(send: Send, html: str, status: int = 200) -> None: |
| 22 | + await send( |
| 23 | + { |
| 24 | + 'type': 'http.response.start', |
| 25 | + 'status': status, |
| 26 | + 'headers': [[b'content-type', b'text/html; charset=utf-8']], |
| 27 | + } |
| 28 | + ) |
| 29 | + await send({'type': 'http.response.body', 'body': html.encode()}) |
| 30 | + |
| 31 | + |
| 32 | +async def app(scope: dict[str, Any], _receive: Receive, send: Send) -> None: |
| 33 | + assert scope['type'] == 'http' |
| 34 | + path = scope['path'] |
| 35 | + |
| 36 | + if path == '/': |
| 37 | + await _send_html( |
| 38 | + send, |
| 39 | + '<html><head><title>E-commerce Test Store</title></head><body>' |
| 40 | + '<h1>Welcome to Test Store</h1>' |
| 41 | + '<a href="/products/1">Widget A</a>' |
| 42 | + '<a href="/products/2">Widget B</a>' |
| 43 | + '<a href="/products/3">Widget C</a>' |
| 44 | + '<a href="/about">About Us</a>' |
| 45 | + '</body></html>', |
| 46 | + ) |
| 47 | + elif path.startswith('/products/'): |
| 48 | + product = _PRODUCTS.get(path.split('/')[-1]) |
| 49 | + if product: |
| 50 | + await _send_html( |
| 51 | + send, |
| 52 | + f'<html><head><title>{product["name"]}</title></head><body>' |
| 53 | + f'<h1>{product["name"]}</h1>' |
| 54 | + f'<span class="price">{product["price"]}</span>' |
| 55 | + f'<p class="description">{product["description"]}</p>' |
| 56 | + f'<a href="/">Back to Home</a>' |
| 57 | + f'</body></html>', |
| 58 | + ) |
| 59 | + else: |
| 60 | + await _send_html(send, '<html><body>Not Found</body></html>', 404) |
| 61 | + elif path == '/about': |
| 62 | + await _send_html( |
| 63 | + send, |
| 64 | + '<html><head><title>About Us</title></head><body>' |
| 65 | + '<h1>About Test Store</h1>' |
| 66 | + '<p class="description">We sell the best widgets in the world.</p>' |
| 67 | + '<a href="/">Back to Home</a>' |
| 68 | + '</body></html>', |
| 69 | + ) |
| 70 | + else: |
| 71 | + await _send_html(send, '<html><body>Not Found</body></html>', 404) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + asyncio.run( |
| 76 | + Server( |
| 77 | + config=Config( |
| 78 | + app=app, |
| 79 | + lifespan='off', |
| 80 | + loop='asyncio', |
| 81 | + port=8080, |
| 82 | + log_config=None, |
| 83 | + log_level=logging.CRITICAL, |
| 84 | + ) |
| 85 | + ).serve() |
| 86 | + ) |
0 commit comments