-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·88 lines (69 loc) · 2.72 KB
/
app.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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from fastapi.responses import JSONResponse, RedirectResponse
import os
import logging
from dotenv import load_dotenv
logger = logging.getLogger(__name__)
# Get the current environment
env = os.environ.get('ENVIRONMENT', 'development')
if env == 'production':
load_dotenv('.env.production')
logger.info(f"Production environment detected.")
else:
load_dotenv('.env.development')
logger.info(f"Development environment detected.")
# Import routers AFTER loading the environment variables
from auth_router import router as auth_router
from storegenerator.store_router import router as store_router
from ruleslawyer.ruleslawyer_router import router as lawyer_router
app = FastAPI()
# Add SessionMiddleware
app.add_middleware(
SessionMiddleware,
secret_key=os.environ.get("SESSION_SECRET_KEY"),
same_site="lax", # This allows cookies to be sent in cross-site requests
https_only=True, # Set to True in production
domain=".dungeonmind.net" # Set to .dungeonmind.net in production
)
# Set allowed hosts based on the environment
# This is a comma-separated list of hosts, so we need to split it
allowed_hosts = os.environ.get('ALLOWED_HOSTS', '').split(',')
logger.info(f"Allowed hosts: {allowed_hosts}")
react_landing_url = os.environ.get('REACT_LANDING_URL')
logger.info(f"React landing URL: {react_landing_url}")
# Add the middleware with the appropriate allowed hosts
app.add_middleware(TrustedHostMiddleware, allowed_hosts=allowed_hosts)
# CORS Middleware
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_hosts,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Routers
app.include_router(auth_router, prefix='/api/auth')
app.include_router(store_router, prefix="/api/store")
app.include_router(lawyer_router, prefix="/api/ruleslawyer")
# Health check route
@app.get("/health", response_class=JSONResponse)
async def health_check():
return {"status": "ok"}
# Serve React app directly
@app.get("/", response_class=RedirectResponse)
async def serve_react_app():
return RedirectResponse(url=react_landing_url)
#return the dungeonmind server api root url
@app.get("/config")
async def get_config():
return {"DUNGEONMIND_API_URL": os.environ.get('DUNGEONMIND_API_URL')}
# Static files
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/saved_data", StaticFiles(directory="saved_data"), name="saved_data")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)