From 5faebd423fc505564f02cc960a9a6d2a6a3cd8bf Mon Sep 17 00:00:00 2001 From: Ittipat Lusuk Date: Mon, 15 Jun 2026 17:24:49 +0700 Subject: [PATCH] [image-service] Add middleware prefix --- main.py | 22 ++++++++++++++++++++-- nginx.conf | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 888a9bd..6dd9a87 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, UploadFile, File, HTTPException, Query +from fastapi import FastAPI, UploadFile, File, HTTPException, Query, Request from fastapi.responses import FileResponse from pathlib import Path from typing import Optional @@ -39,6 +39,23 @@ ALLOWED_FOLDERS = {"page_drink", ALLOWED_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp"} +@app.middleware("http") +async def strip_prefix_middleware(request: Request, call_next): + path = request.scope["path"] + + if path.startswith(PREFIX): + new_path = path[len(PREFIX):] + + if not new_path: + new_path = "/" + + request.scope["path"] = new_path + + print(f"[Middleware] {path} -> {new_path}") + + response = await call_next(request) + return response + def validate_folder(folder: str): if folder not in ALLOWED_FOLDERS: raise HTTPException(400, f"folder must be {ALLOWED_FOLDERS}") @@ -308,7 +325,8 @@ async def commit_files_to_git( commit_data = { "signature_username": display_name, "signature_email": email, - "message": f"commit {folder} {country}" + "message": f"commit {folder} {country}", + "ref": SERVICE_NAME } multipart_files = {} diff --git a/nginx.conf b/nginx.conf index 2ff2da8..a61524c 100644 --- a/nginx.conf +++ b/nginx.conf @@ -2,13 +2,13 @@ server { listen 80; # /image/{folder}/... - location ~ ^/image/(page_drink|page_drink_disable|page_drink_disable_n|page_drink_disable_n2|page_drink_n|page_drink_picture2_n|page_drink_press|page_drink_press_n|page_drink_select)/(.*)$ { + location ~ ^/nginx-image/image/(page_drink|page_drink_disable|page_drink_disable_n|page_drink_disable_n2|page_drink_n|page_drink_picture2_n|page_drink_press|page_drink_press_n|page_drink_select)/(.*)$ { alias /usr/src/app/taobin_project/image/$1/$2; # alias /taobin_project/image/$1/$2; } # /inter/{country}/image/{folder}/... - location ~ ^/inter/([^/]+)/image/(page_drink|page_drink_disable|page_drink_disable_n|page_drink_disable_n2|page_drink_n|page_drink_picture2_n|page_drink_press|page_drink_press_n|page_drink_select)/(.*)$ { + location ~ ^/nginx-image/inter/([^/]+)/image/(page_drink|page_drink_disable|page_drink_disable_n|page_drink_disable_n2|page_drink_n|page_drink_picture2_n|page_drink_press|page_drink_press_n|page_drink_select)/(.*)$ { alias /usr/src/app/taobin_project/inter/$1/image/$2/$3; # alias /taobin_project/inter/$1/image/$2/$3; }