diff --git a/Dockerfile b/Dockerfile index 483e11e..374fbc7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,7 @@ RUN pip install --no-cache-dir -U pip setuptools wheel \ COPY telegram-scraper.py app_web.py ./ COPY templates ./templates/ +COPY static ./static/ EXPOSE 8000 diff --git a/app_web.py b/app_web.py index 403c266..f1c3d8c 100644 --- a/app_web.py +++ b/app_web.py @@ -17,11 +17,12 @@ from pathlib import Path from typing import Any, Dict, List, Optional, Tuple, Union from fastapi import FastAPI, Form, Query, Request -from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse +from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse, Response from fastapi.templating import Jinja2Templates from starlette.middleware.sessions import SessionMiddleware BASE_DIR = Path(__file__).resolve().parent +STATIC_DIR = BASE_DIR / "static" ENV_FILE = BASE_DIR / ".env" SCRIPT_FILE = BASE_DIR / "telegram-scraper.py" STATE_FILE = BASE_DIR / "state.json" @@ -89,6 +90,25 @@ app.add_middleware( same_site="lax", ) templates = Jinja2Templates(directory=str(TEMPLATES_DIR)) +templates.env.globals["app_url"] = with_url_prefix +templates.env.globals["url_prefix"] = web_url_prefix + +@app.get("/favicon.ico", include_in_schema=False) +async def favicon_ico(): + path = STATIC_DIR / "favicon.png" + if path.is_file(): + return FileResponse(path, media_type="image/png") + return Response(status_code=204) + + +_favicon_prefix = web_url_prefix() +if _favicon_prefix: + app.add_api_route( + f"{_favicon_prefix}/favicon.ico", + favicon_ico, + methods=["GET"], + include_in_schema=False, + ) # Starlette 较新版本:TemplateResponse(request, name, context);旧版:(name, context) _template_response_new_style: Optional[bool] = None @@ -748,9 +768,6 @@ async def index(request: Request): "account_channels": account_channels, "console_authed": authed, "need_auth_banner": request.query_params.get("needauth") == "1", - "app_url": with_url_prefix, - "url_prefix": web_url_prefix(), - "app_home": app_home_url(), "connected": service.is_connected(), "job_running": service.is_job_running(), "job_name": service.job_name, diff --git a/static/favicon.png b/static/favicon.png new file mode 100644 index 0000000..c6a734f Binary files /dev/null and b/static/favicon.png differ diff --git a/templates/index.html b/templates/index.html index bd18f3a..162e849 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,6 +4,7 @@ Telegram Scraper 控制台 + @@ -1037,7 +1038,7 @@