Spaces:
Running
Running
Monteeq CI commited on
Commit ·
d8cd523
1
Parent(s): 8ed5529
🚀 Deploy from GitHub @ f03c02b1ad1ec9f03d0347385532e5f46a9cee19
Browse filesTriggered by: Smasduq
Message: Merge pull request #16 from Monteeq/feat/env-driven-cors-and-rust-url
Feat/env driven cors and rust url
- app/api/v1/endpoints/metrics.py +5 -2
- app/core/config.py +19 -0
- main.py +6 -25
- scripts/delete_failed_videos.py +84 -0
app/api/v1/endpoints/metrics.py
CHANGED
|
@@ -33,8 +33,11 @@ def public_platform_stats(db: Session = Depends(get_db)):
|
|
| 33 |
.scalar()
|
| 34 |
or 0
|
| 35 |
)
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
likes = db.query(func.count(Like.id)).scalar() or 0
|
| 39 |
comments = db.query(func.count(Comment.id)).scalar() or 0
|
| 40 |
open_challenges = (
|
|
|
|
| 33 |
.scalar()
|
| 34 |
or 0
|
| 35 |
)
|
| 36 |
+
approved = Video.status == "approved"
|
| 37 |
+
videos = db.query(func.count(Video.id)).filter(approved).scalar() or 0
|
| 38 |
+
views = (
|
| 39 |
+
db.query(func.coalesce(func.sum(Video.views), 0)).filter(approved).scalar() or 0
|
| 40 |
+
)
|
| 41 |
likes = db.query(func.count(Like.id)).scalar() or 0
|
| 42 |
comments = db.query(func.count(Comment.id)).scalar() or 0
|
| 43 |
open_challenges = (
|
app/core/config.py
CHANGED
|
@@ -15,6 +15,25 @@ RUST_SERVICE_URL = os.getenv("RUST_SERVICE_URL")
|
|
| 15 |
FRONTEND_URL = os.getenv("FRONTEND_URL", "")
|
| 16 |
STATIC_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "static")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Database Configuration
|
| 19 |
DATABASE_URL = os.getenv("DATABASE_URL")
|
| 20 |
|
|
|
|
| 15 |
FRONTEND_URL = os.getenv("FRONTEND_URL", "")
|
| 16 |
STATIC_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "static")
|
| 17 |
|
| 18 |
+
# Comma-separated browser origins for CORS + AntiBot preflight headers.
|
| 19 |
+
# Example: https://monteeq.com,https://www.monteeq.com,http://localhost:3000
|
| 20 |
+
def _parse_origins(raw: str) -> list[str]:
|
| 21 |
+
return [origin.strip().rstrip("/") for origin in (raw or "").split(",") if origin.strip()]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
CORS_ALLOWED_ORIGINS = _parse_origins(os.getenv("CORS_ALLOWED_ORIGINS", ""))
|
| 25 |
+
_frontend = (FRONTEND_URL or "").strip().rstrip("/")
|
| 26 |
+
if _frontend and _frontend not in CORS_ALLOWED_ORIGINS:
|
| 27 |
+
CORS_ALLOWED_ORIGINS.append(_frontend)
|
| 28 |
+
|
| 29 |
+
# Optional regex for additional origin matches (local ports by default).
|
| 30 |
+
# Set to empty string to disable.
|
| 31 |
+
CORS_ALLOW_ORIGIN_REGEX = os.getenv(
|
| 32 |
+
"CORS_ALLOW_ORIGIN_REGEX",
|
| 33 |
+
r"http://(localhost|127\.0\.0\.1)(:\d+)?$",
|
| 34 |
+
).strip() or None
|
| 35 |
+
|
| 36 |
+
|
| 37 |
# Database Configuration
|
| 38 |
DATABASE_URL = os.getenv("DATABASE_URL")
|
| 39 |
|
main.py
CHANGED
|
@@ -4,6 +4,7 @@ from fastapi.responses import JSONResponse
|
|
| 4 |
from app.api.v1.api import api_router
|
| 5 |
from app.api.v1.endpoints import seo
|
| 6 |
from app.core import dependencies
|
|
|
|
| 7 |
from app.core.error_handlers import register_exception_handlers
|
| 8 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 9 |
from fastapi_cache import FastAPICache
|
|
@@ -21,6 +22,7 @@ import logging
|
|
| 21 |
import os
|
| 22 |
|
| 23 |
logger = logging.getLogger("monteeq")
|
|
|
|
| 24 |
|
| 25 |
app = FastAPI(
|
| 26 |
title="Monteeq Video Platform",
|
|
@@ -48,16 +50,6 @@ app.add_middleware(RequestIDMiddleware)
|
|
| 48 |
class AntiBotMiddleware(BaseHTTPMiddleware):
|
| 49 |
"""Identify and block malicious bots globally with CORS support."""
|
| 50 |
|
| 51 |
-
ALLOWED_ORIGINS = {
|
| 52 |
-
"https://www.monteeq.com",
|
| 53 |
-
"https://monteeq.com",
|
| 54 |
-
"https://admin.monteeq.com",
|
| 55 |
-
"https://www.admin.monteeq.com",
|
| 56 |
-
"https://monteeqorg-backend.hf.space",
|
| 57 |
-
"http://localhost:5173",
|
| 58 |
-
"http://localhost:5174",
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
async def dispatch(self, request: Request, call_next):
|
| 62 |
from app.core import security
|
| 63 |
|
|
@@ -67,7 +59,7 @@ class AntiBotMiddleware(BaseHTTPMiddleware):
|
|
| 67 |
if request.method == "OPTIONS":
|
| 68 |
origin = request.headers.get("Origin", "")
|
| 69 |
response = JSONResponse(status_code=200, content={})
|
| 70 |
-
if origin in
|
| 71 |
response.headers["Access-Control-Allow-Origin"] = origin
|
| 72 |
response.headers["Access-Control-Allow-Credentials"] = "true"
|
| 73 |
response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, PATCH, OPTIONS"
|
|
@@ -91,7 +83,7 @@ class AntiBotMiddleware(BaseHTTPMiddleware):
|
|
| 91 |
status_code=403,
|
| 92 |
content={"detail": "Access denied. Automated traffic detected.", "ua": ua}
|
| 93 |
)
|
| 94 |
-
if origin in
|
| 95 |
response.headers["Access-Control-Allow-Origin"] = origin
|
| 96 |
response.headers["Access-Control-Allow-Credentials"] = "true"
|
| 97 |
return response
|
|
@@ -104,19 +96,8 @@ app.add_middleware(AntiBotMiddleware)
|
|
| 104 |
# CORS middleware - MUST be added before other middleware
|
| 105 |
app.add_middleware(
|
| 106 |
CORSMiddleware,
|
| 107 |
-
allow_origins=
|
| 108 |
-
|
| 109 |
-
"https://monteeq.com",
|
| 110 |
-
"https://admin.monteeq.com",
|
| 111 |
-
"https://www.admin.monteeq.com",
|
| 112 |
-
"https://monteeqorg-backend.hf.space",
|
| 113 |
-
"http://localhost:5173",
|
| 114 |
-
"http://localhost:5174",
|
| 115 |
-
"http://127.0.0.1:5174",
|
| 116 |
-
],
|
| 117 |
-
# Regex covers any localhost / 127.0.0.1 port for local dev.
|
| 118 |
-
# Production origins are covered by the explicit list above.
|
| 119 |
-
allow_origin_regex=r"http://(localhost|127\.0\.0\.1)(:\d+)?$",
|
| 120 |
allow_credentials=True,
|
| 121 |
allow_methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
|
| 122 |
allow_headers=["*"],
|
|
|
|
| 4 |
from app.api.v1.api import api_router
|
| 5 |
from app.api.v1.endpoints import seo
|
| 6 |
from app.core import dependencies
|
| 7 |
+
from app.core.config import CORS_ALLOW_ORIGIN_REGEX, CORS_ALLOWED_ORIGINS
|
| 8 |
from app.core.error_handlers import register_exception_handlers
|
| 9 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 10 |
from fastapi_cache import FastAPICache
|
|
|
|
| 22 |
import os
|
| 23 |
|
| 24 |
logger = logging.getLogger("monteeq")
|
| 25 |
+
ALLOWED_ORIGINS = set(CORS_ALLOWED_ORIGINS)
|
| 26 |
|
| 27 |
app = FastAPI(
|
| 28 |
title="Monteeq Video Platform",
|
|
|
|
| 50 |
class AntiBotMiddleware(BaseHTTPMiddleware):
|
| 51 |
"""Identify and block malicious bots globally with CORS support."""
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
async def dispatch(self, request: Request, call_next):
|
| 54 |
from app.core import security
|
| 55 |
|
|
|
|
| 59 |
if request.method == "OPTIONS":
|
| 60 |
origin = request.headers.get("Origin", "")
|
| 61 |
response = JSONResponse(status_code=200, content={})
|
| 62 |
+
if origin in ALLOWED_ORIGINS:
|
| 63 |
response.headers["Access-Control-Allow-Origin"] = origin
|
| 64 |
response.headers["Access-Control-Allow-Credentials"] = "true"
|
| 65 |
response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, PATCH, OPTIONS"
|
|
|
|
| 83 |
status_code=403,
|
| 84 |
content={"detail": "Access denied. Automated traffic detected.", "ua": ua}
|
| 85 |
)
|
| 86 |
+
if origin in ALLOWED_ORIGINS:
|
| 87 |
response.headers["Access-Control-Allow-Origin"] = origin
|
| 88 |
response.headers["Access-Control-Allow-Credentials"] = "true"
|
| 89 |
return response
|
|
|
|
| 96 |
# CORS middleware - MUST be added before other middleware
|
| 97 |
app.add_middleware(
|
| 98 |
CORSMiddleware,
|
| 99 |
+
allow_origins=CORS_ALLOWED_ORIGINS,
|
| 100 |
+
allow_origin_regex=CORS_ALLOW_ORIGIN_REGEX,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
allow_credentials=True,
|
| 102 |
allow_methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
|
| 103 |
allow_headers=["*"],
|
scripts/delete_failed_videos.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""One-off: delete videos with status='failed' and related rows."""
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
|
| 4 |
+
load_dotenv()
|
| 5 |
+
|
| 6 |
+
from sqlalchemy import create_engine, text, inspect
|
| 7 |
+
from app.core.config import DATABASE_URL
|
| 8 |
+
|
| 9 |
+
engine = create_engine(DATABASE_URL)
|
| 10 |
+
|
| 11 |
+
CANDIDATE_TABLES = [
|
| 12 |
+
"views",
|
| 13 |
+
"likes",
|
| 14 |
+
"comments",
|
| 15 |
+
"challenge_entries",
|
| 16 |
+
"reposts",
|
| 17 |
+
"watch_later",
|
| 18 |
+
"video_interactions",
|
| 19 |
+
"content_reports",
|
| 20 |
+
"watch_history",
|
| 21 |
+
"library_watch_later",
|
| 22 |
+
"liked_videos",
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
insp = inspect(engine)
|
| 28 |
+
|
| 29 |
+
with engine.connect() as c:
|
| 30 |
+
rows = c.execute(
|
| 31 |
+
text("SELECT status, COUNT(*) AS n FROM videos GROUP BY status ORDER BY n DESC")
|
| 32 |
+
).fetchall()
|
| 33 |
+
print("STATUS COUNTS:")
|
| 34 |
+
for r in rows:
|
| 35 |
+
print(f" {r[0]!r}: {r[1]}")
|
| 36 |
+
|
| 37 |
+
failed_total = c.execute(
|
| 38 |
+
text("SELECT COUNT(*) FROM videos WHERE status = 'failed'")
|
| 39 |
+
).scalar()
|
| 40 |
+
print(f"FAILED TOTAL: {failed_total}")
|
| 41 |
+
if not failed_total:
|
| 42 |
+
return
|
| 43 |
+
|
| 44 |
+
with engine.begin() as c:
|
| 45 |
+
pinned = c.execute(
|
| 46 |
+
text(
|
| 47 |
+
"UPDATE users SET pinned_video_id = NULL "
|
| 48 |
+
"WHERE pinned_video_id IN (SELECT id FROM videos WHERE status = 'failed')"
|
| 49 |
+
)
|
| 50 |
+
)
|
| 51 |
+
print(f"Cleared pinned_video_id on users: {pinned.rowcount}")
|
| 52 |
+
|
| 53 |
+
for table in CANDIDATE_TABLES:
|
| 54 |
+
if not insp.has_table(table):
|
| 55 |
+
print(f"Skip {table}: table missing")
|
| 56 |
+
continue
|
| 57 |
+
cols = {col["name"] for col in insp.get_columns(table)}
|
| 58 |
+
if "video_id" not in cols:
|
| 59 |
+
print(f"Skip {table}: no video_id column")
|
| 60 |
+
continue
|
| 61 |
+
res = c.execute(
|
| 62 |
+
text(
|
| 63 |
+
f"DELETE FROM {table} WHERE video_id IN "
|
| 64 |
+
f"(SELECT id FROM videos WHERE status = 'failed')"
|
| 65 |
+
)
|
| 66 |
+
)
|
| 67 |
+
print(f"Deleted from {table}: {res.rowcount}")
|
| 68 |
+
|
| 69 |
+
deleted = c.execute(text("DELETE FROM videos WHERE status = 'failed'"))
|
| 70 |
+
print(f"Deleted failed videos: {deleted.rowcount}")
|
| 71 |
+
|
| 72 |
+
with engine.connect() as c:
|
| 73 |
+
left = c.execute(text("SELECT COUNT(*) FROM videos WHERE status = 'failed'")).scalar()
|
| 74 |
+
print(f"Remaining failed: {left}")
|
| 75 |
+
rows = c.execute(
|
| 76 |
+
text("SELECT status, COUNT(*) AS n FROM videos GROUP BY status ORDER BY n DESC")
|
| 77 |
+
).fetchall()
|
| 78 |
+
print("STATUS COUNTS AFTER:")
|
| 79 |
+
for r in rows:
|
| 80 |
+
print(f" {r[0]!r}: {r[1]}")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
main()
|