File size: 2,322 Bytes
4289ed3
 
9bf7fc2
 
 
07505ba
4aebf77
 
9bf7fc2
 
 
 
 
ee2e962
 
1a9eef9
 
 
9609347
9bf7fc2
9609347
9bf7fc2
1a9eef9
 
 
9609347
ee2e962
 
 
 
 
 
 
 
 
9bf7fc2
 
9609347
9bf7fc2
2cfcf56
9609347
2cfcf56
4aebf77
ee2e962
 
 
 
4aebf77
 
9bf7fc2
 
4aebf77
9bf7fc2
 
 
 
 
 
 
9609347
4289ed3
 
 
 
 
 
 
9609347
f394afd
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
import asyncio
from contextlib import asynccontextmanager
from fastapi.responses import RedirectResponse
import uvicorn
from fastapi import FastAPI
from modules.dropbox.audio import cleanup_audio_url_cache
from modules.home.app import home_app
from modules.youtube_metadata.app import youtube_metadata_app
from server import router as mobile_router
from app import gradio_app  # your Blocks object
import gradio as gr
import logging
from fastapi import Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded

logging.basicConfig(level=logging.INFO)

app = FastAPI(title="Sanatan AI Unified Server")
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

# Allow all origins (for dev)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # or ["https://your-flutter-web-app.com"]
    allow_credentials=True,
    allow_methods=["*"],  # GET, POST, PUT, DELETE, OPTIONS
    allow_headers=["*"],  # e.g. Authorization, Content-Type
)

# Mount mobile endpoints
app.include_router(mobile_router, prefix="/api")

# Convert Gradio Blocks to ASGI app
# app = gr.mount_gradio_app(app, gradio_app,"/sanatan_ai_web")

# app = gr.mount_gradio_app(app, youtube_metadata_app,"/yt_web")

app = gr.mount_gradio_app(app, home_app,"/web")

app.mount("/home", StaticFiles(directory="static", html=True), name="static")


# Redirect root URL to /home/
@app.get("/")
async def redirect_to_web():
    return RedirectResponse(url="/home/")

@app.middleware("http")
async def log_requests(request: Request, call_next):
    logging.info(f"Request: {request.method} {request.url}")
    response = await call_next(request)
    logging.info(f"Response status: {response.status_code}")
    return response

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup code: start cache cleanup
    asyncio.create_task(cleanup_audio_url_cache())
    yield
    # Shutdown code (optional) can go here

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=False, access_log=False, log_level=logging.WARNING)