From 22813bab656bff3b455afd07ebe09e69c6f9451e Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Tue, 25 Nov 2025 10:52:21 +0100 Subject: [PATCH 1/2] feat: add API prefix for prefixes in url --- .gitea/workflows/release.yml | 14 +++++++++++++- .gitea/workflows/test_pr.yml | 1 + api_service.py | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index abd68bc..fa24455 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -75,6 +75,18 @@ jobs: run: | git config user.name "Gitea CI" git config user.email "ci@git.theprivateserver.de" + + - name: Determine branch to push to + id: push_branch + run: | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then + # workflow_dispatch runs on a branch ref like refs/heads/main + BRANCH=${GITHUB_REF#refs/heads/} + else + # for a merged PR use the PR base ref (target branch) + BRANCH=${{ github.event.pull_request.base.ref }} + fi + echo "PUSH_BRANCH=$BRANCH" >> $GITHUB_ENV - name: Bump version id: bump run: | @@ -133,7 +145,7 @@ jobs: uses: ad-m/github-push-action@master with: github_token: ${{ secrets.GITHUB_TOKEN }} - branch: ${{ github.ref }} + branch: ${{ env.PUSH_BRANCH }} - name: Build Changelog id: build_changelog uses: https://github.com/mikepenz/release-changelog-builder-action@v5 diff --git a/.gitea/workflows/test_pr.yml b/.gitea/workflows/test_pr.yml index 3d64b8c..fb0841b 100644 --- a/.gitea/workflows/test_pr.yml +++ b/.gitea/workflows/test_pr.yml @@ -38,6 +38,7 @@ jobs: with: name: semapform-image path: semapform-api.tar + retention-days: 1 smoke-tests: needs: build-image diff --git a/api_service.py b/api_service.py index 6c2662f..6142951 100644 --- a/api_service.py +++ b/api_service.py @@ -10,8 +10,31 @@ from fastapi import FastAPI, Query from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse + + app = FastAPI(title="Signature Validation API") +# Optional path prefix support: when behind a reverse-proxy that uses a +# URL prefix (eg. `https://api.example.tld/library/...`) set `API_PREFIX` to +# that prefix (example: `/library`) so incoming requests are rewritten to the +# application root. This keeps route definitions unchanged while supporting +# both proxied and direct deployments. +_api_prefix_raw = os.getenv("API_PREFIX", "").strip() +api_prefix = "" +if _api_prefix_raw: + if not _api_prefix_raw.startswith("/"): + _api_prefix_raw = "/" + _api_prefix_raw + api_prefix = _api_prefix_raw.rstrip("/") + + +@app.middleware("http") +async def _strip_api_prefix(request, call_next): + if api_prefix and request.url.path.startswith(api_prefix): + new_path = request.url.path[len(api_prefix) :] + request.scope["path"] = new_path or "/" + request.scope["root_path"] = api_prefix + return await call_next(request) + # Allow PHP application to call this API app.add_middleware( CORSMiddleware, From e0988cf23bf6ed0423f8e69a14c246dee20e98e3 Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Tue, 25 Nov 2025 10:52:59 +0100 Subject: [PATCH 2/2] lint --- api_service.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api_service.py b/api_service.py index 6142951..319ab83 100644 --- a/api_service.py +++ b/api_service.py @@ -10,8 +10,6 @@ from fastapi import FastAPI, Query from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse - - app = FastAPI(title="Signature Validation API") # Optional path prefix support: when behind a reverse-proxy that uses a @@ -35,6 +33,7 @@ async def _strip_api_prefix(request, call_next): request.scope["root_path"] = api_prefix return await call_next(request) + # Allow PHP application to call this API app.add_middleware( CORSMiddleware,