From db8ce9b7f4bf38e8c56816b7ea36b995c298ae9d Mon Sep 17 00:00:00 2001 From: nex Date: Sat, 6 May 2023 17:00:34 +0100 Subject: [PATCH] Add the option to serve static files with the http server --- .gitignore | 3 ++- web/server.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 89b8580..712c98c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ geckodriver.log targets.json *.kdev4 .env -*.pyc \ No newline at end of file +*.pyc +web/static \ No newline at end of file diff --git a/web/server.py b/web/server.py index 29dd3d1..cf54b4c 100644 --- a/web/server.py +++ b/web/server.py @@ -4,6 +4,7 @@ import sys import discord import os import httpx +from pathlib import Path from datetime import datetime, timezone from hashlib import sha512 @@ -13,20 +14,34 @@ from http import HTTPStatus from utils import Student, get_or_none, VerifyCode, console, BannedStudentID from config import guilds +SF_ROOT = Path(__file__).parent / "static" +if SF_ROOT.exists() and SF_ROOT.is_dir(): + from fastapi.staticfiles import StaticFiles +else: + StaticFiles = None + try: from config import OAUTH_ID, OAUTH_SECRET, OAUTH_REDIRECT_URI except ImportError: OAUTH_ID = OAUTH_SECRET = OAUTH_REDIRECT_URI = None +try: + from config import WEB_ROOT_PATH +except ImportError: + WEB_ROOT_PATH = "" + GENERAL = "https://ptb.discord.com/channels/994710566612500550/1018915342317277215/" OAUTH_ENABLED = OAUTH_ID and OAUTH_SECRET and OAUTH_REDIRECT_URI -app = FastAPI(root_path="/jimmy") +app = FastAPI(root_path=WEB_ROOT_PATH) app.state.bot = None app.state.states = {} app.state.http = httpx.Client() +if StaticFiles: + app.mount("/static", StaticFiles(directory=SF_ROOT), name="static") + try: from utils.client import bot app.state.bot = bot