diff options
| author | altaf-creator <dev@altafcreator.com> | 2026-05-17 13:01:57 +0800 |
|---|---|---|
| committer | altaf-creator <dev@altafcreator.com> | 2026-05-17 13:01:57 +0800 |
| commit | 9e7fe6d8dad56c237adb1905e130c1d723131812 (patch) | |
| tree | dd067e14b3ec5af719d748427bb322320c248879 /main.py | |
| parent | d04598ba59d7267ee4db3745ff90eaa1a990072a (diff) | |
big structural change, altafcreator.com no longer served statically, projects now use md, blog is being reworked, videos removed temp
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 100 |
1 files changed, 100 insertions, 0 deletions
@@ -0,0 +1,100 @@ +import fastapi +from fastapi.middleware.cors import CORSMiddleware +import fastapi.staticfiles +import fastapi.responses +from dotenv import load_dotenv +from os import getenv +from pydantic import BaseModel +import captcha # captcha.py +import pages # pages.py + +print("Hello, world!") +load_dotenv() + +app = fastapi.FastAPI(title="altafcreator.com", decscription="altafcreator.com API", version="1.0") + +TURNSTILE_SECRET = getenv("CLOUDFLARE_TURNSTILE_SECRET").encode("utf-8") + +origins = [ + "http://localhost", + "https://altafcreator.com", + "https://backend.altafcreator.com" +] +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + +class TurnstileTokenData(BaseModel): + token: str + + +@app.exception_handler(fastapi.HTTPException) +def exception_handling(request: fastapi.Request, exception: fastapi.HTTPException): + if exception.status_code == 404: + return fastapi.responses.FileResponse("./www/404.html", status_code=404) + elif exception.status_code == 209: + return fastapi.responses.FileResponse("./www/209.html", status_code=209) + + +@app.post("/api/email/") +def email(data: TurnstileTokenData): + if captcha.validate_turnstile(data.token): + return { + "status": True, + "message": "here's my email", + "business": "business@altafcreator.com", + "personal": "altaf@altafcreator.com", + } + else: + return { + "status": False, + "message": "Turnstile verification invalid", + } + + +@app.post("/api/blogs/") +def blog_list(): + return pages.blog_list() + + +@app.get("/projects/{project_name}/") +def project_details(project_name: str): + status, html = pages.render_project_details(project_name) + + if status == 200: + return fastapi.responses.Response(content=html, media_type="text/html", status_code=200) + else: + raise fastapi.HTTPException(status_code=status, detail="Project doesn't exist.") + + +@app.get("/projects/{project_name}", include_in_schema=False) +def project_details_redirect(project_name: str): + return fastapi.responses.RedirectResponse( + url=f"/projects/{project_name}/", + status_code=301 + ) + + +@app.get("/blog/{blogpost_id}/") +def blogpost(blogpost_id: int): + status, html = pages.render_blogpost(blogpost_id) + + if status == 200: + return fastapi.responses.Response(content=html, media_type="text/html", status_code=200) + else: + raise fastapi.HTTPException(status_code=status, detail="Blogpost doesn't exist.") + + +@app.get("/blog/{blogpost}", include_in_schema=False) +def blogpost_redirect(blogpost: str): + return fastapi.responses.RedirectResponse( + url=f"/blog/{blogpost}/", + status_code=301 + ) + +app.mount("/", fastapi.staticfiles.StaticFiles(directory="./www/", html=True), name="static") |
