from __future__ import annotations

import asyncio

from hypercorn.asyncio import serve
from hypercorn.config import Config
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from starlette.routing import Route


async def ip(request: Request) -> PlainTextResponse:
    return PlainTextResponse(content=request.client.host)  # type:ignore[union-attr]


async def delay(request: Request) -> PlainTextResponse:
    seconds = request.path_params["seconds"]
    await asyncio.sleep(seconds)
    return PlainTextResponse(content="ok")


app = Starlette(
    debug=True,
    routes=[
        Route("/ip", ip),
        Route("/delay/{seconds:int}", delay),
    ],
)


def run_app(
    host: str,
    port: int,
    certfile: str | None = None,
    keyfile: str | None = None,
) -> None:
    config = Config()
    config.bind = [f"{host}:{port}"]
    config.certfile = certfile
    config.keyfile = keyfile
    asyncio.run(serve(app, config))  # type:ignore[arg-type]
