File size: 2,181 Bytes
5bea760
 
74cf6bd
 
 
 
 
 
 
5bea760
 
74cf6bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bea760
 
 
 
 
 
 
 
 
74cf6bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any

from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from app.api import router as api_router
from app.services.video_service import get_video_by_id
from jinja2 import pass_context
from starlette.datastructures import URL

app = FastAPI(title="In-Video Search", docs_url=None, redoc_url=None, openapi_url=None)

# Enable CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Adjust this in production
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount static files
app.mount("/static", StaticFiles(directory="app/static"), name="static")

# Templates
templates = Jinja2Templates(directory="app/templates")


@pass_context
def https_url_for(context: dict, name: str, **path_params: Any) -> URL:
    request: Request = context["request"]
    url: URL = request.url_for(name, **path_params)
    return url.replace(scheme="https")

templates.env.globals["https_url_for"] = https_url_for


@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
    return templates.TemplateResponse(
        "index.html", {"request": request, "title": "In-Video Search"}
    )


@app.get("/video/{video_id}", response_class=HTMLResponse)
async def video_page(request: Request, video_id: str):
    # Try to get video info from database
    video = get_video_by_id(video_id)
    title = "Video Player"

    # If video exists and has a title, use it
    if video and video.title:
        title = video.title

    return templates.TemplateResponse(
        "video.html",
        {"request": request, "title": title, "video_id": video_id},
    )


@app.get("/watch")
async def watch_redirect(request: Request, v: str):
    # Redirect YouTube-style URLs to our video page
    return RedirectResponse(url=f"/video/{v}")


# Include API routers
app.include_router(api_router.router, prefix="/api")


if __name__ == "__main__":
    import uvicorn

    uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)