|
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)
|
|
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) |