|
3 | 3 | import random |
4 | 4 | import os |
5 | 5 | import logging |
| 6 | +import aiohttp_cors |
6 | 7 |
|
7 | | -DEFAULT_ERROR_SUCCESS_COUNT = 90 |
8 | | -DEFAULT_ERROR_FAILURE_COUNT = 10 |
| 8 | +DEFAULT_OK_RATIO = 0.6 |
9 | 9 | DEFAULT_PORT = 8080 |
10 | 10 |
|
11 | | -logging.basicConfig(level=logging.INFO) |
12 | | - |
13 | 11 | _message_factory = None |
14 | 12 |
|
| 13 | +logging.basicConfig(level=logging.INFO) |
| 14 | + |
15 | 15 |
|
16 | 16 | class Message: |
17 | | - def __init__(self, message: str, success: bool = True): |
| 17 | + def __init__(self, message: str, ok: bool): |
18 | 18 | self._message = message |
19 | | - self._success = success |
| 19 | + self._ok = ok |
20 | 20 |
|
21 | 21 | @property |
22 | | - def success(self) -> bool: |
23 | | - return self._success |
| 22 | + def ok(self) -> bool: |
| 23 | + return self._ok |
24 | 24 |
|
25 | 25 | def to_json(self) -> str: |
26 | 26 | return json.dumps({"message": self._message}) |
27 | 27 |
|
28 | 28 |
|
29 | 29 | class MessageFactory: |
30 | | - def __init__(self, success_count: int, error_count: int): |
31 | | - self._success_rate: float = success_count / (success_count + error_count) |
| 30 | + def __init__(self, ok_ratio: float): |
| 31 | + self._ok_ratio: float = ok_ratio |
32 | 32 |
|
33 | 33 | def create_message(self) -> Message: |
34 | | - if random.random() < self._success_rate: |
35 | | - return Message("ok") |
| 34 | + if random.random() < self._ok_ratio: |
| 35 | + return Message("good", ok=True) |
36 | 36 | else: |
37 | | - return Message("bad", success=False) |
| 37 | + return Message("bad", ok=False) |
38 | 38 |
|
39 | 39 |
|
40 | 40 | async def index_handler(request: web.Request) -> web.Response: |
41 | 41 | message = _message_factory.create_message() |
42 | | - if message.success: |
43 | | - return web.Response(text=message.to_json(), content_type="application/json") |
44 | | - else: |
45 | | - return web.Response(text=message.to_json(), content_type="application/json", status=500) |
| 42 | + args = {"text": message.to_json()} |
| 43 | + if not message.ok: |
| 44 | + args["status"] = 500 |
| 45 | + return web.json_response(**args) |
46 | 46 |
|
47 | 47 |
|
48 | 48 | def main(): |
49 | 49 | global _message_factory |
50 | 50 |
|
51 | | - ERROR_SUCCESS_COUNT = int(os.getenv("ERROR_SUCCESS_COUNT", DEFAULT_ERROR_SUCCESS_COUNT)) |
52 | | - ERROR_FAILURE_COUNT = int(os.getenv("ERROR_FAILURE_COUNT", DEFAULT_ERROR_FAILURE_COUNT)) |
53 | | - _message_factory = MessageFactory(ERROR_SUCCESS_COUNT, ERROR_FAILURE_COUNT) |
| 51 | + ok_ratio = float(os.getenv("OK_RATIO", DEFAULT_OK_RATIO)) |
| 52 | + if not (0 <= ok_ratio <= 1): |
| 53 | + raise ValueError("OK_RATIO should be between 0 and 1: {ok_ratio=}") |
| 54 | + logging.info(f"{ok_ratio=}") |
| 55 | + _message_factory = MessageFactory(ok_ratio) |
54 | 56 |
|
55 | 57 | port = int(os.getenv("PORT", DEFAULT_PORT)) |
| 58 | + logging.info(f"{port=}") |
56 | 59 |
|
57 | 60 | app = web.Application() |
58 | | - app.router.add_get("/", index_handler) |
| 61 | + cors = aiohttp_cors.setup(app) |
| 62 | + resource = cors.add(app.router.add_resource("/")) |
| 63 | + cors.add( |
| 64 | + resource.add_route("GET", index_handler), |
| 65 | + {"*": aiohttp_cors.ResourceOptions(allow_credentials=False)}, |
| 66 | + ) |
| 67 | + |
59 | 68 | web.run_app(app, host="0.0.0.0", port=port) |
60 | 69 |
|
61 | 70 |
|
|
0 commit comments