-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (87 loc) · 3.77 KB
/
main.py
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
import uvicorn
import logging
from fastapi import FastAPI, Request, Depends
from fastapi.responses import JSONResponse, HTMLResponse, RedirectResponse
from fastapi.exceptions import RequestValidationError
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from starlette.exceptions import HTTPException as StarletteHTTPException
from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware
from sqlalchemy.orm import Session
from datetime import datetime
from app.database.database import Base, engine, User, get_database
from app.core.core import app_setting, limiter
from app.controllers.controllers import router as routes
from app.service.service import get_user_from_cookie
app = FastAPI(title=app_setting.APP_NAME, version="1.0.0",
description="some OSINT tool")
logger = logging.getLogger(__name__)
app.include_router(routes, prefix="/api/v1")
Base.metadata.create_all(bind=engine)
app.state.limiter = limiter
app.add_middleware(SlowAPIMiddleware)
app.mount("/static", StaticFiles(directory="app/static"), name="static")
templates = Jinja2Templates(directory="app/templates")
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
return templates.TemplateResponse(request=request, name="index.html", context={"current_year": datetime.now().year})
@app.get("/login")
async def user_login_page(request: Request):
return templates.TemplateResponse(request=request, name="login.html")
@app.get("/profile")
async def user_profile_page(request: Request, current_user: User = Depends(get_user_from_cookie), db: Session = Depends(get_database)):
if not current_user:
return RedirectResponse(url="/login")
return templates.TemplateResponse(request=request, name="profile.html", context={
"request": request,
"user": current_user,
"search": current_user.domains,
"name": get_name_from_email(current_user.email)
})
@app.exception_handler(RateLimitExceeded)
async def rate_limit_handler(request: Request, exc: RateLimitExceeded):
return JSONResponse(
status_code=429,
content={"detail": "Rate limit exceeded"},
)
@app.exception_handler(RequestValidationError)
def validation_exception_handler(request: Request, exception: RequestValidationError):
errors = exception.errors()
error_messages = [f"{e['loc'][-1]}: {e['msg']}" for e in errors]
return JSONResponse(
status_code=422,
content={"detail": error_messages, "body": exception.body},
)
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exception: StarletteHTTPException):
return JSONResponse(
status_code=exception.status_code,
content={"detail": exception.detail},
)
@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exception: Exception):
logger.error(f"Unhandled exception: {str(exception)}", exc_info=True)
return JSONResponse(
status_code=500,
content={
"detail": "An unexpected error occurred. Please try again later.",
"path": request.url.path
},
)
# remove when not in production
@app.middleware("http")
async def add_no_cache_headers(request: Request, call_next):
response = await call_next(request)
if isinstance(response, HTMLResponse):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
def get_name_from_email(email):
at_index = email.find('@')
if at_index != -1:
return email[:at_index]
return email
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)