mirror of
https://gitlab.com/megazordpobeda/DataRush.git
synced 2026-05-23 01:37:11 +00:00
its okay
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
|
||||
load_dotenv(BASE_DIR / ".env")
|
||||
|
||||
REGISTRY_LOGIN = os.getenv("REGISTRY_USERNAME", None)
|
||||
|
||||
REGISTRY_PASSWORD = os.getenv("REGISTRY_USERNAME", None)
|
||||
|
||||
REGISTRY_URL = os.getenv("REGISTRY_URL", "gitlab.prodcontest.ru:5050")
|
||||
|
||||
DOCKER_IMAGE = os.getenv(
|
||||
"IMAGE", default="gitlab.prodcontest.ru:5050/team-15/project/custom-python"
|
||||
)
|
||||
+21
-15
@@ -10,19 +10,25 @@ import tempfile
|
||||
import logging
|
||||
from urllib.parse import urlparse
|
||||
import re
|
||||
|
||||
app = FastAPI()
|
||||
docker_client = docker.from_env()
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
import config
|
||||
|
||||
|
||||
DOCKER_IMAGE = "gitlab.python:3-slim"
|
||||
CONTAINER_TIMEOUT = 60
|
||||
MAX_FILE_SIZE = 4 * 1024 * 1024
|
||||
ALLOWED_FILENAME_CHARS = r"[^a-zA-Z0-9_\-.]"
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
docker_client = docker.from_env()
|
||||
logger = logging.getLoggerQ(__name__)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
docker_client.login(
|
||||
username=config.REGISTRY_LOGIN,
|
||||
password=config.REGISTRY_PASSWORD,
|
||||
registry=config.REGISTRY_URL,
|
||||
)
|
||||
|
||||
|
||||
class FileDetails(BaseModel):
|
||||
url: HttpUrl = Field(
|
||||
..., description="URL to download the file from (supports HTTP/HTTPS)"
|
||||
@@ -130,7 +136,7 @@ def run_container_safely(
|
||||
volumes[host_path] = {"bind": container_path, "mode": "ro"}
|
||||
|
||||
container = docker_client.containers.run(
|
||||
image=DOCKER_IMAGE,
|
||||
image=config.DOCKER_IMAGE,
|
||||
command=command,
|
||||
volumes=volumes,
|
||||
working_dir="/execution",
|
||||
@@ -166,6 +172,14 @@ def run_container_safely(
|
||||
pass
|
||||
|
||||
|
||||
def validate_file_path(path: str) -> bool:
|
||||
return (
|
||||
not os.path.isabs(path)
|
||||
and os.path.basename(path) == path
|
||||
and all(c.isalnum() or c in {"_", "-", "."} for c in path)
|
||||
)
|
||||
|
||||
|
||||
@app.post("/execute", response_model=ExecutionResponse)
|
||||
async def execute_code(request: ExecutionRequest) -> ExecutionResponse:
|
||||
try:
|
||||
@@ -279,11 +293,3 @@ async def health_check() -> HealthCheckResponse:
|
||||
return HealthCheckResponse(status="healthy", docker="connected")
|
||||
except docker.errors.DockerException:
|
||||
return HealthCheckResponse(status="degraded", docker="unavailable")
|
||||
|
||||
|
||||
def validate_file_path(path: str) -> bool:
|
||||
return (
|
||||
not os.path.isabs(path)
|
||||
and os.path.basename(path) == path
|
||||
and all(c.isalnum() or c in {"_", "-", "."} for c in path)
|
||||
)
|
||||
|
||||
@@ -7,6 +7,7 @@ dependencies = [
|
||||
"aiohttp>=3.11.13",
|
||||
"docker>=7.1.0",
|
||||
"fastapi>=0.115.11",
|
||||
"python-dotenv>=1.0.1",
|
||||
"python-multipart>=0.0.20",
|
||||
"regex>=2024.11.6",
|
||||
"uvicorn>=0.34.0",
|
||||
|
||||
Reference in New Issue
Block a user