Init
This commit is contained in:
10
api/Dockerfile
Normal file
10
api/Dockerfile
Normal file
@@ -0,0 +1,10 @@
|
||||
FROM python:3.9-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY server.py .
|
||||
|
||||
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
2
api/requirements.txt
Normal file
2
api/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
fastapi>=0.68.0
|
||||
uvicorn>=0.15.0
|
||||
38
api/server.py
Normal file
38
api/server.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import socket
|
||||
import asyncio
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Configure CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
async def check_port(host: str, port: int, timeout: float = 2.0) -> bool:
|
||||
try:
|
||||
# Create async socket connection
|
||||
reader, writer = await asyncio.wait_for(
|
||||
asyncio.open_connection(host, port),
|
||||
timeout=timeout
|
||||
)
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
return True
|
||||
except (socket.gaierror, ConnectionRefusedError, asyncio.TimeoutError):
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@app.get("/check-port")
|
||||
async def port_check(host: str, port: int):
|
||||
is_alive = await check_port(host, port)
|
||||
return {"status": "online" if is_alive else "offline"}
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
return {"status": "ok"}
|
||||
Reference in New Issue
Block a user