From a43ce47339014b7ab141fbf3f963ae6a95594503 Mon Sep 17 00:00:00 2001 From: Michelle Date: Sun, 10 May 2026 23:11:54 +0200 Subject: [PATCH] init project Co-authored-by: Copilot --- .env.example | 6 +++ .github/workflows/build-and-publish.yml | 56 +++++++++++++++++++++++++ .gitignore | 2 + Dockerfile | 10 +++++ README.md | 2 + compose.dev.yml | 23 ++++++++++ compose.yml | 23 ++++++++++ main.py | 38 +++++++++++++++++ requirements.txt | 2 + 9 files changed, 162 insertions(+) create mode 100644 .env.example create mode 100644 .github/workflows/build-and-publish.yml create mode 100644 Dockerfile create mode 100644 compose.dev.yml create mode 100644 compose.yml create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..541a432 --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +DB_HOST=mariadb +DB_PORT=3306 +DB_ROOT_PASSWORD=rootpassword +DB_USER=bnuy +DB_PASSWORD=example +DB_NAME=bnuy \ No newline at end of file diff --git a/.github/workflows/build-and-publish.yml b/.github/workflows/build-and-publish.yml new file mode 100644 index 0000000..213645c --- /dev/null +++ b/.github/workflows/build-and-publish.yml @@ -0,0 +1,56 @@ +# this is the first time I write a github / gitea actions workflow myself, so excuse me if it sucks +name: Build and publish bnuy api + +on: + push: + branches: + - master + release: + types: [published] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v6 + + # this step was made by AI, i hope it works + - name: Extract registry host + id: registry + run: echo "host=$(echo ${{ github.server_url }} | sed 's|https\?://||')" >> "$GITHUB_OUTPUT" + + - name: Docker meta + id: meta + uses: docker/metadata-action@v6 + with: + images: ${{ steps.registry.outputs.host }}/${{ github.repository }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v4 + + - name: Login to Gitea Container Registry + uses: docker/login-action@v4 + with: + registry: ${{ steps.registry.outputs.host }} + username: ${{ github.repository_owner }} + password: ${{ secrets.REPO_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v7 + with: + context: . + file: ./Dockerfile + push: ${{ github.event_name != 'pull_request' }} + platforms: linux/amd64,linux/arm64 + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.gitignore b/.gitignore index 36b13f1..5bc8d25 100644 --- a/.gitignore +++ b/.gitignore @@ -174,3 +174,5 @@ cython_debug/ # PyPI configuration file .pypirc +data/ + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..69550b4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +# copy pasted from ich_iel bot +FROM python:3.13.13-slim-trixie + +ENV PATH=/usr/local/bin:$PATH +WORKDIR /app +COPY requirements.txt . +RUN apt-get update && apt-get install -y gcc libmariadb-dev && rm -rf /var/lib/apt/lists/* +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["fastapi", "run", "main.py"] \ No newline at end of file diff --git a/README.md b/README.md index 759c611..6e96b6f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # bnuy-api +A API which collects pictures of bunnies and provides a API to get random bunny pictures. +doesn't work yet, still in progress \ No newline at end of file diff --git a/compose.dev.yml b/compose.dev.yml new file mode 100644 index 0000000..4731e4f --- /dev/null +++ b/compose.dev.yml @@ -0,0 +1,23 @@ +services: + bnuy-api: + build: . + restart: unless-stopped + volumes: + - ./data:/app/data + ports: + - "8000:8000" + env_file: ".env" + + mariadb: + image: mariadb:12 + restart: unless-stopped + environment: + - MARIADB_DATABASE=${DB_NAME} + - MARIADB_PORT=${DB_PORT} + - MARIADB_USER=${DB_USER} + - MARIADB_PASSWORD=${DB_PASSWORD} + - MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD} + volumes: + - mariadb_data:/var/lib/mysql +volumes: + mariadb_data: \ No newline at end of file diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..8ff1fed --- /dev/null +++ b/compose.yml @@ -0,0 +1,23 @@ +services: + bnuy-api: + image: git.scrunkly.cat/michelle/bnuy-api:latest + restart: unless-stopped + volumes: + - ./data:/app/data + ports: + - "8000:8000" + env_file: ".env" + + mariadb: + image: mariadb:12 + restart: unless-stopped + environment: + - MARIADB_DATABASE=${DB_NAME} + - MARIADB_PORT=${DB_PORT} + - MARIADB_USER=${DB_USER} + - MARIADB_PASSWORD=${DB_PASSWORD} + - MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD} + volumes: + - mariadb_data:/var/lib/mysql +volumes: + mariadb_data: \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..17ff2f7 --- /dev/null +++ b/main.py @@ -0,0 +1,38 @@ +from fastapi import FastAPI +import asyncmy +import asyncio +import os +from contextlib import asynccontextmanager + +@asynccontextmanager +async def connect_db(app: FastAPI): + app.state.pool = await asyncmy.create_pool( + host=os.getenv("DB_HOST", "localhost"), + port=int(os.getenv("DB_PORT", 3306)), + user=os.getenv("DB_USER"), + password=os.getenv("DB_PASSWORD"), + db=os.getenv("DB_NAME"), + minsize=5, + maxsize=20 + ) + try: + yield + finally: + app.state.pool.close() + await app.state.pool.wait_closed() + +app = FastAPI(lifespan=connect_db) + +@asynccontextmanager +async def get_connection(): + async with app.state.pool.acquire() as conn: + yield conn + +@app.get("/") +async def root(): + return {"message": "yes the api works"} + +@app.get("/random") +async def get_random_bnuy(): + async with get_connection() as conn: + return {"message": "here could be a bnuy, if I would've implemented it"} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..36d1d38 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi[standard] +asyncmy \ No newline at end of file