From 9bb609c165fd81fd2d6b7168281fb70cef89af13 Mon Sep 17 00:00:00 2001 From: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com> Date: Fri, 8 May 2026 09:23:52 +0000 Subject: [PATCH] chore: update --- .vscode/settings.json | 3 +- cart-service/pom.xml | 10 --- .../.mvn/wrapper/maven-wrapper.properties | 4 + file-service/pom.xml | 80 +++++++++++++++++++ file-service/python/Dockerfile | 32 ++++++++ file-service/python/main.py | 35 ++++++++ file-service/python/requirements.txt | 3 + k8s/base/file.yaml | 38 +++++++++ k8s/base/kustomization.yaml | 1 + pom.xml | 1 + 10 files changed, 196 insertions(+), 11 deletions(-) create mode 100644 file-service/.mvn/wrapper/maven-wrapper.properties create mode 100644 file-service/pom.xml create mode 100644 file-service/python/Dockerfile create mode 100644 file-service/python/main.py create mode 100644 file-service/python/requirements.txt create mode 100644 k8s/base/file.yaml diff --git a/.vscode/settings.json b/.vscode/settings.json index 3e30470..efcd922 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,7 +8,8 @@ "**/target": true, "**/docker": true, "node_modules": true, - "**/.mvn": true + "**/.mvn": true, + "**/venv": true }, "explorerExclude.backup": {}, "java.compile.nullAnalysis.mode": "automatic", diff --git a/cart-service/pom.xml b/cart-service/pom.xml index febe854..d659d3f 100644 --- a/cart-service/pom.xml +++ b/cart-service/pom.xml @@ -111,16 +111,6 @@ quarkus-maven-plugin ${quarkus.platform.version} true - - - - build - generate-code - generate-code-tests - native-image-agent - - - maven-compiler-plugin diff --git a/file-service/.mvn/wrapper/maven-wrapper.properties b/file-service/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..ce33c1c --- /dev/null +++ b/file-service/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,4 @@ +wrapperVersion=3.3.4 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.15/apache-maven-3.9.15-bin.zip +distributionSha256Sum=b0d9292f06c5faded31ddcb6cb69099f316d6fea40a7778624b259bad9fed18a diff --git a/file-service/pom.xml b/file-service/pom.xml new file mode 100644 index 0000000..d7e8db9 --- /dev/null +++ b/file-service/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + + + com.drinkool + drinkool + 1.5.24 + ../pom.xml + + + file-service + + + vps.demonkernel.io.vn + foodsurf + file-service + + + ${quarkus.container-image.registry}/${quarkus.container-image.group}/${quarkus.container-image.name} + + + + + io.quarkus + quarkus-container-image-docker + + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + docker-build-python + package + + exec + + + docker + ${project.basedir}/python + + build + -t + ${python.image.full.name}:${parent.version} + -t + ${python.image.full.name}:latest + . + + + + + + docker-push-python + install + + exec + + + docker + + push + ${python.image.full.name} + + + + + + + + + \ No newline at end of file diff --git a/file-service/python/Dockerfile b/file-service/python/Dockerfile new file mode 100644 index 0000000..4b4333e --- /dev/null +++ b/file-service/python/Dockerfile @@ -0,0 +1,32 @@ +# --- Stage 1: Build (Cài đặt thư viện) --- +FROM python:3.9-slim AS builder + +WORKDIR /build + +# Khởi tạo môi trường ảo để tách biệt thư viện +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" + +COPY requirements.txt . +RUN pip install --upgrade --no-cache-dir -r requirements.txt + + +# --- Stage 2: Runtime (Chỉ chạy Prod) --- +FROM python:3.9-slim + +WORKDIR /app + +# Chỉ copy môi trường ảo đã build xong từ Stage 1 +COPY --from=builder /opt/venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" + +# Copy mã nguồn +COPY main.py . + +# Tạo folder lưu ảnh và phân quyền (tăng tính bảo mật) +RUN mkdir images && chmod 777 images + +# Chạy bằng Uvicorn với chế độ production (tắt reload) +# Port 80 theo yêu cầu trước đó của bạn +EXPOSE 80 +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80", "--workers", "4"] \ No newline at end of file diff --git a/file-service/python/main.py b/file-service/python/main.py new file mode 100644 index 0000000..8aa0d9a --- /dev/null +++ b/file-service/python/main.py @@ -0,0 +1,35 @@ +import os +from fastapi import FastAPI, File, UploadFile, HTTPException +from fastapi.responses import FileResponse +from pathlib import Path + +app = FastAPI() + +# Thư mục lưu trữ ảnh (nên được mount volume khi chạy docker) +STORAGE_DIR = Path("images") +STORAGE_DIR.mkdir(exist_ok=True) + +@app.post("/") +async def upload_image(file: UploadFile = File(...)): + # Chỉ chấp nhận định dạng ảnh + if not file.content_type.startswith("image/"): + raise HTTPException(status_code=400, detail="File không hợp lệ. Chỉ chấp nhận ảnh.") + + file_path = STORAGE_DIR / file.filename + + # Ghi file theo block để tối ưu bộ nhớ + with open(file_path, "wb") as f: + content = await file.read() + f.write(content) + + return {"status": "success", "filename": file.filename} + +@app.get("/{name}") +async def get_image(name: str): + file_path = STORAGE_DIR / name + + if not file_path.exists() or not file_path.is_file(): + raise HTTPException(status_code=404, detail="Không tìm thấy ảnh") + + # Trả về ảnh trực tiếp cho trình duyệt/client + return FileResponse(file_path) \ No newline at end of file diff --git a/file-service/python/requirements.txt b/file-service/python/requirements.txt new file mode 100644 index 0000000..6d7503c --- /dev/null +++ b/file-service/python/requirements.txt @@ -0,0 +1,3 @@ +fastapi +uvicorn +python-multipart \ No newline at end of file diff --git a/k8s/base/file.yaml b/k8s/base/file.yaml new file mode 100644 index 0000000..844e419 --- /dev/null +++ b/k8s/base/file.yaml @@ -0,0 +1,38 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: file-deploy +spec: + replicas: 1 + selector: + matchLabels: + app: file + template: + metadata: + labels: + app: file + spec: + containers: + - name: file-pod + image: git.demonkernel.io.vn/foodsurf/file-service:1.5.24 + imagePullPolicy: IfNotPresent + ports: + - containerPort: 8080 + resources: + requests: + memory: "32Mi" + cpu: "50m" + limits: + memory: "64Mi" + cpu: "250m" +--- +apiVersion: v1 +kind: Service +metadata: + name: file-service +spec: + selector: + app: file + ports: + - port: 80 + targetPort: 80 diff --git a/k8s/base/kustomization.yaml b/k8s/base/kustomization.yaml index 3fb2e8a..678d10b 100644 --- a/k8s/base/kustomization.yaml +++ b/k8s/base/kustomization.yaml @@ -2,6 +2,7 @@ resources: - account.yaml - eatery.yaml - cart.yaml + - file.yaml - gateway.yaml - redis.yaml - kafdrop.yaml diff --git a/pom.xml b/pom.xml index ec9ebe2..e109e52 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ account-service gateway-service cart-service + file-service