Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions factory/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
status [run_id] — show run(s) status
eval <run_id> — re-run eval commands for an existing run
attach <run_id> — SSH into the remote tmux session for a run
hello [--port PORT] — start a hello HTTP endpoint server
"""
from __future__ import annotations

Expand Down Expand Up @@ -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")
Expand Down
39 changes: 39 additions & 0 deletions factory/hello_server.py
Original file line number Diff line number Diff line change
@@ -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()