88 lines
2.6 KiB
Python
88 lines
2.6 KiB
Python
import os
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import (
|
|
RedirectResponse,
|
|
)
|
|
from infoserver.dependencies import Dependencies
|
|
from infoserver.router_login import router_login
|
|
from infoserver.router_pdf import router_pdf
|
|
from infoserver.router_static import router_static
|
|
from jwt.exceptions import PyJWTError
|
|
from starlette.middleware.sessions import SessionMiddleware
|
|
|
|
# Set your paths here
|
|
DB_PATH = '~/code/git.ourworld.tf/freeflowuniverse/heroweb/authdb_example'
|
|
TEMPLATES_DIR = '~/code/git.ourworld.tf/freeflowuniverse/heroweb/poc/out'
|
|
STATIC_DIR = '~/code/git.ourworld.tf/freeflowuniverse/heroweb/poc/static'
|
|
|
|
DB_PATH = os.path.abspath(os.path.expanduser(DB_PATH))
|
|
TEMPLATES_DIR = os.path.abspath(os.path.expanduser(TEMPLATES_DIR))
|
|
STATIC_DIR = os.path.abspath(os.path.expanduser(STATIC_DIR))
|
|
|
|
if not os.path.exists(DB_PATH):
|
|
raise FileNotFoundError(f'Database path does not exist: {DB_PATH}')
|
|
if not os.path.exists(TEMPLATES_DIR):
|
|
raise FileNotFoundError(
|
|
f'Templates directory does not exist: {TEMPLATES_DIR}'
|
|
)
|
|
if not os.path.exists(STATIC_DIR):
|
|
raise FileNotFoundError(f'Static directory does not exist: {STATIC_DIR}')
|
|
|
|
app = FastAPI()
|
|
|
|
# Add session middleware for cookie management
|
|
jwt_secret_key = os.getenv('JWT_SECRET_KEY')
|
|
if not jwt_secret_key:
|
|
raise EnvironmentError('JWT_SECRET_KEY environment variable is not set')
|
|
app.add_middleware(SessionMiddleware, secret_key=jwt_secret_key)
|
|
|
|
# Include your routers here
|
|
app.include_router(router_login)
|
|
app.include_router(router_pdf)
|
|
# app.include_router(router_template)
|
|
app.include_router(router_static)
|
|
|
|
deps = Dependencies(DB_PATH, TEMPLATES_DIR, STATIC_DIR)
|
|
app.deps = deps
|
|
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=['*'], # Allows all origins
|
|
allow_credentials=True,
|
|
allow_methods=['*'], # Allows all methods
|
|
allow_headers=['*'], # Allows all headers
|
|
)
|
|
|
|
|
|
@app.middleware('http')
|
|
async def check_authentication(
|
|
request: Request,
|
|
call_next,
|
|
):
|
|
if request.url.path in ['/signup', '/loginsubmit', '/register']:
|
|
return await call_next(request)
|
|
|
|
token = request.cookies.get('access_token')
|
|
if not token:
|
|
return RedirectResponse(url='/signup')
|
|
|
|
jwt_handler = request.app.deps.jwt_handler
|
|
try:
|
|
email = jwt_handler.verify_access_token(token)
|
|
except PyJWTError:
|
|
return RedirectResponse(url='/signup')
|
|
|
|
request.state.email = email
|
|
# print(f'Authenticated: {email}')
|
|
|
|
return await call_next(request)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
import uvicorn
|
|
|
|
uvicorn.run(app, host='0.0.0.0', port=8000)
|