-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (40 loc) · 1.68 KB
/
main.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
import subprocess
import json
from fastapi import (FastAPI, status,
Depends, Request)
from starlette.concurrency import iterate_in_threadpool
import uvicorn
from pydantic import BaseModel
from api.auth import verify_access
from scheduler.scheduler import Scheduler
from scheduler.celery_worker import unblock_proxy
from scheduler.unblock_all_proxies import unblocking_proxy_subprocess
app = FastAPI()
class Report(BaseModel):
proxy_id: int
status: str
error: str
@app.get("/get_proxy", status_code=status.HTTP_200_OK)
async def get_proxy(source_id: int, password: str = Depends(verify_access)):
# request scheduler to get proxy for source
scheduler = Scheduler()
return scheduler.get_proxy(source_id)[0][0]
@app.post("/send_report", status_code=status.HTTP_200_OK)
async def send_report(report: Report , password: str = Depends(verify_access)):
# send report to scheduler
report = await report
scheduler = Scheduler()
return {"message": "Report was recieved successfully."}
@app.middleware("http")
async def add_custom_header(request: Request, call_next):
response = await call_next(request)
if "/get_proxy" == request.url.path and response.status_code == 200:
# print the body of response
response_body = [chunk async for chunk in response.body_iterator][0].decode()
unblock_proxy.apply_async(args=[json.loads(response_body)])
response.body_iterator = iterate_in_threadpool(iter(response_body))
return response
if __name__ == "__main__":
# start redis sudo service redis-server start
unblocking_proxy_subprocess.start()
uvicorn.run(app, host="127.0.0.1", port=8000)