diff --git a/factory/cli.py b/factory/cli.py index 59d1509..3face0d 100644 --- a/factory/cli.py +++ b/factory/cli.py @@ -7,6 +7,7 @@ status [run_id] — show run(s) status eval — re-run eval commands for an existing run attach — SSH into the remote tmux session for a run + hello [--port PORT] — start a hello HTTP endpoint server """ from __future__ import annotations @@ -1035,6 +1036,23 @@ def poll( console.print() +# ── hello ───────────────────────────────────────────────────────────────────── + +@app.command() +def hello( + port: int = typer.Option(8000, "--port", "-p", help="Port to listen on"), + host: str = typer.Option("0.0.0.0", "--host", "-h", help="Host to bind to"), +) -> None: + """Start a simple HTTP server with a /hello endpoint that returns JSON.""" + from factory.hello_server import run_server + + console.print(f"[green]✓[/green] Starting hello server on http://{host}:{port}/hello") + try: + run_server(host=host, port=port) + except KeyboardInterrupt: + console.print("\n[yellow]Shutting down...[/yellow]") + + # ── slack-listen ───────────────────────────────────────────────────────────── @app.command(name="slack-listen") diff --git a/factory/hello_server.py b/factory/hello_server.py new file mode 100644 index 0000000..9541c1c --- /dev/null +++ b/factory/hello_server.py @@ -0,0 +1,39 @@ +""" +Simple HTTP server with a hello endpoint. + +Usage: + from factory.hello_server import run_server + run_server(port=8000) +""" +from http.server import HTTPServer, BaseHTTPRequestHandler +import json + + +class HelloHandler(BaseHTTPRequestHandler): + """HTTP request handler for the hello endpoint.""" + + def do_GET(self): + """Handle GET requests.""" + if self.path == "/hello": + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.end_headers() + response = json.dumps({"message": "hello"}) + self.wfile.write(response.encode()) + else: + self.send_response(404) + self.send_header("Content-Type", "application/json") + self.end_headers() + response = json.dumps({"error": "not found"}) + self.wfile.write(response.encode()) + + def log_message(self, format, *args): + """Suppress default logging.""" + pass + + +def run_server(host: str = "0.0.0.0", port: int = 8000) -> None: + """Start the HTTP server on the specified host and port.""" + server = HTTPServer((host, port), HelloHandler) + print(f"Hello server listening on http://{host}:{port}/hello") + server.serve_forever()