[image-service] Add middleware prefix
This commit is contained in:
parent
cb6261472a
commit
5faebd423f
2 changed files with 22 additions and 4 deletions
22
main.py
22
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 fastapi.responses import FileResponse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
@ -39,6 +39,23 @@ ALLOWED_FOLDERS = {"page_drink",
|
||||||
|
|
||||||
ALLOWED_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp"}
|
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):
|
def validate_folder(folder: str):
|
||||||
if folder not in ALLOWED_FOLDERS:
|
if folder not in ALLOWED_FOLDERS:
|
||||||
raise HTTPException(400, f"folder must be {ALLOWED_FOLDERS}")
|
raise HTTPException(400, f"folder must be {ALLOWED_FOLDERS}")
|
||||||
|
|
@ -308,7 +325,8 @@ async def commit_files_to_git(
|
||||||
commit_data = {
|
commit_data = {
|
||||||
"signature_username": display_name,
|
"signature_username": display_name,
|
||||||
"signature_email": email,
|
"signature_email": email,
|
||||||
"message": f"commit {folder} {country}"
|
"message": f"commit {folder} {country}",
|
||||||
|
"ref": SERVICE_NAME
|
||||||
}
|
}
|
||||||
|
|
||||||
multipart_files = {}
|
multipart_files = {}
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@ server {
|
||||||
listen 80;
|
listen 80;
|
||||||
|
|
||||||
# /image/{folder}/...
|
# /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 /usr/src/app/taobin_project/image/$1/$2;
|
||||||
# alias /taobin_project/image/$1/$2;
|
# alias /taobin_project/image/$1/$2;
|
||||||
}
|
}
|
||||||
|
|
||||||
# /inter/{country}/image/{folder}/...
|
# /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 /usr/src/app/taobin_project/inter/$1/image/$2/$3;
|
||||||
# alias /taobin_project/inter/$1/image/$2/$3;
|
# alias /taobin_project/inter/$1/image/$2/$3;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue