from fastapi import FastAPI, HTTPException from fastapi.responses import FileResponse, HTMLResponse from fastapi.staticfiles import StaticFiles import uvicorn import os PORT = 8051 DIRECTORY = "./data/db" # Ensure the data directory exists (if not already handled elsewhere) os.makedirs(DIRECTORY, exist_ok=True) app = FastAPI() # Mount static files to serve content from the database directory app.mount("/data", StaticFiles(directory=DIRECTORY), name="data") app.mount("/static", StaticFiles(directory="static"), name="static") @app.get("/files", response_class=HTMLResponse) async def list_files(): files_html = "" for root, _, files in os.walk(DIRECTORY): for file in files: file_path = os.path.join(root, file) relative_path = os.path.relpath(file_path, DIRECTORY) try: file_size = os.path.getsize(file_path) # convert to GB file_size = file_size / (1024 * 1024 * 1024) files_html += f"""
  • {relative_path} ({file_size:.3f} GB) Download
  • """ except Exception as e: files_html += f"
  • Error accessing {relative_path}: {e}
  • " return f""" Files in Directory

    Files in {DIRECTORY}

    Back to Home

    """ @app.delete("/delete_file/{filename}") async def delete_single_file(filename: str): file_path = os.path.join(DIRECTORY, filename) if os.path.exists(file_path): try: os.remove(file_path) return {"message": f"File {filename} deleted successfully.", "status": "deleted"} except Exception as e: raise HTTPException(status_code=500, detail=f"Error deleting file {filename}: {e}") else: raise HTTPException(status_code=404, detail=f"File {filename} not found.") @app.get("/") async def root(): return HTMLResponse(""" Market Data Server

    Market Data Server

    Welcome to the Market Data Server.

    Database Files

    Access database files directly via /data/your_file.db

    List Files

    """) if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=PORT)