1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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")
|