static_files

litestar.static_files.create_static_files_router(path: str, directories: Sequence[PathType], file_system: AnyFileSystem | str | None = None, send_as_attachment: bool = False, html_mode: bool = False, name: str = 'static', after_request: AfterRequestHookHandler | None = None, after_response: AfterResponseHookHandler | None = None, before_request: BeforeRequestHookHandler | None = None, cache_control: CacheControlHeader | None = None, exception_handlers: ExceptionHandlersMap | None = None, guards: Sequence[Guard] | None = None, include_in_schema: bool | EmptyType = _EmptyEnum.EMPTY, middleware: Sequence[Middleware] | None = None, opt: Mapping[str, Any] | None = None, security: Sequence[SecurityRequirement] | None = None, tags: Sequence[str] | None = None, router_class: type[Router] = <class 'litestar.router.Router'>, allow_symlinks_outside_directory: bool = False) Router

Create a router with handlers to serve static files.

Parameters:
  • path – Path to serve static files under

  • directories – Directories to serve static files from

  • file_system – The file system to load the file from. Instances of BaseFileSystem, fsspec.spec.AbstractFileSystem, fsspec.asyn.AsyncFileSystem will be used directly. If passed string, use it to look up the corresponding file system from the FileSystemRegistry. If not given, the file will be loaded from default

  • send_as_attachment – Whether to send the file as an attachment

  • html_mode – When in HTML: - Serve an index.html file from / - Serve 404.html when a file could not be found

  • name – Name to pass to the generated handlers

  • after_requestafter_request handlers passed to the router

  • after_responseafter_response handlers passed to the router

  • before_requestbefore_request handlers passed to the router

  • cache_controlcache_control passed to the router

  • exception_handlers – Exception handlers passed to the router

  • guards – Guards passed to the router

  • include_in_schema – Include the routes / router in the OpenAPI schema

  • middleware – Middlewares passed to the router

  • opt – Opts passed to the router

  • security – Security options passed to the router

  • tagstags passed to the router

  • router_class – The class used to construct a router from

  • allow_symlinks_outside_directory

    Allow serving files that link a path inside a base directory (as specified in ‘directories’) to a path outside it. This should be handled with caution, as it allows potentially unintended access to files outside the defined ‘directories’ via symlink chains.