-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
148 lines (131 loc) · 4.59 KB
/
main.py
File metadata and controls
148 lines (131 loc) · 4.59 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from fastapi import FastAPI, Request, Depends, Path
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, RedirectResponse
from sqlalchemy.ext.asyncio import AsyncSession
from contextlib import asynccontextmanager
from app.database import init_db, get_db
from app.utils.auth import get_current_user_from_request
from app.routes.auth_routes import router as auth_router
import logging
from dotenv import load_dotenv
# Import models to ensure they're registered with SQLModel before database initialization
from app.models.user import User, Session, PasswordResetToken # noqa: F401
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage application lifespan."""
# Startup
try:
await init_db()
logger.info("Database initialized successfully")
except Exception as e:
logger.error(f"Database initialization error: {str(e)}", exc_info=True)
# Continue startup even if DB init fails (for development)
pass
yield
# Shutdown: nothing yet
# Create FastAPI app
app = FastAPI(
title="bluehex2",
description="A FastAPI application with signup/signin functionality",
version="1.0.0",
lifespan=lifespan,
)
# Setup templates and static files
templates = Jinja2Templates(directory="app/templates")
app.mount("/static", StaticFiles(directory="app/static"), name="static")
# Include auth routes
app.include_router(auth_router)
# Rayo template routes (excluding auth/contact which are handled elsewhere)
RAYO_PAGES = [
"404",
"about-me",
"about-us",
"blog-article",
"blog-creative",
"blog-standard",
"faq",
"index",
"index-creative-design-studio",
"index-creative-developer",
"index-designer",
"index-digital-agency",
"index-freelancer-portfolio",
"index-main",
"index-personal-portfolio",
"index-software-development-company",
"index-web-agency",
"pricing",
"project-details",
"services",
"team",
"works-masonry",
"works-simple",
]
RAYO_PAGE_REGEX = "^(?:" + "|".join(RAYO_PAGES) + ")$"
@app.get("/", response_class=HTMLResponse)
async def root(request: Request, db: AsyncSession = Depends(get_db)):
"""Root endpoint."""
user = await get_current_user_from_request(request, db)
return templates.TemplateResponse(request, "index.html", {
"request": request,
"user": user,
"is_logged_in": user is not None,
"current_path": "/"
})
@app.get("/customers-love-us", response_class=HTMLResponse)
async def customers_love_us_page(request: Request, db: AsyncSession = Depends(get_db)):
"""Show customers love us page."""
user = await get_current_user_from_request(request, db)
return templates.TemplateResponse(request, "customers-love-us.html", {
"request": request,
"user": user,
"is_logged_in": user is not None,
"current_path": "/customers-love-us"
})
@app.get("/bluehex-blocks", response_class=HTMLResponse)
async def bluehex_blocks_page(request: Request, db: AsyncSession = Depends(get_db)):
"""Show bluehex blocks page."""
user = await get_current_user_from_request(request, db)
return templates.TemplateResponse(request, "bluehex-blocks.html", {
"request": request,
"user": user,
"is_logged_in": user is not None,
"current_path": "/bluehex-blocks"
})
@app.get("/contact", response_class=HTMLResponse)
async def contact_page(request: Request, db: AsyncSession = Depends(get_db)):
"""Show contact page."""
user = await get_current_user_from_request(request, db)
return templates.TemplateResponse(request, "contact.html", {
"request": request,
"user": user,
"is_logged_in": user is not None,
"current_path": "/contact"
})
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {"status": "healthy"}
@app.get("/{page}", response_class=HTMLResponse)
async def rayo_page(
request: Request,
page: str = Path(..., pattern=RAYO_PAGE_REGEX),
db: AsyncSession = Depends(get_db),
):
"""Serve other Rayo pages."""
user = await get_current_user_from_request(request, db)
return templates.TemplateResponse(request, f"{page}.html", {
"request": request,
"user": user,
"is_logged_in": user is not None,
"current_path": f"/{page}"
})
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)