from __future__ import annotations
import os
import tempfile
from mq_bridge import Message, Route
LISTEN = os.environ.get("MQB_LISTEN", "0.0.0.0:8080")
CONFIG = f"""
routes:
techempower:
concurrency: 1
batch_size: 512
input:
http:
url: "{LISTEN}"
method: "GET"
concurrency_limit: 65536
internal_buffer_size: 16384
inline_response_fast_path: true
output:
response: {{}}
"""
JSON_META = {"content-type": "application/json", "Server": "mq-bridge-py"}
TEXT_META = {"content-type": "text/plain", "Server": "mq-bridge-py"}
NOT_FOUND_META = {
"content-type": "text/plain",
"Server": "mq-bridge-py",
"http_status_code": "404",
}
def handle(message: Message) -> Message:
path = message.metadata.get("http_path", "")
if path == "/json":
return Message.from_json({"message": "Hello, World!"}, JSON_META)
if path == "/plaintext":
return Message(b"Hello, World!", 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, "techempower").with_handler(handle)
route.run()
if __name__ == "__main__":
main()