from __future__ import annotations
import os
import tempfile
from mq_bridge import Message, Route
LISTEN = os.environ.get("MQB_LISTEN", "0.0.0.0:3000")
USER_PREFIX = "/user/"
CONFIG = f"""
routes:
the-benchmarker:
concurrency: 1
batch_size: 512
input:
http:
url: "{LISTEN}"
concurrency_limit: 65536
internal_buffer_size: 16384
inline_response_fast_path: true
output:
response: {{}}
"""
TEXT_META = {"content-type": "text/plain"}
NOT_FOUND_META = {"content-type": "text/plain", "http_status_code": "404"}
def handle(message: Message) -> Message:
method = message.metadata.get("http_method", "")
path = message.metadata.get("http_path", "")
if method == "GET" and path == "/":
return Message(b"")
if method == "POST" and path == "/user":
return Message(b"")
if method == "GET" and path.startswith(USER_PREFIX):
user_id = path[len(USER_PREFIX):]
if user_id and "/" not in user_id:
return Message(user_id.encode(), TEXT_META)
return Message(b"Not Found", NOT_FOUND_META)
def main() -> None:
with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as handle_file:
handle_file.write(CONFIG)
config_path = handle_file.name
route = Route.from_yaml(config_path, "the-benchmarker").with_handler(handle)
route.run()
if __name__ == "__main__":
main()