mohsin-devs's picture
Deploy HF-ready DocVault with HF storage backend
2fe2727
"""DocVault Flask application."""
import os
from flask import Flask, jsonify, send_file
from flask_cors import CORS
from server.config import DEBUG, HF_REPO_ID, MAX_CONTENT_LENGTH, SECRET_KEY
from server.routes.api import api_bp
from server.utils.logger import setup_logger
logger = setup_logger(__name__)
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
def create_app() -> Flask:
app = Flask(__name__, static_folder=None)
app.config["SECRET_KEY"] = SECRET_KEY
app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH
app.config["JSON_SORT_KEYS"] = False
CORS(app, resources={r"/api/*": {"origins": "*"}})
app.register_blueprint(api_bp)
@app.after_request
def add_cache_headers(response):
if response.content_type and (
"text/html" in response.content_type
or "text/javascript" in response.content_type
or "application/javascript" in response.content_type
or "text/css" in response.content_type
):
response.cache_control.max_age = 0
response.cache_control.no_cache = True
response.cache_control.no_store = True
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
@app.errorhandler(413)
def request_entity_too_large(error):
return jsonify(
{
"success": False,
"error": f"File too large. Maximum size: {MAX_CONTENT_LENGTH / (1024 * 1024):.0f}MB",
}
), 413
@app.route("/<path:filename>")
def serve_static(filename: str):
filepath = os.path.join(ROOT_DIR, filename)
if os.path.exists(filepath) and os.path.isfile(filepath):
return send_file(filepath)
return jsonify({"error": "Not found"}), 404
@app.route("/", methods=["GET"])
def index():
index_path = os.path.join(ROOT_DIR, "index.html")
if os.path.exists(index_path):
return send_file(index_path)
return jsonify(
{
"name": "DocVault",
"version": "2.0.0",
"description": "Hugging Face Hub-backed document storage",
"storage": "HF",
"repo": HF_REPO_ID,
"endpoints": {
"GET /api/health": "Health check",
"POST /api/create-folder": "Create folder",
"POST /api/upload": "Upload file",
"POST /api/delete": "Delete file or folder",
"POST /api/rename": "Rename file or folder",
"GET /api/list": "List contents",
"GET /api/download/<file_path>": "Download file",
},
}
), 200
logger.info("DocVault application initialized")
return app
if __name__ == "__main__":
app = create_app()
port = int(os.environ.get("PORT", 7860))
logger.info("Starting DocVault on http://0.0.0.0:%s", port)
app.run(debug=DEBUG, host="0.0.0.0", port=port)