Skip to content

Commit 6c9838e

Browse files
committed
feat(frontend): add static file serving and integrate frontend resources
1 parent 61d461e commit 6c9838e

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ dist
2121
build
2222
app.spec
2323
design_plan.md
24-
frontend
24+
frontend/test.html
25+
frontend/test_index.html

app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from core.logging import setup_logging
2+
from core.utils import frontend_path
23
from api.system import router as system_router
34
from api.auth import router as auth_router
45
from api.lecture_content import router as lec_content_router
@@ -19,6 +20,16 @@
1920

2021
logger.info("Application startup complete")
2122

23+
static_dir = frontend_path()
24+
25+
app.mount("/static", StaticFiles(directory=static_dir), name="static")
26+
27+
28+
@app.get("/", include_in_schema=False)
29+
def serve_frontend():
30+
return FileResponse(os.path.join(static_dir, "index.html"))
31+
32+
2233
# Include routers
2334
app.include_router(system_router, prefix="/api")
2435
app.include_router(auth_router, prefix="/api")

core/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pathlib import Path
33
from dotenv import load_dotenv, set_key
44
from typing import Any, Optional
5+
import sys
56

67
ENV_PATH = ".env"
78

@@ -56,3 +57,15 @@ def standard_response(
5657
"data": data if success else None,
5758
"status_code": status_code,
5859
}
60+
61+
62+
def resource_path(path):
63+
if hasattr(sys, "_MEIPASS"):
64+
return os.path.join(sys._MEIPASS, path)
65+
return os.path.join(os.path.abspath("."), path)
66+
67+
68+
def frontend_path():
69+
if hasattr(sys, "_MEIPASS"):
70+
return resource_path("frontend")
71+
return "frontend"

frontend/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>pw-client</title>
8+
9+
</head>
10+
11+
<body>
12+
<h1>-- IN PROGRESS --</h1>
13+
</body>
14+
15+
</html>

launcher.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os, ctypes
2+
from app import app
3+
import uvicorn
4+
5+
6+
def enable_vt100():
7+
if os.name == "nt":
8+
kernel32 = ctypes.windll.kernel32
9+
handle = kernel32.GetStdHandle(-11)
10+
mode = ctypes.c_uint()
11+
kernel32.GetConsoleMode(handle, ctypes.byref(mode))
12+
kernel32.SetConsoleMode(handle, mode.value | 0x0004)
13+
14+
15+
enable_vt100()
16+
os.environ["NO_COLOR"] = "1"
17+
os.environ["UVICORN_NO_COLOR"] = "1"
18+
19+
20+
if __name__ == "__main__":
21+
port = int(os.environ.get("PORT", 8000))
22+
uvicorn.run(app, host="0.0.0.0", port=port)

winx64.spec

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
from PyInstaller.utils.hooks import collect_submodules
3+
4+
hidden = collect_submodules('fastapi') + collect_submodules('uvicorn')
5+
6+
block_cipher = None
7+
8+
a = Analysis(
9+
['launcher.py'],
10+
pathex=['.'],
11+
binaries=[],
12+
datas=[
13+
('frontend', 'frontend'),
14+
('schema', 'schema'),
15+
('frontend/src/favicon.ico', 'frontend/src'),
16+
],
17+
hiddenimports=hidden,
18+
hookspath=[],
19+
runtime_hooks=[],
20+
excludes=[],
21+
win_no_prefer_redirects=False,
22+
win_private_assemblies=False,
23+
cipher=block_cipher,
24+
)
25+
26+
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
27+
28+
exe = EXE(
29+
pyz,
30+
a.scripts,
31+
a.binaries,
32+
a.zipfiles,
33+
a.datas,
34+
name='pw-client',
35+
debug=False,
36+
strip=False,
37+
upx=False,
38+
console=True,
39+
icon='frontend/src/favicon.ico',
40+
)
41+

0 commit comments

Comments
 (0)