from asgiref.sync import iscoroutinefunction, markcoroutinefunction
from django.http import HttpResponse
from django.utils.decorators import async_only_middleware


@async_only_middleware
class HealthCheckMiddleware:
    def __init__(self, get_response) -> None:
        self.get_response = get_response
        if iscoroutinefunction(self.get_response):
            print("HealthcheckMiddleware is async.")
            markcoroutinefunction(self)  # type: ignore[arg-type]

    async def __call__(self, request):
        if request.path == "/healthcheck":
            return HttpResponse("ok")
        return await self.get_response(request)
