|
@@ -6,17 +6,24 @@ from fastapi.templating import Jinja2Templates
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
# =====================================================================
|
|
# =====================================================================
|
|
|
-# MODIFICATION: ROOT PATH FOR NGINX
|
|
|
|
|
-# Why: When Nginx routes traffic from yourdomain.com/patch-manager/
|
|
|
|
|
-# to this sidecar, FastAPI needs to know its "base" URL is no longer "/".
|
|
|
|
|
-# If we don't set this, the app will try to load CSS and submit forms
|
|
|
|
|
-# to the wrong root URL, resulting in 404 errors.
|
|
|
|
|
|
|
+# MODIFICATION START: REMOVED ROOT_PATH
|
|
|
|
|
+# Reason: We removed the `root_path="/patch-manager"` argument.
|
|
|
|
|
+# Because Nginx already strips the prefix before passing traffic to
|
|
|
|
|
+# this container, setting it here was causing FastAPI to double-process
|
|
|
|
|
+# the route and throw internal 404s. Standard FastAPI initialization is best.
|
|
|
|
|
+# =====================================================================
|
|
|
|
|
+app = FastAPI()
|
|
|
|
|
+# =====================================================================
|
|
|
|
|
+# MODIFICATION END
|
|
|
# =====================================================================
|
|
# =====================================================================
|
|
|
-app = FastAPI(root_path="/patch-manager")
|
|
|
|
|
|
|
|
|
|
|
|
+# Failsafe: Create the directory inside the container if it somehow gets missed
|
|
|
|
|
+os.makedirs("static", exist_ok=True)
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
+
|
|
|
templates = Jinja2Templates(directory="templates")
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
|
|
|
|
|
+# The persistent directory for your custom code
|
|
|
OVERLAY_DIR = "/srv"
|
|
OVERLAY_DIR = "/srv"
|
|
|
|
|
|
|
|
@app.get("/", response_class=HTMLResponse)
|
|
@app.get("/", response_class=HTMLResponse)
|
|
@@ -36,10 +43,18 @@ async def upload_file(file: UploadFile = File(...), target_path: str = Form(""))
|
|
|
file_location = os.path.join(save_dir, file.filename)
|
|
file_location = os.path.join(save_dir, file.filename)
|
|
|
with open(file_location, "wb+") as file_object:
|
|
with open(file_location, "wb+") as file_object:
|
|
|
shutil.copyfileobj(file.file, file_object)
|
|
shutil.copyfileobj(file.file, file_object)
|
|
|
-
|
|
|
|
|
- # Notice we redirect to "/" here. Because we set root_path="/patch-manager",
|
|
|
|
|
- # FastAPI automatically translates this redirect to "/patch-manager/"!
|
|
|
|
|
- return RedirectResponse(url="/", status_code=303)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ # =====================================================================
|
|
|
|
|
+ # MODIFICATION START: EXPLICIT NGINX REDIRECTS
|
|
|
|
|
+ # Reason: Since we removed root_path, we must explicitly tell the
|
|
|
|
|
+ # user's browser to redirect back to the Nginx URL ("/patch-manager/")
|
|
|
|
|
+ # after a successful action, otherwise it redirects to the internal "/"
|
|
|
|
|
+ # and gets lost on the main ArduPilot page.
|
|
|
|
|
+ # =====================================================================
|
|
|
|
|
+ return RedirectResponse(url="/patch-manager/", status_code=303)
|
|
|
|
|
+ # =====================================================================
|
|
|
|
|
+ # MODIFICATION END
|
|
|
|
|
+ # =====================================================================
|
|
|
|
|
|
|
|
@app.post("/delete")
|
|
@app.post("/delete")
|
|
|
async def delete_file(filepath: str = Form(...)):
|
|
async def delete_file(filepath: str = Form(...)):
|
|
@@ -49,7 +64,7 @@ async def delete_file(filepath: str = Form(...)):
|
|
|
target_dir = os.path.dirname(target)
|
|
target_dir = os.path.dirname(target)
|
|
|
if not os.listdir(target_dir) and target_dir != OVERLAY_DIR:
|
|
if not os.listdir(target_dir) and target_dir != OVERLAY_DIR:
|
|
|
os.rmdir(target_dir)
|
|
os.rmdir(target_dir)
|
|
|
- return RedirectResponse(url="/", status_code=303)
|
|
|
|
|
|
|
+ return RedirectResponse(url="/patch-manager/", status_code=303)
|
|
|
|
|
|
|
|
@app.get("/edit", response_class=HTMLResponse)
|
|
@app.get("/edit", response_class=HTMLResponse)
|
|
|
async def edit_file(request: Request, filepath: str):
|
|
async def edit_file(request: Request, filepath: str):
|
|
@@ -66,4 +81,4 @@ async def save_file(filepath: str = Form(...), content: str = Form(...)):
|
|
|
if os.path.exists(target):
|
|
if os.path.exists(target):
|
|
|
with open(target, "w", encoding="utf-8") as f:
|
|
with open(target, "w", encoding="utf-8") as f:
|
|
|
f.write(content)
|
|
f.write(content)
|
|
|
- return RedirectResponse(url="/", status_code=303)
|
|
|
|
|
|
|
+ return RedirectResponse(url="/patch-manager/", status_code=303)
|