Server API Reference¶
The server module provides functionality for running Spartan protocol servers.
Quick example¶
import asyncio
from teyaotlani import ServerConfig, run_server
async def main():
config = ServerConfig(
host="localhost",
port=3000,
document_root="./capsule",
)
await run_server(config)
asyncio.run(main())
ServerConfig¶
teyaotlani.ServerConfig
dataclass
¶
ServerConfig(
host="localhost",
port=DEFAULT_PORT,
document_root=".",
max_file_size=DEFAULT_MAX_FILE_SIZE,
enable_directory_listing=False,
index_files=(lambda: ["index.gmi", "index.gemini"])(),
enable_rate_limiting=True,
rate_limit_capacity=10,
rate_limit_refill_rate=1.0,
rate_limit_retry_after=30,
enable_access_control=False,
access_control_allow_list=None,
access_control_deny_list=None,
access_control_default_allow=True,
enable_upload=False,
upload_dir=None,
max_upload_size=10 * 1024 * 1024,
enable_delete=False,
hash_client_ips=True,
log_level="INFO",
json_logs=False,
)
Configuration for Spartan server.
Attributes:
| Name | Type | Description |
|---|---|---|
host |
str
|
Server host address. |
port |
int
|
Server port (default: 300). |
document_root |
Path | str
|
Path to directory containing files to serve. |
Examples:
>>> config = ServerConfig(
... host="localhost",
... port=300,
... document_root=Path("./capsule"),
... )
Functions¶
__post_init__ ¶
Validate and normalize configuration.
Source code in src/teyaotlani/server/config.py
validate ¶
Validate configuration at runtime.
Raises:
| Type | Description |
|---|---|
ValueError
|
If configuration is invalid. |
Source code in src/teyaotlani/server/config.py
get_rate_limit_config ¶
Get rate limit configuration if enabled.
Returns:
| Type | Description |
|---|---|
RateLimitConfig | None
|
RateLimitConfig if enabled, None otherwise. |
Source code in src/teyaotlani/server/config.py
get_access_control_config ¶
Get access control configuration if enabled.
Returns:
| Type | Description |
|---|---|
AccessControlConfig | None
|
AccessControlConfig if enabled, None otherwise. |
Source code in src/teyaotlani/server/config.py
from_toml
classmethod
¶
Load configuration from a TOML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the TOML configuration file. |
required |
Returns:
| Type | Description |
|---|---|
ServerConfig
|
A ServerConfig instance. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the config file doesn't exist. |
ValueError
|
If the config file is invalid. |
Source code in src/teyaotlani/server/config.py
Server Functions¶
run_server¶
teyaotlani.run_server ¶
Convenience function to run a server synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document_root
|
Path | str
|
Path to serve files from. |
required |
host
|
str
|
Server host address. |
'localhost'
|
port
|
int
|
Server port. |
DEFAULT_PORT
|
**kwargs
|
object
|
Additional arguments passed to start_server. |
{}
|
Source code in src/teyaotlani/server/server.py
start_server¶
teyaotlani.start_server
async
¶
start_server(
config=None,
host=None,
port=None,
document_root=None,
enable_directory_listing=False,
log_level="INFO",
json_logs=False,
hash_ips=True,
)
Start a Spartan protocol server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ServerConfig | None
|
Server configuration. If None, uses other arguments. |
None
|
host
|
str | None
|
Server host address (default: localhost). |
None
|
port
|
int | None
|
Server port (default: 300). |
None
|
document_root
|
Path | str | None
|
Path to serve files from. |
None
|
enable_directory_listing
|
bool
|
Whether to enable directory listings. |
False
|
log_level
|
str
|
Logging level. |
'INFO'
|
json_logs
|
bool
|
Whether to output logs as JSON. |
False
|
hash_ips
|
bool
|
Whether to hash client IPs in logs. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If configuration is invalid. |
Source code in src/teyaotlani/server/server.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |