33 Commits

Author SHA1 Message Date
Gitea CI
d0dafec4cb Bump version: 0.1.2 → 0.1.3 2025-11-25 10:06:03 +00:00
075041f371 Update pyproject.toml 2025-11-25 10:00:51 +00:00
981d5211b6 Merge pull request 'Add new env variable to use prefixes in url [release-patch]' (#8) from dev into main
Reviewed-on: #8
2025-11-25 09:57:16 +00:00
e0988cf23b lint
Some checks failed
PR tests / build-image (pull_request) Successful in 2m44s
PR tests / smoke-tests (pull_request) Successful in 34s
/ build (pull_request) Failing after 42s
2025-11-25 10:52:59 +01:00
22813bab65 feat: add API prefix for prefixes in url 2025-11-25 10:52:21 +01:00
d5cf0a8636 Merge pull request 'dev' (#7) from dev into main
Reviewed-on: #7
2025-11-25 09:51:05 +00:00
d6aeabc0a9 new download image
All checks were successful
PR tests / build-image (pull_request) Successful in 1m15s
PR tests / smoke-tests (pull_request) Successful in 37s
/ build (pull_request) Has been skipped
2025-11-25 10:48:41 +01:00
5456f8e740 new upload artifact image
Some checks failed
PR tests / build-image (pull_request) Successful in 1m15s
PR tests / smoke-tests (pull_request) Failing after 31s
2025-11-25 10:46:21 +01:00
26ebe3b40e increase version of upload-artifact
Some checks failed
PR tests / build-image (pull_request) Failing after 1m27s
PR tests / smoke-tests (pull_request) Has been skipped
2025-11-25 10:41:24 +01:00
25bc94dfa5 test: split workflow into parts
Some checks failed
PR tests / build-image (pull_request) Failing after 2m19s
PR tests / smoke-tests (pull_request) Has been skipped
2025-11-25 10:21:23 +01:00
0c251ac807 feat: only install bibapi catalogue dependencies 2025-11-25 10:20:45 +01:00
5a1c19ccbe Merge pull request 'dev [release-patch]' (#6) from dev into main
Reviewed-on: #6
2025-11-25 09:05:53 +00:00
36f5dd14b5 check
All checks were successful
PR tests / build-and-smoke (pull_request) Successful in 1m4s
/ build (pull_request) Successful in 16m56s
2025-11-25 10:03:13 +01:00
811313d1ef fix: add uvicorn dependency
All checks were successful
PR tests / build-and-smoke (pull_request) Successful in 1m43s
feat: add test_api file
2025-11-25 09:59:24 +01:00
c44dc5a61c test new validation test
All checks were successful
PR tests / build-and-smoke (pull_request) Successful in 54s
2025-11-25 09:52:51 +01:00
c9f139d29c re-add port declaration
All checks were successful
PR tests / build-and-smoke (pull_request) Successful in 47s
2025-11-25 09:49:07 +01:00
6ef463458f use python instead of uv
All checks were successful
PR tests / build-and-smoke (pull_request) Successful in 1m19s
2025-11-25 09:46:46 +01:00
7a74ead335 chore: new service check using signature
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 1m19s
2025-11-25 09:41:11 +01:00
6f38dd482e chore: only import catalogue if needed
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 1m20s
2025-11-25 09:23:29 +01:00
e1da6085ed test new workflow
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 22s
2025-11-25 09:20:15 +01:00
af1ee0ce71 test: lazy initiation of catalogue
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 2m5s
2025-11-25 09:15:54 +01:00
0e7c45182a chore: add uv, generate requirements.txt for tests
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 54s
2025-11-25 09:12:51 +01:00
c21afa0776 chore: add custom pypi url
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 32s
2025-11-25 09:10:03 +01:00
df527b30da feat: add pr test workflow 2025-11-25 09:09:44 +01:00
0cf88113be maintenance: add .venv to dockerignore 2025-11-25 09:03:29 +01:00
59c90ca471 Merge pull request 'chore: replace apk with apt [release-patch]' (#5) from dev into main
Reviewed-on: #5
2025-11-25 08:00:20 +00:00
0d9ffb8539 chore: replace apk with apt
Some checks failed
/ build (pull_request) Failing after 12m43s
2025-11-25 08:59:46 +01:00
1e7d45ec49 Merge pull request 'test: inspired by other Dockerfile [release-patch]' (#4) from dev into main
Reviewed-on: #4
2025-11-25 07:51:22 +00:00
2ee575aff4 test: inspired by other Dockerfile
Some checks failed
/ build (pull_request) Failing after 11m19s
2025-11-25 08:50:07 +01:00
69e463c9a4 Merge pull request 'test: add path, switch to uv [release-patch]' (#3) from dev into main
Reviewed-on: #3
2025-11-25 07:39:30 +00:00
b1cdeffb4c Update pyproject.toml 2025-11-25 07:39:24 +00:00
e2cd6d7ba3 Update pyproject.toml 2025-11-25 07:38:30 +00:00
6837d0b4b3 test: add path, switch to uv
Some checks failed
/ build (pull_request) Failing after 11m20s
2025-11-25 08:35:46 +01:00
7 changed files with 171 additions and 50 deletions

View File

@@ -13,3 +13,4 @@ dist/
.gitignore .gitignore
.env .env
*.sock *.sock
.venv

View File

@@ -75,6 +75,18 @@ jobs:
run: | run: |
git config user.name "Gitea CI" git config user.name "Gitea CI"
git config user.email "ci@git.theprivateserver.de" 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 - name: Bump version
id: bump id: bump
run: | run: |
@@ -133,7 +145,7 @@ jobs:
uses: ad-m/github-push-action@master uses: ad-m/github-push-action@master
with: with:
github_token: ${{ secrets.GITHUB_TOKEN }} github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }} branch: ${{ env.PUSH_BRANCH }}
- name: Build Changelog - name: Build Changelog
id: build_changelog id: build_changelog
uses: https://github.com/mikepenz/release-changelog-builder-action@v5 uses: https://github.com/mikepenz/release-changelog-builder-action@v5

View File

@@ -0,0 +1,98 @@
name: PR tests
on:
pull_request:
types: [opened, synchronize, edited, reopened]
jobs:
build-image:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: (optional) Prepare dependencies
run: |
uv python install --python-version-file pyproject.toml || true
uv sync --all-groups || true
uv add pip || true
uv export --format requirements.txt -o requirements.txt || true
- name: Build image
run: |
docker build -t semapform-api:test-pr .
- name: Save image artifact
run: |
docker save semapform-api:test-pr -o semapform-api.tar
- name: Upload image artifact
uses: https://github.com/christopherHX/gitea-upload-artifact@v4
with:
name: semapform-image
path: semapform-api.tar
retention-days: 1
smoke-tests:
needs: build-image
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download image artifact
uses: christopherhx/gitea-download-artifact@v4
with:
name: semapform-image
- name: Restore image
run: |
docker load -i semapform-api.tar
- name: Start container (background)
run: |
docker run -d --name semapform-test semapform-api:test-pr sleep infinity
- name: Start server in container and smoke test HTTP (in-container)
run: |
set -x
# start the server inside the container (detached)
docker exec -d semapform-test python api_service.py || true
# show container status to aid debugging
docker ps -a --filter name=semapform-test || true
echo "waiting for service to become ready inside container"
set -e
READY=0
for i in $(seq 1 20); do
echo "ready attempt $i"
if docker exec semapform-test python -c 'import urllib.request,sys; urllib.request.urlopen("http://127.0.0.1:8001/health", timeout=1); print("ok")' ; then
READY=1
break
fi
sleep 1
done
if [ "$READY" -ne 1 ]; then
echo "service did not become ready"
docker logs semapform-test --tail 200 || true
exit 1
fi
echo "running test_api.py inside container"
docker exec semapform-test python test_api.py || true
docker logs semapform-test --tail 200 || true
- name: Cleanup container
if: always()
run: |
docker rm -f semapform-test || true

View File

@@ -1,43 +1,13 @@
FROM python:3.13-slim AS builder FROM python:3.13.9-slim
# Prevent Python from writing .pyc files and enable unbuffered stdout/stderr RUN apt update
ENV PYTHONDONTWRITEBYTECODE=1 \ RUN apt upgrade -y
PYTHONUNBUFFERED=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
WORKDIR /app WORKDIR /app
# Install build deps required to build wheels COPY requirements.txt .
RUN apt-get update \ RUN pip install --no-cache-dir -r requirements.txt --extra-index-url https://git.theprivateserver.de/api/packages/PHB/pypi/simple/
&& apt-get install -y --no-install-recommends build-essential gcc \
&& rm -rf /var/lib/apt/lists/*
# Upgrade packaging tools and install runtime dependencies into a separate prefix COPY . .
COPY requirements.txt ./
RUN pip install --upgrade pip setuptools wheel \
&& pip install --prefix=/install --no-cache-dir \
--extra-index-url https://git.theprivateserver.de/api/packages/PHB/pypi/simple/ \
-r requirements.txt \
&& rm -rf /root/.cache/pip
# Copy application source (only used to include app files in final image) ENTRYPOINT [ "python", "api_service.py" ]
COPY . /app
FROM python:3.13-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
API_PORT=8001
WORKDIR /app
# Copy installed packages from the builder image into the final image
COPY --from=builder /install /usr/local
# Copy application sources
COPY --from=builder /app /app
EXPOSE 8001
# Run using uvicorn; the app is defined in `api_service.py` as `app`
CMD ["uvicorn", "api_service:app", "--host", "0.0.0.0", "--port", "8001"]

View File

@@ -1,18 +1,39 @@
""" """Lightweight Python API service for signature validation
Lightweight Python API service for signature validation
This can run independently to support the PHP application This can run independently to support the PHP application
""" """
import os import os
import re import re
from bibapi import catalogue # Avoid importing heavy modules at top-level to keep `import api_service` lightweight
from fastapi import FastAPI, Query from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
app = FastAPI(title="Signature Validation API") 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 # Allow PHP application to call this API
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
@@ -22,15 +43,25 @@ app.add_middleware(
allow_headers=["*"], allow_headers=["*"],
) )
# Initialize catalogue for signature validation # Catalogue is expensive to initialize at import time; instantiate lazily
cat = catalogue.Catalogue() cat = None
def _get_catalogue():
global cat
if cat is None:
# import inside function to avoid expensive work during module import
from bibapi import catalogue as _catalogue
cat = _catalogue.Catalogue()
return cat
@app.get("/api/validate-signature") @app.get("/api/validate-signature")
async def validate_signature(signature: str = Query(...)): async def validate_signature(signature: str = Query(...)):
"""Validate a book signature and return total pages""" """Validate a book signature and return total pages"""
try: try:
book_result = cat.get_book_with_data(signature) book_result = _get_catalogue().get_book_with_data(signature)
if book_result and hasattr(book_result, "pages") and book_result.pages: if book_result and hasattr(book_result, "pages") and book_result.pages:
# Try to extract numeric page count # Try to extract numeric page count
pages_str = str(book_result.pages) pages_str = str(book_result.pages)
@@ -39,7 +70,7 @@ async def validate_signature(signature: str = Query(...)):
if match: if match:
total_pages = int(match.group(1)) total_pages = int(match.group(1))
return JSONResponse( return JSONResponse(
{"valid": True, "total_pages": total_pages, "signature": signature} {"valid": True, "total_pages": total_pages, "signature": signature},
) )
return JSONResponse( return JSONResponse(
@@ -47,13 +78,13 @@ async def validate_signature(signature: str = Query(...)):
"valid": False, "valid": False,
"error": "Signatur nicht gefunden oder keine Seitenzahl verfügbar", "error": "Signatur nicht gefunden oder keine Seitenzahl verfügbar",
"signature": signature, "signature": signature,
} },
) )
except Exception as e: except Exception as e:
return JSONResponse( return JSONResponse(
{ {
"valid": False, "valid": False,
"error": f"Fehler bei der Validierung: {str(e)}", "error": f"Fehler bei der Validierung: {e!s}",
"signature": signature, "signature": signature,
}, },
status_code=500, status_code=500,

View File

@@ -1,20 +1,21 @@
[project] [project]
name = "semapform-api" name = "semapform-api"
version = "0.1.1" version = "0.1.3"
description = "Add your description here" description = "Add your description here"
readme = "README.md" readme = "README.md"
requires-python = ">=3.13" requires-python = ">=3.13"
dependencies = [ dependencies = [
"bibapi[catalogue]>=0.0.5", "bibapi[catalogue]>=0.0.6",
"fastapi>=0.122.0", "fastapi>=0.122.0",
"pip>=25.3", "pip>=25.3",
"uvicorn>=0.38.0",
] ]
[[tool.uv.index]] [[tool.uv.index]]
name = "gitea" name = "gitea"
url = "https://git.theprivateserver.de/api/packages/PHB/pypi/simple/" url = "https://git.theprivateserver.de/api/packages/PHB/pypi/simple/"
[tool.bumpversion] [tool.bumpversion]
current_version = "0.1.1" current_version = "0.1.3"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)" parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
serialize = ["{major}.{minor}.{patch}"] serialize = ["{major}.{minor}.{patch}"]
search = "{current_version}" search = "{current_version}"

8
test_api.py Normal file
View File

@@ -0,0 +1,8 @@
# test api endpoint with a signature, print the response
import urllib.request
response = urllib.request.urlopen(
"http://localhost:8001/api/validate-signature?signature=ST%20250%20U42%20%2815%29",
)
print(response.read().decode("utf-8"))