This repository was archived by the owner on Dec 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (30 loc) · 1.26 KB
/
main.py
File metadata and controls
41 lines (30 loc) · 1.26 KB
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, HTTPException, Request
from modules import database_manager, ips
import os
import asyncio
app = FastAPI()
@app.get("/refreshdatabase")
async def refresh_database():
try:
await database_manager.download_database()
except Exception as e:
raise HTTPException(status_code=500, detail={"successful": False, "error": str(e)})
return {"successful": True}
@app.get("/ip")
async def ip(request: Request):
# Get the client's IP address from the request object
client_ip = request.client.host
# If your FastAPI application is behind a proxy server,
# you can get the proxy's IP address from the headers
proxy_ip = request.headers.get("X-Forwarded-For").split(",")[0] if request.headers.get("X-Forwarded-For") else None
ip = proxy_ip if proxy_ip else client_ip
ip_data = await ips.get_ip_info(ip)
if ip_data is None:
raise HTTPException(status_code=404, detail={"successful": False, "error": "Didn't find ip data."})
return {"successful": True, "ip_data": ip_data}
@app.get("/ip/{ip}")
async def ip(ip: str):
ip_data = await ips.get_ip_info(ip)
if ip_data is None:
raise HTTPException(status_code=404, detail={"successful": False, "error": "Didn't find ip data."})
return {"successful": True, "ip_data": ip_data}