File size: 1,278 Bytes
709c473 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import os
import threading
import time
from fastapi import FastAPI
from fastapi.responses import FileResponse, HTMLResponse, PlainTextResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
from run import start_finder
app = FastAPI()
app.mount('/file/db', StaticFiles(directory='db/', html=False), '/file/db')
def tail_file(path):
with open(path, "r", encoding="utf-8") as f:
f.seek(0, os.SEEK_END) # start at EOF
while True:
line = f.readline()
if line:
yield f"data: {line.strip()}\n\n"
else:
time.sleep(1)
@app.get("/raw-pair")
def send_raw_pair():
with open('public/pair.txt') as f:
return PlainTextResponse(f.read())
@app.get("/live-pair")
def live_pairs():
with open('public/live-pairs.html') as f:
return HTMLResponse(f.read())
@app.get("/stream-pair")
def stream_pairs():
return StreamingResponse(tail_file('public/pair.txt'), media_type="text/event-stream")
@app.on_event('startup')
def start():
runner = threading.Thread(target=start_finder, daemon=True)
runner.start()
if __name__ == "__main__":
uvicorn.run(app, host='0.0.0.0', port=7860) |