This commit is contained in:
Dennis Kerschus 2024-08-10 20:26:57 +02:00
commit 62f3ae7881
21 changed files with 189 additions and 0 deletions

8
.dockerignore Normal file
View File

@ -0,0 +1,8 @@
__pycache__
*.pyc
*.pyo
*.pyd
*.db
*.sqlite
*.log
*.env

4
.env Normal file
View File

@ -0,0 +1,4 @@
FLASK_APP=run.py
FLASK_ENV=development
SECRET_KEY=your-secret-key
SQLALCHEMY_DATABASE_URI=sqlite:///development.db

20
Dockerfile Normal file
View File

@ -0,0 +1,20 @@
# Use the official image as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Define environment variable
ENV FLASK_APP wsgi.py
# Run app.py when the container launches
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "wsgi:app"]

0
app/__init__.py Normal file
View File

25
app/app.py Normal file
View File

@ -0,0 +1,25 @@
from flask import Flask
from app.config.config import get_config_by_name
from app.initialize_functions import initialize_route,initialize_db
def create_app(config=None) -> Flask:
"""
Create a Flask application.
Args:
config: The configuration object to use.
Returns:
A Flask application instance.
"""
app = Flask(__name__)
if config:
app.config.from_object(get_config_by_name(config))
# Initialize extensions
initialize_db(app)
# Register blueprints
initialize_route(app)
return app

0
app/config/__init__.py Normal file
View File

34
app/config/config.py Normal file
View File

@ -0,0 +1,34 @@
class BaseConfig:
"""Base configuration."""
DEBUG = False
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = 'your-secret-key'
class DevelopmentConfig(BaseConfig):
"""Development configuration."""
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///development.db'
class TestingConfig(BaseConfig):
"""Testing configuration."""
DEBUG = True
TESTING = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///testing.db'
class ProductionConfig(BaseConfig):
"""Production configuration."""
DEBUG = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///production.db'
def get_config_by_name(config_name):
""" Get config by name """
if config_name == 'development':
return DevelopmentConfig()
elif config_name == 'production':
return ProductionConfig()
elif config_name == 'testing':
return TestingConfig()
else:
return DevelopmentConfig()

0
app/db/__init__.py Normal file
View File

9
app/db/db.py Normal file
View File

@ -0,0 +1,9 @@
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)

View File

@ -0,0 +1,14 @@
from flask import Flask
from app.modules.main.route import main_bp
from app.db.db import db
def initialize_route(app: Flask):
with app.app_context():
app.register_blueprint(main_bp)
def initialize_db(app: Flask):
with app.app_context():
db.init_app(app)
db.create_all()

0
app/modules/__init__.py Normal file
View File

View File

View File

@ -0,0 +1,3 @@
class MainController:
def index(self):
return {'message':'Hello, World!'}

View File

@ -0,0 +1,10 @@
import unittest
import json
from app.modules.main.controller import MainController
def test_index():
main_controller = MainController()
result = main_controller.index()
assert result == {'message': 'Hello, World!'}

11
app/modules/main/route.py Normal file
View File

@ -0,0 +1,11 @@
from flask import Blueprint, make_response, jsonify
from .controller import MainController
main_bp = Blueprint('main', __name__)
main_controller = MainController()
@main_bp.route('/')
def index():
result=main_controller.index()
return make_response(jsonify(data=result))

0
app/tests/__init__.py Normal file
View File

15
app/tests/conftest.py Normal file
View File

@ -0,0 +1,15 @@
import pytest
from app.app import create_app
@pytest.fixture
def app():
app = create_app('testing')
app.config.update({"TESTING": True})
yield app
@pytest.fixture
def client(app):
return app.test_client()

12
docker-compose.yml Normal file
View File

@ -0,0 +1,12 @@
version: '3.8'
services:
web:
build: .
command: gunicorn --bind 0.0.0.0:5000 wsgi:app
volumes:
- .:/app
ports:
- "5000:5000"
environment:
- FLASK_ENV=production

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
Flask
Flask-SQLAlchemy
python-dotenv
pytest
gunicorn

16
run.py Normal file
View File

@ -0,0 +1,16 @@
import os
from dotenv import load_dotenv
from app.app import create_app
load_dotenv()
config=os.getenv('FLASK_ENV') or 'development'
app = create_app(config)
if __name__ == "__main__":
if config == 'development':
app.run(debug=True)
else:
from werkzeug.serving import run_simple
run_simple('0.0.0.0', 5000, app)

3
wsgi.py Normal file
View File

@ -0,0 +1,3 @@
from app.app import create_app
app = create_app('production')