Static Files¶
Static file serving for Gemini capsules.
Configuration¶
StaticFilesConfig¶
Configuration options for static file serving.
StaticFilesConfig
dataclass
¶
StaticFilesConfig(
index_files: list[str] = (
lambda: ["index.gmi", "index.gemini"]
)(),
directory_listing: bool = False,
max_file_size: int = 100 * 1024 * 1024,
mime_types: dict[str, str] = dict(),
follow_symlinks: bool = False,
)
Configuration for static file serving.
Attributes:
| Name | Type | Description |
|---|---|---|
index_files |
list[str]
|
Files to serve for directory requests. |
directory_listing |
bool
|
Enable directory listing when no index found. |
max_file_size |
int
|
Maximum file size to serve (bytes). |
mime_types |
dict[str, str]
|
Custom MIME type mappings by extension. |
follow_symlinks |
bool
|
Whether to follow symbolic links. |
Handler¶
StaticFiles¶
Serve static files from a directory.
StaticFiles
¶
StaticFiles(
directory: Path | str,
*,
config: StaticFilesConfig | None = None,
index_files: list[str] | None = None,
directory_listing: bool | None = None,
max_file_size: int | None = None,
mime_types: dict[str, str] | None = None,
follow_symlinks: bool | None = None,
)
Serve static files from a directory.
This handler serves files from a specified directory, with support for directory indexes, directory listings, custom MIME types, and security controls.
Example
from xitzin.staticfiles import StaticFiles
Basic usage¶
handler = StaticFiles("./public") app.mount("/files", handler)
With configuration¶
handler = StaticFiles( "./docs", directory_listing=True, max_file_size=50 * 1024 * 1024, )
@handler.not_found def custom_404(request, path_info): return "# File Not Found"
app.mount("/docs", handler)
Create a static file handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path | str
|
Directory to serve files from. |
required |
config
|
StaticFilesConfig | None
|
Configuration object (overridden by other params). |
None
|
index_files
|
list[str] | None
|
Files to serve for directory requests. |
None
|
directory_listing
|
bool | None
|
Enable directory listing when no index found. |
None
|
max_file_size
|
int | None
|
Maximum file size to serve (bytes). |
None
|
mime_types
|
dict[str, str] | None
|
Custom MIME type mappings by extension. |
None
|
follow_symlinks
|
bool | None
|
Whether to follow symbolic links. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If directory doesn't exist or isn't a directory. |
Source code in src/xitzin/staticfiles.py
__call__
async
¶
Handle a request for a static file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The Gemini request. |
required |
path_info
|
str
|
Path after the mount prefix (e.g., "/docs/page.gmi"). |
required |
Returns:
| Type | Description |
|---|---|
GeminiResponse
|
GeminiResponse with the file content or error. |
Raises:
| Type | Description |
|---|---|
NotFound
|
If file doesn't exist (and no custom handler). |
BadRequest
|
If path validation fails. |
Source code in src/xitzin/staticfiles.py
not_found
¶
Register a custom not-found handler.
The handler receives (request, path_info) and should return a response.
Example
@handler.not_found def custom_404(request, path_info): return f"# Not Found\n\nFile {path_info} doesn't exist."