mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-04 22:13:08 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""Centralized utilities for API routes.
|
|
|
|
Post-Mongo-cutover slim: the old Mongo-shaped helpers (``validate_object_id``,
|
|
``check_resource_ownership``, ``paginated_response``, ``serialize_object_id``,
|
|
``safe_db_operation``, ``validate_enum``, ``extract_sort_params``) have been
|
|
removed — they carried ``bson`` / ``pymongo`` imports and had zero callers.
|
|
"""
|
|
|
|
from functools import wraps
|
|
from typing import Callable, Optional
|
|
|
|
from flask import (
|
|
Response,
|
|
jsonify,
|
|
make_response,
|
|
request,
|
|
)
|
|
|
|
|
|
def get_user_id() -> Optional[str]:
|
|
"""Extract user ID from decoded JWT token, or None if unauthenticated."""
|
|
decoded_token = getattr(request, "decoded_token", None)
|
|
return decoded_token.get("sub") if decoded_token else None
|
|
|
|
|
|
def require_auth(func: Callable) -> Callable:
|
|
"""Decorator to require authentication. Returns 401 when absent."""
|
|
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
user_id = get_user_id()
|
|
if not user_id:
|
|
return make_response(jsonify({"success": False, "error": "Unauthorized"}), 401)
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
def success_response(
|
|
data=None, message: Optional[str] = None, status: int = 200
|
|
) -> Response:
|
|
"""Shape a successful JSON response."""
|
|
body = {"success": True}
|
|
if data is not None:
|
|
body["data"] = data
|
|
if message is not None:
|
|
body["message"] = message
|
|
return make_response(jsonify(body), status)
|
|
|
|
|
|
def error_response(message: str, status: int = 400, **kwargs) -> Response:
|
|
"""Shape an error JSON response; any kwargs are merged into the body."""
|
|
body = {"success": False, "error": message, **kwargs}
|
|
return make_response(jsonify(body), status)
|
|
|
|
|
|
def require_fields(required: list) -> Callable:
|
|
"""Decorator: return 400 if any listed field is missing/falsy in the JSON body."""
|
|
|
|
def decorator(func: Callable) -> Callable:
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
data = request.get_json()
|
|
if not data:
|
|
return error_response("Request body required")
|
|
missing = [field for field in required if not data.get(field)]
|
|
if missing:
|
|
return error_response(
|
|
f"Missing required fields: {', '.join(missing)}"
|
|
)
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|