Skip to content

Commit b70721b

Browse files
committed
add cdn_cache
1 parent d5f7ea8 commit b70721b

13 files changed

Lines changed: 81347 additions & 12 deletions

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typer = "^0.9.0"
3434
rich = "^13.7.0"
3535
aiofiles = "^24.1.0"
3636
pydantic-settings = "^2.8.0"
37+
aiohttp = "^3.9.0"
3738

3839
[tool.poetry.group.dev.dependencies]
3940
pytest = "^7.4.3"

servefs/cli.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import os
32
from pathlib import Path
43
from typing import Optional
@@ -8,6 +7,8 @@
87
from rich.console import Console
98
from rich.panel import Panel
109

10+
from servefs.utils.network import get_local_addresses
11+
1112
app = typer.Typer(
1213
name="servefs",
1314
help="A modern HTTP file server with web UI",
@@ -89,9 +90,19 @@ def main(
8990
os.environ["SERVEFS_BASIC_AUTH"] = basic_auth
9091

9192
# Display server information
93+
if host == "0.0.0.0":
94+
# Show all available addresses
95+
addresses = get_local_addresses()
96+
address_lines = []
97+
for addr in addresses:
98+
address_lines.append(f"[bold]http://{addr}:{port}[/bold]")
99+
server_info = "\n".join(address_lines)
100+
else:
101+
server_info = f"[bold]http://{host}:{port}[/bold]"
102+
92103
console.print(Panel.fit(
93104
f"[bold green]Starting server at[/bold green]\n"
94-
f"[bold]http://{host}:{port}[/bold]\n"
105+
f"{server_info}\n"
95106
f"[bold blue]Root directory:[/bold blue] {os.environ['SERVEFS_ROOT']}\n"
96107
f"[bold yellow]Developer mode:[/bold yellow] {'enabled' if debug else 'disabled'}\n"
97108
"\n[dim]Press Ctrl+C to quit[/dim]",

servefs/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from pathlib import Path
2-
2+
from typing import Union, Optional
33
from pydantic_settings import BaseSettings
44

55

66
class Settings(BaseSettings):
77
DEBUG: bool = False
8-
BASIC_AUTH: str | None = None
8+
BASIC_AUTH: Optional[str] = None
99
ROOT: Path = Path("./files")
1010

1111
class Config:

servefs/routes/page.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99

1010
import aiofiles
1111
from fastapi import APIRouter, HTTPException, Request
12-
from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse
12+
from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse, RedirectResponse
1313

1414
from ..utils.static import ETaggedStaticFiles
15+
from ..utils.cdn_cache import CDNCacheManager
1516

1617
logger = logging.getLogger(__name__)
1718

1819
# Get current module path
1920
PACKAGE_DIR = Path(__file__).parent.parent
2021

22+
# Initialize CDN cache manager
23+
cdn_cache_manager = CDNCacheManager(PACKAGE_DIR / "static" / "cdn_cache")
24+
2125
router = APIRouter(tags=["page"])
2226

2327
async def stream_file_range(file_path: Path, start: int, end: int) -> AsyncIterator[bytes]:
@@ -122,6 +126,17 @@ def init_static_files(app):
122126
"""Initialize static file serving"""
123127
app.mount("/static", ETaggedStaticFiles(directory=PACKAGE_DIR / "static"), name="static")
124128

129+
@router.get("/cdn/{url:path}")
130+
async def proxy_cdn(url: str, request: Request):
131+
"""Proxy requests to CDN with local caching"""
132+
full_url = f'https://{url}'
133+
response = await cdn_cache_manager.serve_cached_resource(full_url, request)
134+
135+
if response:
136+
return response
137+
# If caching failed, redirect to original URL
138+
return RedirectResponse(url=full_url, status_code=302)
139+
125140
# Serve index.html for the root path
126141
@router.get("/", response_class=HTMLResponse)
127142
async def serve_root():

servefs/static/cdn_cache/14b2812c4b916ea2a2a456f902a53453.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)