From 8828803754a753c61628e161d9890c9038ca2e1d Mon Sep 17 00:00:00 2001 From: Karl Date: Mon, 14 Jul 2025 13:42:58 +0100 Subject: [PATCH] working config.py setup --- .gitea/workflows/docker-publish.yml | 45 +++++++++++++++++++++++++++++ .vscode/launch.json | 15 ++++------ config.py.sample | 11 +++++++ dockerfile | 28 ++++++++++++++++++ ktvmanager/config.py | 22 ++++++++++++++ ktvmanager/lib/database.py | 20 +++++-------- ktvmanager/main.py | 11 ++++++- 7 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 .gitea/workflows/docker-publish.yml create mode 100644 config.py.sample create mode 100644 dockerfile create mode 100644 ktvmanager/config.py diff --git a/.gitea/workflows/docker-publish.yml b/.gitea/workflows/docker-publish.yml new file mode 100644 index 0000000..d748978 --- /dev/null +++ b/.gitea/workflows/docker-publish.yml @@ -0,0 +1,45 @@ +name: Build and Publish Docker Image + +on: + push: + tags: + - "v*.*.*" + workflow_dispatch: + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ secrets.REGISTRY }}/${{ secrets.USERNAME }}/ktvmanager-backend-python + tags: | + type=semver,pattern={{version}} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Registry + uses: docker/login-action@v3 + with: + registry: ${{ secrets.REGISTRY }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: | + org.opencontainers.image.version=${{ steps.meta.outputs.version }} + org.opencontainers.image.title=ktvmanager-backend-python + org.opencontainers.image.description=ktvmanager-backend-python + build-args: | + VERSION=${{ steps.meta.outputs.version }} diff --git a/.vscode/launch.json b/.vscode/launch.json index a19fda1..21c801f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,19 +7,16 @@ "request": "launch", "module": "flask", "env": { - "FLASK_APP": "ktvmanager.main:create_app()", - "FLASK_DEBUG": "1" + "FLASK_APP": "ktvmanager.main:create_app", + "FLASK_ENV": "development", + "PYTHONPATH": "${workspaceFolder}", + "FLASK_RUN_PORT": "5001" }, "args": [ "run", - "--no-debugger", - "--no-reload", - "--host=0.0.0.0", - "--port=5001" + "--host=0.0.0.0" ], - "jinja": true, - "justMyCode": true, - "python": "${workspaceFolder}/.venv/bin/python" + "jinja": false } ] } \ No newline at end of file diff --git a/config.py.sample b/config.py.sample new file mode 100644 index 0000000..1d9ebcc --- /dev/null +++ b/config.py.sample @@ -0,0 +1,11 @@ +# config.py.sample + +class Config: + DEBUG = False + SECRET_KEY = 'a_secret_key' + +class DevelopmentConfig(Config): + DEBUG = True + +class ProductionConfig(Config): + pass \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..5fd988c --- /dev/null +++ b/dockerfile @@ -0,0 +1,28 @@ +FROM python:3.11-slim-bookworm + +RUN apt-get update && apt-get install -y --no-install-recommends \ + libglib2.0-0 \ + libsm6 \ + libxrender1 \ + libxext6 \ + libgomp1 \ + libgl1 \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +RUN chmod +x run.sh + +ARG VERSION +RUN echo $VERSION > VERSION + +EXPOSE 3001 + +ENV FLASK_ENV production +CMD ["./run.sh"] diff --git a/ktvmanager/config.py b/ktvmanager/config.py new file mode 100644 index 0000000..631cf73 --- /dev/null +++ b/ktvmanager/config.py @@ -0,0 +1,22 @@ +# ktvmanager/config.py + +import os + +class Config: + DEBUG = False + TESTING = False + SECRET_KEY = os.environ.get('SECRET_KEY', 'my_secret_key') + HOST = '0.0.0.0' + PORT = 3001 + DBHOST = os.getenv("DBHOST") + DBUSER = os.getenv("DBUSER") + DBPASS = os.getenv("DBPASS") + DATABASE = os.getenv("DATABASE") + DBPORT = os.getenv("DBPORT") + STREAM_URLS = ["http://example.com", "http://example.org"] + +class DevelopmentConfig(Config): + DEBUG = True + +class ProductionConfig(Config): + PORT = os.environ.get('PORT', 5000) \ No newline at end of file diff --git a/ktvmanager/lib/database.py b/ktvmanager/lib/database.py index 588bd1b..6f216ee 100644 --- a/ktvmanager/lib/database.py +++ b/ktvmanager/lib/database.py @@ -1,19 +1,15 @@ -import os import mysql.connector -from dotenv import load_dotenv -from flask import jsonify, request +from flask import jsonify, request, current_app from ktvmanager.lib.checker import single_account_check from ktvmanager.lib.encryption import encrypt_password, decrypt_password -load_dotenv() - def _create_connection(): return mysql.connector.connect( - host=os.getenv("DBHOST"), - user=os.getenv("DBUSER"), - password=os.getenv("DBPASS"), - database=os.getenv("DATABASE"), - port=os.getenv("DBPORT") + host=current_app.config["DBHOST"], + user=current_app.config["DBUSER"], + password=current_app.config["DBPASS"], + database=current_app.config["DATABASE"], + port=current_app.config["DBPORT"] ) def _execute_query(query, params=None): @@ -58,9 +54,7 @@ def get_stream_names(): def single_check(): data = request.get_json() - # This is a placeholder for getting stream URLs. In a real application, - # this would likely come from a database query or a configuration file. - stream_urls = ["http://example.com", "http://example.org"] + stream_urls = current_app.config["STREAM_URLS"] result = single_account_check(data, stream_urls) if result: # Here you would typically update the database with the new information diff --git a/ktvmanager/main.py b/ktvmanager/main.py index 60b3215..0765d7d 100644 --- a/ktvmanager/main.py +++ b/ktvmanager/main.py @@ -1,8 +1,17 @@ +import os from flask import Flask, jsonify +from dotenv import load_dotenv +from ktvmanager.config import DevelopmentConfig, ProductionConfig from routes.api import api_blueprint def create_app(): app = Flask(__name__) + load_dotenv() + + if os.environ.get("FLASK_ENV") == "production": + app.config.from_object(ProductionConfig) + else: + app.config.from_object(DevelopmentConfig) # Register blueprints app.register_blueprint(api_blueprint) @@ -20,4 +29,4 @@ def create_app(): if __name__ == "__main__": app = create_app() - app.run(debug=True, host='0.0.0.0', port=5001) \ No newline at end of file + app.run(debug=app.config["DEBUG"], host=app.config["HOST"], port=app.config["PORT"]) \ No newline at end of file