fileserver
This commit is contained in:
parent
245ded50ef
commit
bf6fffd52c
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
@ -1,24 +1,87 @@
|
|||||||
import http.server
|
from fastapi import FastAPI, HTTPException
|
||||||
|
from fastapi.responses import FileResponse, HTMLResponse
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
import uvicorn
|
||||||
import os
|
import os
|
||||||
|
|
||||||
PORT = 8050
|
PORT = 8051
|
||||||
DIRECTORY = "./data/db"
|
DIRECTORY = "./data/db"
|
||||||
|
|
||||||
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
# Ensure the data directory exists (if not already handled elsewhere)
|
||||||
def translate_path(self, path):
|
os.makedirs(DIRECTORY, exist_ok=True)
|
||||||
# Serve files from the specified directory
|
|
||||||
path = super().translate_path(path)
|
|
||||||
return os.path.join(DIRECTORY, os.path.relpath(path, os.path.dirname(path)))
|
|
||||||
|
|
||||||
def run(server_class=http.server.HTTPServer, handler_class=CustomHandler):
|
app = FastAPI()
|
||||||
server_address = ("", PORT)
|
|
||||||
httpd = server_class(server_address, handler_class)
|
# Mount static files to serve content from the database directory
|
||||||
print(f"Serving on port {PORT}... Press Ctrl-C to stop.")
|
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:
|
try:
|
||||||
while True:
|
file_size = os.path.getsize(file_path)
|
||||||
httpd.handle_request()
|
# convert to GB
|
||||||
except KeyboardInterrupt:
|
file_size = file_size / (1024 * 1024 * 1024)
|
||||||
print("\nServer stopped by user.")
|
files_html += f"""
|
||||||
|
<li>
|
||||||
|
{relative_path} ({file_size:.3f} GB)
|
||||||
|
<a href="/data/{relative_path}" download="{relative_path}">Download</a>
|
||||||
|
<button onclick="deleteFile('{relative_path}')">Delete</button>
|
||||||
|
</li>
|
||||||
|
"""
|
||||||
|
except Exception as e:
|
||||||
|
files_html += f"<li>Error accessing {relative_path}: {e}</li>"
|
||||||
|
|
||||||
|
return f"""
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Files in Directory</title>
|
||||||
|
<script src="/static/script.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Files in {DIRECTORY}</h1>
|
||||||
|
<ul>
|
||||||
|
{files_html}
|
||||||
|
</ul>
|
||||||
|
<p><a href="/">Back to Home</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
@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("""
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Market Data Server</title>
|
||||||
|
<script src="/static/script.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Market Data Server</h1>
|
||||||
|
<p>Welcome to the Market Data Server.</p>
|
||||||
|
<h2>Database Files</h2>
|
||||||
|
<p>Access database files directly via /data/your_file.db</p>
|
||||||
|
<p><a href="/files">List Files</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
run()
|
uvicorn.run(app, host="0.0.0.0", port=PORT)
|
||||||
15
static/script.js
Normal file
15
static/script.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
async function deleteFile(filename) {
|
||||||
|
if (confirm('Are you sure you want to delete ' + filename + '?')) {
|
||||||
|
const response = await fetch(`/delete_file/${filename}`, { method: 'DELETE' });
|
||||||
|
const data = await response.json();
|
||||||
|
alert(data.message);
|
||||||
|
if (response.ok) {
|
||||||
|
location.reload(); // Reload the page to update the file list
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial check when page loads
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
// No archive functions needed now
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user