Add core modules: CLI, recorder, server, snapshot manager
This commit is contained in:
49
api_snapshot/cli/serve.py
Normal file
49
api_snapshot/cli/serve.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
import click
|
||||
from rich.console import Console
|
||||
|
||||
from api_snapshot.server import create_app
|
||||
from api_snapshot.snapshot import load_snapshot
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.argument("name")
|
||||
@click.option("--host", "-H", default="127.0.0.1", help="Host to bind to")
|
||||
@click.option("--port", "-p", default=8080, type=int, help="Port to listen on")
|
||||
@click.option("--latency", "-l", default="original", type=click.Choice(["original", "fixed", "random", "none"]), help="Latency mode")
|
||||
@click.option("--latency-ms", default=None, type=int, help="Fixed latency in milliseconds")
|
||||
@click.option("--latency-min", default=None, type=int, help="Minimum latency for random mode")
|
||||
@click.option("--latency-max", default=None, type=int, help="Maximum latency for random mode")
|
||||
def serve(
|
||||
name: str,
|
||||
host: str,
|
||||
port: int,
|
||||
latency: str,
|
||||
latency_ms: int | None,
|
||||
latency_min: int | None,
|
||||
latency_max: int | None,
|
||||
) -> None:
|
||||
"""Start a mock server from a snapshot."""
|
||||
snapshot = load_snapshot(name)
|
||||
|
||||
app = create_app(snapshot)
|
||||
|
||||
latency_config = {"mode": latency}
|
||||
if latency_ms is not None:
|
||||
latency_config["fixed_ms"] = latency_ms
|
||||
if latency_min is not None:
|
||||
latency_config["min_ms"] = latency_min
|
||||
if latency_max is not None:
|
||||
latency_config["max_ms"] = latency_max
|
||||
|
||||
click.echo(f"Starting mock server on http://{host}:{port}")
|
||||
click.echo("Press Ctrl+C to stop")
|
||||
|
||||
try:
|
||||
app.run(host=host, port=port, debug=False)
|
||||
except KeyboardInterrupt:
|
||||
click.echo("\nServer stopped")
|
||||
Reference in New Issue
Block a user