Initial upload: API Mock CLI v0.1.0
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
This commit is contained in:
104
src/cli/commands/start.py
Normal file
104
src/cli/commands/start.py
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
import os
|
||||||
|
import click
|
||||||
|
from src.cli.config import ConfigLoader
|
||||||
|
from src.core.server import MockServer
|
||||||
|
from src.models.config import ServerConfig
|
||||||
|
from src.models.endpoint import Endpoint
|
||||||
|
from src.utils.file import FileUtils
|
||||||
|
|
||||||
|
|
||||||
|
@click.command("start")
|
||||||
|
@click.option(
|
||||||
|
"--port",
|
||||||
|
type=int,
|
||||||
|
default=None,
|
||||||
|
help="Port to run the server on"
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--host",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="Host to bind to"
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--reload/--no-reload",
|
||||||
|
default=None,
|
||||||
|
help="Enable auto-reload"
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--offline/--online",
|
||||||
|
default=None,
|
||||||
|
help="Run in offline mode (no tunnel)"
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--tunnel/--no-tunnel",
|
||||||
|
default=None,
|
||||||
|
help="Enable/disable tunnel"
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--config",
|
||||||
|
type=click.Path(exists=True),
|
||||||
|
default=None,
|
||||||
|
help="Path to configuration file"
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--endpoints",
|
||||||
|
type=click.Path(exists=True),
|
||||||
|
default=None,
|
||||||
|
help="Path to endpoints file"
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--verbose",
|
||||||
|
"-v",
|
||||||
|
is_flag=True,
|
||||||
|
default=False,
|
||||||
|
help="Enable verbose output"
|
||||||
|
)
|
||||||
|
def start_command(
|
||||||
|
port: int,
|
||||||
|
host: str,
|
||||||
|
reload: bool,
|
||||||
|
offline: bool,
|
||||||
|
tunnel: bool,
|
||||||
|
config: str,
|
||||||
|
endpoints: str,
|
||||||
|
verbose: bool
|
||||||
|
):
|
||||||
|
config_path = ConfigLoader.find_config_file(config)
|
||||||
|
if config_path:
|
||||||
|
config_data = ConfigLoader.load(config_path)
|
||||||
|
else:
|
||||||
|
config_data = ConfigLoader.load()
|
||||||
|
server_config = config_data.server
|
||||||
|
if port:
|
||||||
|
server_config.port = port
|
||||||
|
if host:
|
||||||
|
server_config.host = host
|
||||||
|
if reload is not None:
|
||||||
|
server_config.reload = reload
|
||||||
|
if offline is not None:
|
||||||
|
server_config.offline = offline
|
||||||
|
if tunnel is not None:
|
||||||
|
server_config.tunnel = tunnel
|
||||||
|
endpoint_path = endpoints or server_config.endpoints_file
|
||||||
|
if not os.path.exists(endpoint_path):
|
||||||
|
click.echo(f"Error: Endpoints file not found: {endpoint_path}", err=True)
|
||||||
|
return
|
||||||
|
endpoints_data = FileUtils.load_yaml(endpoint_path)
|
||||||
|
endpoints_list = []
|
||||||
|
for ep_data in endpoints_data.get("endpoints", []):
|
||||||
|
try:
|
||||||
|
endpoint = Endpoint(**ep_data)
|
||||||
|
endpoints_list.append(endpoint)
|
||||||
|
except Exception as e:
|
||||||
|
click.echo(f"Warning: Invalid endpoint definition: {e}")
|
||||||
|
server = MockServer(server_config)
|
||||||
|
server.register_endpoints(endpoints_list)
|
||||||
|
if verbose:
|
||||||
|
click.echo(f"Registered {len(endpoints_list)} endpoints")
|
||||||
|
for ep in endpoints_list:
|
||||||
|
click.echo(f" - {ep.method} {ep.path}")
|
||||||
|
click.echo(f"Starting server on {server_config.host}:{server_config.port}")
|
||||||
|
if server_config.offline:
|
||||||
|
click.echo("Mode: Offline (no tunnel)")
|
||||||
|
server.run()
|
||||||
Reference in New Issue
Block a user