From e8e40e6eb141cbd73d00a73e3cc81845b9b2432b Mon Sep 17 00:00:00 2001 From: Philipp Traber Date: Tue, 10 Sep 2024 16:08:08 +0200 Subject: [PATCH] init --- .github/workflows/.docker-image.yml | 46 +++++++++++++++++ Dockerfile | 7 +++ README.md | 17 +++++++ docker-compose.yml | 5 ++ requirements.txt | 4 ++ src/api.py | 43 ++++++++++++++++ src/template/main.html | 76 +++++++++++++++++++++++++++++ 7 files changed, 198 insertions(+) create mode 100644 .github/workflows/.docker-image.yml create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 requirements.txt create mode 100644 src/api.py create mode 100644 src/template/main.html diff --git a/.github/workflows/.docker-image.yml b/.github/workflows/.docker-image.yml new file mode 100644 index 0000000..4139823 --- /dev/null +++ b/.github/workflows/.docker-image.yml @@ -0,0 +1,46 @@ +name: Build and Push Docker Image + +on: + push: + branches: + - main + tags: + - 'v*.*.*' # Trigger on version tags + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Log in to Docker Registry + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login ${{ secrets.REGISTRY_URL }} -u ${{ secrets.DOCKER_USERNAME }} --password-stdin + + - name: Extract repo name + id: repo + run: echo "REPO_NAME=$(basename ${{ github.repository }})" >> $GITHUB_ENV + + - name: Set image tags + id: tags + run: | + if [[ "${GITHUB_REF}" == refs/tags/* ]]; then + TAG="${GITHUB_REF#refs/tags/}" + else + TAG="${GITHUB_SHA::7}" # Short SHA for non-tag builds + fi + echo "IMAGE_TAG=${TAG}" >> $GITHUB_ENV + echo "IMAGE_TAG_LATEST=latest" >> $GITHUB_ENV + + - name: Build Docker image + run: docker build -t ${{ secrets.REGISTRY_URL }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} . + + - name: Push Docker image + run: | + docker push ${{ secrets.REGISTRY_URL }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} + docker tag ${{ secrets.REGISTRY_URL }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} ${{ secrets.REGISTRY_URL }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG_LATEST }} + docker push ${{ secrets.REGISTRY_URL }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG_LATEST }} + + - name: Logout of Docker Registry + run: docker logout ${{ secrets.REGISTRY_URL }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d7693b0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3 +WORKDIR /app +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY src/ . +CMD [ "fastapi", "run", "api.py" ] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6cc5777 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Spätzle check + +Me and my colleges have been eating in the Mensa at our university for a while now. If spätzle are on the menu it was always a gamble if they are good or really bad. Until somebody told us the problem of the bad spätzle is that they are vegan (no hate against vegan products but some products just shouldn't be vegan). + + +Because of the lack of eggs the spätzle are not sticking together and are just a big pile of mush. + +Using the allergen information on the mensa website we can check if the spätzle are vegan or not and therefore can decide if one can eat them or not. + +This project serves a simple [website](spaetzle.traberph.de) that checks the spätzle for you. + +## How to run +Simply run it with docker-compose + +``` +docker-compose up +``` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..677df3b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,5 @@ +services: + web: + build: . + ports: + - "8000:8000" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7e3308d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +fastapi[standard]==0.114.0 +Jinja2==3.1.4 +requests==2.32.3 +cachetools==5.5.0 \ No newline at end of file diff --git a/src/api.py b/src/api.py new file mode 100644 index 0000000..f35d870 --- /dev/null +++ b/src/api.py @@ -0,0 +1,43 @@ +from fastapi import FastAPI +from fastapi import FastAPI, Request +from fastapi.responses import HTMLResponse +from fastapi.templating import Jinja2Templates +import requests +from cachetools import cached, TTLCache + +cache = TTLCache(maxsize=1, ttl=3600) +app = FastAPI() +templates = Jinja2Templates(directory="template") + +@cached(cache) +def checkMensa(): + print("Checking Mensa") + url = 'https://seezeit.com/essen/speiseplaene/mensa-giessberg/' + r = requests.get(url) + text = r.text + pos = text.find('Spätzle') + if pos != -1: + cut = r.text[r.text.find('Spätzle'):r.text.find('Spätzle')+50] + ingredients = cut.split('(')[1].split(')')[0].split(',') + if '28' in ingredients: + answer = "Spätzle with egg today" + color = "green" + emoji = "✅" + else: + answer = "WARNING: Spätzle without egg today" + color = "red" + emoji = "❌" + else: + answer = "No Spätzle today " + color = "grey" + emoji = "🤷🏻‍♂️" + + return answer, color, emoji + +@app.get("/", response_class=HTMLResponse) +async def root(request: Request): + answer, color, emoji = checkMensa() + return templates.TemplateResponse( + request=request, name="main.html", context={"text": answer, "color": color , "emoji": emoji} + ) + diff --git a/src/template/main.html b/src/template/main.html new file mode 100644 index 0000000..728908a --- /dev/null +++ b/src/template/main.html @@ -0,0 +1,76 @@ + + + + + + + + + + {{text}} - {{emoji}} + + + + +

👮

+

Spätzle Check:

+
+ {{ text }} +
+
{{emoji}}
+ + + + + \ No newline at end of file