working config.py setup

This commit is contained in:
Karl 2025-07-14 13:42:58 +01:00
parent ff5b8b4937
commit 8828803754
7 changed files with 129 additions and 23 deletions

View File

@ -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 }}

15
.vscode/launch.json vendored
View File

@ -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
}
]
}

11
config.py.sample Normal file
View File

@ -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

28
dockerfile Normal file
View File

@ -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"]

22
ktvmanager/config.py Normal file
View File

@ -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)

View File

@ -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

View File

@ -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)
app.run(debug=app.config["DEBUG"], host=app.config["HOST"], port=app.config["PORT"])