Fix routing order issue.
This commit is contained in:
parent
7875030298
commit
933592a2e2
59
main.py
59
main.py
@ -122,6 +122,35 @@ def initialize_server(args: argparse.Namespace):
|
|||||||
print(f"Indexed {len(file_mapping)} files from {len(indexers)} source(s)")
|
print(f"Indexed {len(file_mapping)} files from {len(indexers)} source(s)")
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/health")
|
||||||
|
async def health_check():
|
||||||
|
return {"status": "healthy", "file_count": len(file_mapping)}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/{file_hash}/data")
|
||||||
|
async def get_file_data(file_hash: str):
|
||||||
|
"""Serve a specific file by its hash"""
|
||||||
|
if file_hash not in file_mapping:
|
||||||
|
raise HTTPException(status_code=404, detail="File not found")
|
||||||
|
|
||||||
|
indexer = _find_indexer_for_hash(file_hash)
|
||||||
|
if not indexer:
|
||||||
|
raise HTTPException(status_code=404, detail="File not found")
|
||||||
|
|
||||||
|
filename = indexer.get_filename_by_hash(file_hash)
|
||||||
|
content_type, _ = mimetypes.guess_type(filename or "")
|
||||||
|
if not content_type:
|
||||||
|
content_type = "application/octet-stream"
|
||||||
|
|
||||||
|
return StreamingResponse(
|
||||||
|
indexer.get_file_by_hash(file_hash),
|
||||||
|
media_type=content_type,
|
||||||
|
headers={
|
||||||
|
"Content-Disposition": f"inline; filename={os.path.basename(filename or '')}",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root():
|
async def root():
|
||||||
"""Redirect to a random file hash"""
|
"""Redirect to a random file hash"""
|
||||||
@ -217,36 +246,6 @@ def _get_random_hash() -> str:
|
|||||||
return random.choice(keys)
|
return random.choice(keys)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/{file_hash}/data")
|
|
||||||
async def get_file_data(file_hash: str):
|
|
||||||
"""Serve a specific file by its hash"""
|
|
||||||
if file_hash not in file_mapping:
|
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
|
||||||
|
|
||||||
indexer = _find_indexer_for_hash(file_hash)
|
|
||||||
if not indexer:
|
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
|
||||||
|
|
||||||
filename = indexer.get_filename_by_hash(file_hash)
|
|
||||||
content_type, _ = mimetypes.guess_type(filename or "")
|
|
||||||
if not content_type:
|
|
||||||
content_type = "application/octet-stream"
|
|
||||||
|
|
||||||
return StreamingResponse(
|
|
||||||
indexer.get_file_by_hash(file_hash),
|
|
||||||
media_type=content_type,
|
|
||||||
headers={
|
|
||||||
"Content-Disposition": f"inline; filename={os.path.basename(filename or '')}",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# Optional: Add a health check endpoint
|
|
||||||
@app.get("/api/health")
|
|
||||||
async def health_check():
|
|
||||||
return {"status": "healthy", "file_count": len(file_mapping)}
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="Run the file server")
|
parser = argparse.ArgumentParser(description="Run the file server")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user