text: # AUTOGENERATED! DO NOT EDIT! File to edit: 11_fastapi_main.ipynb (unless otherwise specified).
  
  __all__ = ['MeasuringFileResponse', 'read_root', 'get_file_urls', 'serve_file', 'app']
  
  # Cell
  import time
  
  from pathlib import Path
  
  from fastapi import FastAPI
  from fastapi.responses import FileResponse
  
  # Cell
  
  import stat
  import aiofiles
  
  from aiofiles.os import stat as aio_stat
  
  from starlette.types import Receive, Scope, Send
  
  
  class MeasuringFileResponse(FileResponse):
      async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
          if self.stat_result is None:
              try:
                  stat_result = await aio_stat(self.path)
                  self.set_stat_headers(stat_result)
              except FileNotFoundError:
                  raise RuntimeError(f"File at path {self.path} does not exist.")
              else:
                  mode = stat_result.st_mode
                  if not stat.S_ISREG(mode):
                      raise RuntimeError(f"File at path {self.path} is not a file.")
          await send(
              {
                  "type": "http.response.start",
                  "status": self.status_code,
                  "headers": self.raw_headers,
              }
          )
          if self.send_header_only:
              await send({"type": "http.response.body", "body": b"", "more_body": False})
          else:
              async with aiofiles.open(self.path, mode="rb") as file:
                  start = time.perf_counter()
                  more_body = True
                  while more_body:
                      chunk = await file.read(self.chunk_size)
                      more_body = len(chunk) == self.chunk_size
                      await send(
                          {
                              "type": "http.response.body",
                              "body": chunk,
                              "more_body": more_body,
                          }
                      )
                  elapsed = time.perf_counter() - start
                  print(f"{self.path},{elapsed}")
  
          if self.background is not None:
              await self.background()
  
  # Cell
  
  app = FastAPI()
  
  
  @app.get("/")
  def read_root():
      return {"Hello": "World"}
  
  
  @app.get("/pathinfo/{base_dir}")
  def get_file_urls(base_dir: str):
      pass
  
  
  @app.get("/{base}/{path}/{num}")
  async def serve_file(base: str, path: str, num: int):
      file_path = str(Path(base) / path / str(num))
      return MeasuringFileResponse(file_path)