File size: 1,203 Bytes
9726e3d
 
 
f083b69
 
 
9726e3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Request
import requests
import time

app = FastAPI()

def callback_sender(sender_url: str):
    """Callback function to periodically call registered senders"""
    response_ok = True
    try:
        response = requests.get(sender_url, params={'callback': 'true', 'timestamp': time.time()})
        print(f"Callback to {sender_url}: {response.status_code}")
    except Exception as e:
        print(f"Callback to {sender_url} failed: {e}")
        response_ok = False
        
    return response_ok

@app.get("/hc")
async def receive_hc(request: Request, timestamp: float, host_url: str):
    response = False
    # calling the sender back
    if host_url:
        print(f"Calling back: {host_url}")
        response = callback_sender(host_url)
    
    # Print received data
    status = 'OK' if response else 'KO'
    url = str(request.url)
    original_url = url.split('?')[0]
    print(f"URL: {original_url}")
    print(f"Sender Host URL: {host_url}")
    print(f"Timestamp: {timestamp}")
    print(f"status: {status}")
    print("-" * 40)
    
    return {"status": status}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)