Bladeren bron

changing paths and adapting the overlay manager

Nicole Portas 1 maand geleden
bovenliggende
commit
0d8a238ee1
3 gewijzigde bestanden met toevoegingen van 32 en 16 verwijderingen
  1. 27 12
      overlay_manager/main.py
  2. BIN
      overlay_manager/static/logo.png
  3. 5 4
      overlay_manager/templates/index.html

+ 27 - 12
overlay_manager/main.py

@@ -6,17 +6,24 @@ from fastapi.templating import Jinja2Templates
 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")
+
 templates = Jinja2Templates(directory="templates")
 
+# The persistent directory for your custom code
 OVERLAY_DIR = "/srv"
 
 @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)
     with open(file_location, "wb+") as 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")
 async def delete_file(filepath: str = Form(...)):
@@ -49,7 +64,7 @@ async def delete_file(filepath: str = Form(...)):
         target_dir = os.path.dirname(target)
         if not os.listdir(target_dir) and target_dir != OVERLAY_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)
 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):
         with open(target, "w", encoding="utf-8") as f:
             f.write(content)
-    return RedirectResponse(url="/", status_code=303)
+    return RedirectResponse(url="/patch-manager/", status_code=303)

BIN
overlay_manager/static/logo.png


+ 5 - 4
overlay_manager/templates/index.html

@@ -13,7 +13,7 @@
 <body class="bg-light">
     <nav class="navbar navbar-dark shadow-sm py-3" style="background-color: #0B61A4;">
         <div class="container d-flex align-items-center">
-            <img src="/static/logo.png" alt="ArduPilot" height="40" class="me-3 bg-white p-1 rounded">
+            <img src="/patch-manager/static/logo.png" alt="ArduPilot Logo" class="me-3 bg-white p-2 rounded shadow-sm">
             <span class="navbar-brand mb-0 h3 fw-bold">Overlay Manager</span>
         </div>
     </nav>
@@ -22,7 +22,8 @@
         <div class="card shadow border-0 rounded-3 mb-4">
             <div class="card-body p-4">
                 <h5 class="card-title fw-bold text-secondary mb-3"><i class="bi bi-cloud-arrow-up-fill me-2 text-primary"></i>Inject Custom Source</h5>
-                <form action="/upload" method="post" enctype="multipart/form-data" class="row g-3">
+                
+                <form action="/patch-manager/upload" method="post" enctype="multipart/form-data" class="row g-3">
                     <div class="col-md-5"><input class="form-control border-primary" type="file" name="file" required></div>
                     <div class="col-md-5"><input type="text" class="form-control border-primary" name="target_path" placeholder="Path (e.g., libraries/AP_HAL)"></div>
                     <div class="col-md-2"><button type="submit" class="btn btn-custom w-100 fw-bold">Upload</button></div>
@@ -39,8 +40,8 @@
                         <tr>
                             <td class="ps-4 font-monospace">{{ file }}</td>
                             <td class="text-end pe-4">
-                                <a href="/edit?filepath={{ file }}" class="btn btn-sm btn-outline-primary me-2"><i class="bi bi-pencil"></i> Edit</a>
-                                <form action="/delete" method="post" class="d-inline" onsubmit="return confirm('Delete this file?');">
+                                <a href="/patch-manager/edit?filepath={{ file }}" class="btn btn-sm btn-outline-primary me-2"><i class="bi bi-pencil"></i> Edit</a>
+                                <form action="/patch-manager/delete" method="post" class="d-inline" onsubmit="return confirm('Delete this file?');">
                                     <input type="hidden" name="filepath" value="{{ file }}">
                                     <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i> Delete</button>
                                 </form>