File size: 15,760 Bytes
1b4b6d2
 
 
0d6b4f2
 
 
 
 
1b4b6d2
 
 
 
 
46448c1
8ff2b30
46448c1
1b4b6d2
ef1f427
 
 
1b4b6d2
 
 
 
 
 
 
369b6c9
 
51a8001
ef1f427
 
51a8001
e9e7d5f
 
 
 
 
1e220c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f5f4f4
 
 
 
 
 
 
 
 
 
 
 
 
 
51a8001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e220c5
 
ef1f427
 
1e220c5
 
 
 
 
 
ef1f427
 
 
 
 
1b4b6d2
 
 
 
ef1f427
 
1b4b6d2
 
beb2442
 
46448c1
 
beb2442
 
46448c1
 
beb2442
46448c1
58b433f
 
 
 
 
46448c1
1b4b6d2
beb2442
 
1b4b6d2
beb2442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b4b6d2
 
47433e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b4b6d2
 
 
 
 
 
 
 
 
369b6c9
 
51a8001
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
361e46e
 
 
1b4b6d2
 
361e46e
 
 
 
e6d715a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361e46e
 
e6d715a
361e46e
 
 
e6d715a
 
361e46e
 
 
 
 
 
 
 
 
e6d715a
 
 
 
 
 
 
361e46e
 
 
 
 
 
 
 
 
 
 
 
e6d715a
 
 
 
 
 
 
 
 
 
 
361e46e
 
 
e6d715a
 
 
 
1b4b6d2
 
 
7516417
 
 
1b4b6d2
7516417
1b4b6d2
 
 
 
 
 
7516417
1b4b6d2
 
 
 
 
3219688
 
ba2244d
3219688
 
353b13f
 
 
 
 
 
 
 
1b4b6d2
 
 
 
 
 
 
 
 
 
 
 
 
e9e7d5f
244b900
28e1248
 
 
 
244b900
28e1248
 
a255a2f
 
 
 
244b900
a255a2f
 
 
 
 
 
244b900
 
 
 
a255a2f
1b4b6d2
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import warnings
warnings.filterwarnings("ignore", module="streamlit")

# Make the app's own loggers (qto.*) actually emit β€” without this, all the
# diagnostic logging added across the codebase would be silently dropped.
import logging as _logging
_logging.getLogger("qto").setLevel(_logging.INFO)

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import os
import json
import traceback
from fastapi.responses import JSONResponse, FileResponse, HTMLResponse
from fastapi import Request

import asyncio
from contextlib import asynccontextmanager

from api.auth import router as auth_router
from api.projects import router as projects_router
from api.workflow import router as workflow_router
from api.market import router as market_router
from api.agents import router as agents_router
from api.billing import router as billing_router
from api.admin import router as admin_router
from api.modules import router as modules_router
from api.routers.feedback import router as feedback_router
from api.routers.community import router as community_router
from utils.garbage_collection import cache_cleanup_task


# ── RENDER PROXY MODE ──
# If running on Render, serve the high-speed HuggingFace Space via an iframe for all GET requests.
# This allows keeping the custom domain (qshub.online) while using 16GB RAM for processing!
is_render = os.environ.get("RENDER") == "true" or os.environ.get("RENDER") == "1"

def _ensure_feedback_tables():
    """Ensure the ratings + complaints tables exist on MySQL.

    These were only defined in the local SQLite bootstrap, so on the production
    MySQL DB the "Rate Your Experience" insert (and the admin panel that reads
    it) had no table to hit. This runs only the two idempotent CREATE TABLE IF
    NOT EXISTS statements β€” it deliberately does NOT run the full migration
    suite, because migration 008 drops qto_active_projects and would wipe every
    user's in-progress project state on each startup.
    """
    from utils.db import is_sqlite, safe_execute
    if is_sqlite():
        return  # SQLite bootstrap (initialize_sqlite_db) already creates these.
    safe_execute(
        """
        CREATE TABLE IF NOT EXISTS qto_project_feedback (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            tool_name VARCHAR(100) NOT NULL,
            project_name VARCHAR(255) NOT NULL,
            rating INT NOT NULL,
            reason TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
        """
    )
    safe_execute(
        """
        CREATE TABLE IF NOT EXISTS qto_customer_complaints (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            complaint_text TEXT NOT NULL,
            status VARCHAR(50) DEFAULT 'open',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
        """
    )
    safe_execute(
        """
        CREATE TABLE IF NOT EXISTS qto_active_projects (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            project_id INT NOT NULL,
            current_step INT DEFAULT 1,
            state_data JSON,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            UNIQUE KEY unique_user_project (user_id, project_id)
        )
        """
    )
    safe_execute(
        """
        CREATE TABLE IF NOT EXISTS qto_inquiries (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NULL,
            name VARCHAR(255) NULL,
            email VARCHAR(255) NOT NULL,
            subject VARCHAR(255) NOT NULL,
            message TEXT NOT NULL,
            category VARCHAR(50) DEFAULT 'general',
            status VARCHAR(50) DEFAULT 'new',
            admin_notes TEXT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
        """
    )
    safe_execute(
        """
        CREATE TABLE IF NOT EXISTS qto_reviews (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NULL,
            user_name VARCHAR(255) NOT NULL,
            user_role VARCHAR(255) NULL,
            company VARCHAR(255) NULL,
            rating INT NOT NULL,
            review_title VARCHAR(255) NULL,
            review_text TEXT NOT NULL,
            is_approved TINYINT(1) DEFAULT 0,
            is_featured TINYINT(1) DEFAULT 0,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
        """
    )
    try:
        from utils.db import seed_mockup_reviews
        seed_mockup_reviews()
    except Exception as e:
        _logging.getLogger("qto").warning("seed mockup reviews failed: %s", e)



@asynccontextmanager
async def lifespan(app: FastAPI):
    # Make sure the feedback/complaints tables exist (MySQL prod). Best-effort:
    # never let a DB hiccup stop the app from booting.
    try:
        _ensure_feedback_tables()
    except Exception as _e:
        _logging.getLogger("qto").warning("ensure feedback tables failed: %s", _e)
    # Start the garbage collection background task on startup
    task = asyncio.create_task(cache_cleanup_task())
    yield
    # Cancel the task on shutdown
    task.cancel()

app = FastAPI(
    title="THE QS HUB API",
    description="SaaS Backend API for Quantity Takeoff (QTO) and BOQ Automation",
    version="1.0.0",
    lifespan=lifespan
)

from fastapi.exceptions import HTTPException as _StarletteHTTPException

@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    if isinstance(exc, _StarletteHTTPException):
        return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail})
    err_str = traceback.format_exc()
    _logging.getLogger("qto").error("GLOBAL EXCEPTION: %s", err_str)
    return JSONResponse(status_code=500, content={"detail": str(exc), "traceback": err_str})

@app.get("/test-health")
def test_health():
    return {"status": "ok"}



# Enable CORS for the React frontend.
# Supports qshub.online, huggingface.co, render.com, localhost and all subdomains seamlessly.
_cors_env = os.environ.get("CORS_ALLOW_ORIGINS", "*")
ALLOWED_ORIGINS = [o.strip() for o in _cors_env.split(",") if o.strip()]
if "*" in ALLOWED_ORIGINS or not ALLOWED_ORIGINS:
    app.add_middleware(
        CORSMiddleware,
        allow_origin_regex=r".*",
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
else:
    app.add_middleware(
        CORSMiddleware,
        allow_origins=ALLOWED_ORIGINS,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )


# ------------------------------------------------------------------
# Non-blocking Visitor Tracking Middleware
# ------------------------------------------------------------------
from utils.visitor import log_visitor_hit

@app.middleware("http")
async def track_visitors_middleware(request: Request, call_next):
    path = request.url.path
    method = request.method

    if method != "OPTIONS":
        skip_prefixes = ("/assets/", "/favicon", "/health", "/test-health", "/docs", "/openapi", "/redoc")
        skip_extensions = (".js", ".css", ".png", ".jpg", ".jpeg", ".svg", ".ico", ".woff", ".woff2", ".ttf")

        if not path.startswith(skip_prefixes) and not any(path.endswith(ext) for ext in skip_extensions):
            client_ip = (
                request.headers.get("cf-connecting-ip")
                or request.headers.get("x-forwarded-for", "").split(",")[0].strip()
                or request.headers.get("x-real-ip")
                or (request.client.host if request.client else "127.0.0.1")
            )
            user_agent = request.headers.get("user-agent", "")
            referer = request.headers.get("referer", "")
            try:
                log_visitor_hit(client_ip, path, user_agent, referer)
            except Exception:
                pass

    response = await call_next(request)
    return response



# Include API Routers
app.include_router(auth_router, prefix="/api/auth", tags=["Authentication"])
app.include_router(projects_router, prefix="/api/projects", tags=["Projects"])
app.include_router(workflow_router, prefix="/api/workflow", tags=["Workflow"])
app.include_router(market_router, prefix="/api/market", tags=["Market Prices"])
app.include_router(agents_router, prefix="/api/agents", tags=["AI Agents"])
app.include_router(billing_router, prefix="/api/billing", tags=["Billing"])
app.include_router(admin_router, prefix="/api/admin", tags=["Admin Dashboard"])
app.include_router(modules_router, prefix="/api/modules", tags=["Programme & Cash Flow"])
app.include_router(feedback_router, prefix="/api/feedback", tags=["Feedback"])
app.include_router(community_router, prefix="/api", tags=["Community & Reviews"])


@app.post("/webhooks/dodopayments")
async def dodo_webhook(request: Request):
    payload = await request.body()
    try:
        from utils.payments import handle_dodo_webhook
        ok, message = handle_dodo_webhook(payload, request.headers)
        return {"ok": ok, "message": message}
    except Exception as exc:
        return JSONResponse({"ok": False, "error": str(exc)}, status_code=400)

# Cached PDF page images. These are CONFIDENTIAL engineering drawings, so the
# folder is NOT served as public static files. Access requires a valid session
# and (for project-scoped paths) ownership of that project.
cache_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "_qto_cache")
os.makedirs(cache_dir, exist_ok=True)

from fastapi import HTTPException as _HTTPException
from fastapi.responses import FileResponse

# ── Cache-image serving speedup ──
# A drawing page fires one <img> request per preview, so this route runs in a
# burst of dozens of hits. The security-hardening pass replaced the old instant
# StaticFiles mount with per-request DB auth (token check + ownership check),
# which turned every image into 2 remote-DB round-trips and made previews slow.
# We keep the exact same security, but:
#   1. run as a plain `def` (FastAPI runs it in a threadpool, so the blocking DB
#      calls no longer freeze the whole server's event loop), and
#   2. cache the token/ownership results in-memory for a few seconds so a burst
#      of images for the same user/project doesn't re-hit the DB every time.
# Revocation still takes effect after the short TTL expires.
import time as _time

_AUTH_TTL = 60           # seconds a verified token is trusted before re-checking
_OWN_TTL = 300           # seconds an ownership result is cached
_auth_cache: dict = {}   # token -> (expires_at, user_dict)
_own_cache: dict = {}     # (user_id, pid) -> expires_at


@app.get("/cache/{file_path:path}")
def serve_cache(file_path: str, request: Request):
    from api.auth import verify_session_token
    from utils.db import safe_query

    now = _time.time()

    # Token from ?token= / ?Authorization= (img tags can't set headers) or header.
    token = request.query_params.get("token") or request.query_params.get("Authorization")
    if not token:
        auth = request.headers.get("authorization", "")
        token = auth
    if token and token.startswith("Bearer "):
        token = token.split(" ", 1)[1]
    if not token:
        raise _HTTPException(status_code=401, detail="Authentication required.")

    cached = _auth_cache.get(token)
    if cached and cached[0] > now:
        user = cached[1]
    else:
        user = verify_session_token(token)  # raises 401 on invalid/expired
        _auth_cache[token] = (now + _AUTH_TTL, user)

    # Path-traversal protection.
    rel = os.path.normpath(file_path).replace("\\", "/")
    full = os.path.abspath(os.path.join(cache_dir, rel))
    if not full.startswith(os.path.abspath(cache_dir) + os.sep):
        raise _HTTPException(status_code=400, detail="Invalid path.")

    # Ownership: cache images live under /cache/{project_id}/... β€” verify the
    # requester owns that project (admins bypass).
    parts = rel.split("/")
    if len(parts) >= 2 and parts[0].isdigit() and user.get("role") != "admin":
        pid = int(parts[0])
        own_key = (user["id"], pid)
        own_exp = _own_cache.get(own_key)
        if not (own_exp and own_exp > now):
            owned = safe_query(
                "SELECT 1 FROM qto_projects WHERE id=%s AND user_id=%s "
                "UNION SELECT 1 FROM qto_active_projects WHERE project_id=%s AND user_id=%s",
                (pid, user["id"], pid, user["id"]),
            )
            if owned.empty:
                raise _HTTPException(status_code=403, detail="Access denied.")
            _own_cache[own_key] = now + _OWN_TTL

    if not os.path.isfile(full):
        raise _HTTPException(status_code=404, detail="Not found.")
    # Let the browser reuse the image instead of re-downloading it on every
    # revisit. Short max-age + ETag revalidation means a re-upload to the same
    # project (which reuses filenames) still gets picked up quickly.
    return FileResponse(full, headers={"Cache-Control": "private, max-age=300"})

SETTINGS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "user_settings.json")

from fastapi import Depends
from api.admin import verify_admin

@app.get("/api/settings")
def get_settings(admin: dict = Depends(verify_admin)):
    if os.path.exists(SETTINGS_FILE):
        with open(SETTINGS_FILE, "r", encoding="utf-8") as f:
            return json.load(f)
    return {}

@app.post("/api/settings")
async def save_settings(request: Request, admin: dict = Depends(verify_admin)):
    data = await request.json()
    with open(SETTINGS_FILE, "w", encoding="utf-8") as f:
        json.dump(data, f, indent=2)
    return {"status": "success"}

@app.get("/test-html", response_class=HTMLResponse)
def test_html():
    print("HTML ENDPOINT HIT!")
    return "<html><body>Hello HF Space!</body></html>"

@app.get("/api/logs")
async def get_logs():
    try:
        with open("app.log", "r") as f:
            return {"logs": f.readlines()[-50:]}
    except Exception as e:
        return {"error": str(e)}

@app.get("/api/health")
async def health():
    return {
        "status": "online",
        "message": "THE QS HUB API is fully operational."
    }

# ── Serve the built React SPA (production) ──
# Mounted LAST so /api/* and /cache/* always take precedence over the catch-all.
# Skipped automatically in dev (no frontend/dist yet) β€” use `npm run dev` there.
_frontend_dist = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "frontend", "dist"
)

if os.path.isdir(_frontend_dist):
    @app.get("/")
    async def serve_index():
        index_path = os.path.join(_frontend_dist, "index.html")
        if os.path.exists(index_path):
            return FileResponse(index_path)
        return {"detail": "Not Found"}

    @app.get("/assets/{file_path:path}")
    async def serve_assets(file_path: str):
        full_path = os.path.join(_frontend_dist, "assets", file_path)
        if os.path.isfile(full_path):
            return FileResponse(full_path)
        return {"detail": "Not Found"}

    @app.get("/{file_name}")
    async def serve_root_files(file_name: str):
        full_path = os.path.join(_frontend_dist, file_name)
        if os.path.isfile(full_path):
            return FileResponse(full_path)
        index_path = os.path.join(_frontend_dist, "index.html")
        if os.path.exists(index_path):
            return FileResponse(index_path)
        return {"detail": "Not Found"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("api.main:app", host="0.0.0.0", port=8000, reload=True)