Add the option to serve static files with the http server

This commit is contained in:
Nexus 2023-05-06 17:00:34 +01:00
parent 3dea3a7f3d
commit db8ce9b7f4
Signed by: nex
GPG key ID: 0FA334385D0B689F
2 changed files with 18 additions and 2 deletions

3
.gitignore vendored
View file

@ -7,4 +7,5 @@ geckodriver.log
targets.json targets.json
*.kdev4 *.kdev4
.env .env
*.pyc *.pyc
web/static

View file

@ -4,6 +4,7 @@ import sys
import discord import discord
import os import os
import httpx import httpx
from pathlib import Path
from datetime import datetime, timezone from datetime import datetime, timezone
from hashlib import sha512 from hashlib import sha512
@ -13,20 +14,34 @@ from http import HTTPStatus
from utils import Student, get_or_none, VerifyCode, console, BannedStudentID from utils import Student, get_or_none, VerifyCode, console, BannedStudentID
from config import guilds 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: try:
from config import OAUTH_ID, OAUTH_SECRET, OAUTH_REDIRECT_URI from config import OAUTH_ID, OAUTH_SECRET, OAUTH_REDIRECT_URI
except ImportError: except ImportError:
OAUTH_ID = OAUTH_SECRET = OAUTH_REDIRECT_URI = None 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/" GENERAL = "https://ptb.discord.com/channels/994710566612500550/1018915342317277215/"
OAUTH_ENABLED = OAUTH_ID and OAUTH_SECRET and OAUTH_REDIRECT_URI 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.bot = None
app.state.states = {} app.state.states = {}
app.state.http = httpx.Client() app.state.http = httpx.Client()
if StaticFiles:
app.mount("/static", StaticFiles(directory=SF_ROOT), name="static")
try: try:
from utils.client import bot from utils.client import bot
app.state.bot = bot app.state.bot = bot