-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (52 loc) · 1.69 KB
/
Copy pathapp.py
File metadata and controls
72 lines (52 loc) · 1.69 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from aiohttp import web
import json
import random
import os
import logging
import aiohttp_cors
DEFAULT_OK_RATIO = 0.6
DEFAULT_PORT = 8080
_message_factory = None
logging.basicConfig(level=logging.INFO)
class Message:
def __init__(self, message: str, ok: bool):
self._message = message
self._ok = ok
@property
def ok(self) -> bool:
return self._ok
def to_json(self) -> str:
return json.dumps({"message": self._message})
class MessageFactory:
def __init__(self, ok_ratio: float):
self._ok_ratio: float = ok_ratio
def create_message(self) -> Message:
if random.random() < self._ok_ratio:
return Message("good", ok=True)
else:
return Message("bad", ok=False)
async def index_handler(request: web.Request) -> web.Response:
message = _message_factory.create_message()
args = {"text": message.to_json()}
if not message.ok:
args["status"] = 500
return web.json_response(**args)
def main():
global _message_factory
ok_ratio = float(os.getenv("OK_RATIO", DEFAULT_OK_RATIO))
if not (0 <= ok_ratio <= 1):
raise ValueError("OK_RATIO should be between 0 and 1: {ok_ratio=}")
logging.info(f"{ok_ratio=}")
_message_factory = MessageFactory(ok_ratio)
port = int(os.getenv("PORT", DEFAULT_PORT))
logging.info(f"{port=}")
app = web.Application()
cors = aiohttp_cors.setup(app)
resource = cors.add(app.router.add_resource("/"))
cors.add(
resource.add_route("GET", index_handler),
{"*": aiohttp_cors.ResourceOptions(allow_credentials=False)},
)
web.run_app(app, host="0.0.0.0", port=port)
if __name__ == "__main__":
main()