Files
news_all_spo/web/main.py
2026-05-14 16:47:58 +05:00

66 lines
2.4 KiB
Python

import os
from pathlib import Path
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
import flet as ft
import flet.fastapi as flet_fastapi
import designer as d
import theme_manager
from router import handle_route
from editor_page import router as editor_router
from public_pages import router as public_router
UPLOAD_DIR = os.getenv("FLET_UPLOAD_DIR", "/tmp/flet_uploads")
os.makedirs(UPLOAD_DIR, exist_ok=True)
async def main(page: ft.Page):
page.title = "Новости"
page.padding = 0
page.spacing = 0
page.window.width = 1280
page.window.min_width = 360
page.fonts = {
"Merriweather": "https://cdn.jsdelivr.net/npm/@fontsource/merriweather@latest/files/merriweather-latin-400-normal.woff2",
"Inter": "https://cdn.jsdelivr.net/npm/@fontsource/inter@latest/files/inter-latin-400-normal.woff2",
"Playfair Display": "https://cdn.jsdelivr.net/npm/@fontsource/playfair-display@latest/files/playfair-display-latin-400-normal.woff2",
"JetBrains Mono": "https://cdn.jsdelivr.net/npm/@fontsource/jetbrains-mono@latest/files/jetbrains-mono-latin-400-normal.woff2",
"Roboto": "https://cdn.jsdelivr.net/npm/@fontsource/roboto@latest/files/roboto-latin-400-normal.woff2",
}
theme_manager.apply(page, "light")
async def on_route_change(e: ft.RouteChangeEvent):
theme_manager.reset_font(page)
await handle_route(page)
def on_view_pop(e: ft.ViewPopEvent):
page.views.pop()
if page.views:
page.go(page.views[-1].route)
page.on_route_change = on_route_change
page.on_view_pop = on_view_pop
await handle_route(page)
# Custom routes must be registered on a WRAPPER app BEFORE mounting Flet.
# flet_fastapi registers a catch-all handler internally; anything added to
# the same app via include_router() ends up AFTER that catch-all and is
# never reached. The wrapper ensures our routes are checked first.
_flet = flet_fastapi.app(main, upload_dir=UPLOAD_DIR)
app = FastAPI()
app.include_router(public_router) # GET /article/{slug}
app.include_router(editor_router) # GET /editor/{id} + /api/* proxy
# Serve React editor static assets (JS/CSS chunks built by Vite)
_editor_dist = Path(__file__).parent / "editor-ui" / "dist"
if _editor_dist.exists():
app.mount("/editor-assets", StaticFiles(directory=str(_editor_dist)), name="editor-assets")
app.mount("/", _flet) # everything else → Flet SPA