diff --git a/prophet/app.py b/prophet/app.py index 32715a6..78c12b1 100644 --- a/prophet/app.py +++ b/prophet/app.py @@ -7,6 +7,7 @@ from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi_utils.tasks import repeat_every +from prophet import view from prophet.domain.improvement import Improvement from prophet.domain.improvement_repo import IImprovementRepo from prophet.domain.original import Original @@ -64,21 +65,27 @@ def improve_originals(originals: list[Original]) -> list[Improvement]: return improvements -app = FastAPI() -app.mount("/static", StaticFiles(directory="static"), name="static") +def init() -> FastAPI: + app = FastAPI() + app.mount("/static", StaticFiles(directory="static"), name="static") -origins = [ - "http://localhost", - "http://localhost:8080", -] + origins = [ + "http://localhost", + "http://localhost:8080", + ] -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) + app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) + view.define_routes(app) + return app + + +app = init() @app.get("/improve-title") @@ -94,6 +101,7 @@ def improve_summary(original_title: str, new_title: str, original_summary: str): return llm.rewrite_summary(o, new_title) +# TODO: Switch to lifecycle events to avoid deprecated method @app.on_event("startup") @repeat_every(seconds=REFRESH_PERIOD) async def refresh_articles(): diff --git a/prophet/view.py b/prophet/view.py index 6079d60..fbfcf59 100644 --- a/prophet/view.py +++ b/prophet/view.py @@ -1,84 +1,69 @@ +# pyright: reportUnusedFunction=false + +from fastapi import FastAPI from fastapi.responses import HTMLResponse -from prophet import app from prophet.domain.improvement_repo import IImprovementRepo from prophet.infra.improvement_pickle_repo import ImprovementPickleRepo repo: IImprovementRepo = ImprovementPickleRepo() -html_app = app.app - -@html_app.get("/improvements", response_class=HTMLResponse) -def list_improvements(): - improved = repo.get_all() - return ( - """ """ - + "\n".join( - f""" -
-
- -
-
{item.title}
-
{item.summary}
-
""" - for item in sorted(improved, key=lambda i: i.original.date, reverse=True) +def define_routes(app: FastAPI): + @app.get("/improvements", response_class=HTMLResponse) + def list_improvements(): + improved = repo.get_all() + return ( + """ """ + + "\n".join( + f""" +
+
+ +
+
{item.title}
+
{item.summary}
+
""" + for item in sorted( + improved, key=lambda i: i.original.date, reverse=True + ) + ) ) - ) - -@html_app.get("/originals", response_class=HTMLResponse) -def list_originals(): - improved = repo.get_all() - return ( - """ """ - + "\n".join( - f""" -
-
- -
-
{item.original.title}
-
{item.original.summary}
-
""" - for item in sorted(improved, key=lambda i: i.original.date, reverse=True) + @app.get("/originals", response_class=HTMLResponse) + def list_originals(): + improved = repo.get_all() + return ( + """ """ + + "\n".join( + f""" +
+
+ +
+
{item.original.title}
+
{item.original.summary}
+
""" + for item in sorted( + improved, key=lambda i: i.original.date, reverse=True + ) + ) ) - ) - -style = """ -.card { - border: 1px solid #ccc; - padding: 10px; - margin: auto; - margin-bottom: 40px; - width: 600px; -} - -.card-title { - font-size: 24px; - margin-bottom: 5px; -} -""" - - -@html_app.get("/", response_class=HTMLResponse) -def root_route(): - return f""" - - - - The Pollen Prophet - - - - -

The Pollen Prophet

-

Making funny since 2025 what ought not bee.

-
- - - """ + @app.get("/", response_class=HTMLResponse) + def root_route(): + return """ + + + + The Pollen Prophet + + + +

The Pollen Prophet

+

Making funny since 2025 what ought not bee.

+
+ + + """ diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..c731e2a --- /dev/null +++ b/static/style.css @@ -0,0 +1,12 @@ +.card { + border: 1px solid #ccc; + padding: 10px; + margin: auto; + margin-bottom: 40px; + width: 600px; +} + +.card-title { + font-size: 24px; + margin-bottom: 5px; +}