heroweb/herowebserver/main.py

125 lines
3.9 KiB
Python
Raw Normal View History

2024-09-14 03:59:42 +00:00
import logging
2024-09-09 03:56:44 +00:00
import os
2024-09-14 03:59:42 +00:00
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
2024-09-09 03:56:44 +00:00
from fastapi.responses import (
RedirectResponse,
)
2024-09-14 03:59:42 +00:00
from infoserver.dependencies import Dependencies
from infoserver.routers.router_index import router_index
from infoserver.routers.router_login import router_login
from infoserver.routers.router_pdf_preso import router_pdf
from infoserver.routers.router_static import router_static
from infoserver.routers.router_template import router_template
2024-09-09 03:56:44 +00:00
from jwt.exceptions import PyJWTError
from starlette.middleware.sessions import SessionMiddleware
2024-09-14 03:59:42 +00:00
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# 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'
HEROWEB_DIR = '~/code/git.ourworld.tf/tfgrid/info_tfgrid/heroweb'
COLLECTIONS_DIR = '~/hero/var/collections'
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))
STATIC_DIR2 = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
HEROWEB_DIR = os.path.abspath(os.path.expanduser(HEROWEB_DIR))
COLLECTIONS_DIR = os.path.abspath(os.path.expanduser(COLLECTIONS_DIR))
SERVERHOST = 'http://localhost:8000'
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}')
if not os.path.exists(STATIC_DIR2):
raise FileNotFoundError(f'Static directory does not exist: {STATIC_DIR}')
2024-09-09 03:56:44 +00:00
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)
2024-09-14 03:59:42 +00:00
# Include your routers here
app.include_router(router_static)
app.include_router(router_login)
app.include_router(router_pdf)
app.include_router(router_index)
app.include_router(router_template)
deps = Dependencies(
DB_PATH,
TEMPLATES_DIR,
STATIC_DIR,
STATIC_DIR2,
HEROWEB_DIR,
COLLECTIONS_DIR,
SERVERHOST,
2024-09-09 03:56:44 +00:00
)
2024-09-14 03:59:42 +00:00
app.deps = deps
2024-09-09 03:56:44 +00:00
2024-09-14 03:59:42 +00:00
app.add_middleware(
CORSMiddleware,
allow_origins=['*'], # Allows all origins
allow_credentials=True,
allow_methods=['*'], # Allows all methods
allow_headers=['*'], # Allows all headers
2024-09-09 03:56:44 +00:00
)
@app.middleware('http')
2024-09-14 03:59:42 +00:00
async def check_authentication(
request: Request,
call_next,
):
logger.debug(f'Received request for URL: {request.url.path}')
# BYPASS
return await call_next(request)
2024-09-09 03:56:44 +00:00
if request.url.path in ['/signup', '/loginsubmit', '/register']:
2024-09-14 03:59:42 +00:00
logger.debug(
'Skipping authentication for signup, loginsubmit, or register path'
)
2024-09-09 03:56:44 +00:00
return await call_next(request)
token = request.cookies.get('access_token')
if not token:
2024-09-14 03:59:42 +00:00
logger.debug('No access token found, redirecting to /signup')
2024-09-09 03:56:44 +00:00
return RedirectResponse(url='/signup')
2024-09-14 03:59:42 +00:00
jwt_handler = request.app.deps.jwt_handler
2024-09-09 03:56:44 +00:00
try:
email = jwt_handler.verify_access_token(token)
2024-09-14 03:59:42 +00:00
except PyJWTError as e:
logger.error(f'Token verification failed: {e}')
2024-09-09 03:56:44 +00:00
return RedirectResponse(url='/signup')
request.state.email = email
2024-09-14 03:59:42 +00:00
logger.debug(f'Authenticated user: {email}')
2024-09-09 03:56:44 +00:00
2024-09-14 03:59:42 +00:00
response = await call_next(request)
# logger.debug(f'Response status code: {response.status_code}')
2024-09-09 03:56:44 +00:00
return response
2024-09-14 03:59:42 +00:00
if __name__ == '__main__':
import uvicorn
2024-09-09 03:56:44 +00:00
2024-09-14 03:59:42 +00:00
uvicorn.run(app, host='0.0.0.0', port=8000)