From 026c987977592ecfe781c190d687e824134104ac Mon Sep 17 00:00:00 2001 From: CFIALeronB <133677161+CFIALeronB@users.noreply.github.com> Date: Mon, 6 Nov 2023 14:51:26 -0500 Subject: [PATCH] Initial commit with basic Flask project structure This commit sets up a minimal Flask project with the following: - app.py: Contains a simple read_root route. - requirements.txt: Lists Flask as a dependency. - .env file: Defines FLASK_APP environment variable for flask run. - venv/: A virtual environment for project-specific dependencies. --- .gitignore | 38 ++++++++++++++++++++++++++++++++++ Dockerfile | 16 ++++++++++++++ devcontainer/devcontainer.json | 6 ++++++ requirements-production.txt | 2 ++ requirements.txt | 3 +++ src/__init__.py | 0 src/app.py | 11 ++++++++++ tests/__init__.py | 0 tests/test_main.py | 16 ++++++++++++++ 9 files changed, 92 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 devcontainer/devcontainer.json create mode 100644 requirements-production.txt create mode 100644 requirements.txt create mode 100644 src/__init__.py create mode 100644 src/app.py create mode 100644 tests/__init__.py create mode 100644 tests/test_main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a64fefc --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +# Ignore Python Bytecode +__pycache__/ +*.pyc + +# Ignore Python virtual environment +venv/ + +# Ignore IDE and editor-specific files (customize this based on your editor or IDE) +.idea/ + +# Ignore environment-specific files +.env + +# Ignore any local configuration files +config.local.ini + +# Ignore log files +*.log + +# Ignore compiled Python files +*.pyc +*.pyo +*.pyd + +# Ignore database files +*.db + +# Ignore cache directories +__pycache__/ +.cache/ + +# Ignore any sensitive or private information (e.g., API keys, passwords) +secrets.txt +keys/ +*.pem + +# Ignore Flask Sessions +flask_session/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0afba2e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +# Dockerfile +# Base image with Python 3.11 on Alpine +FROM python:3.11-alpine + +# Establish /code as working directory in the container +WORKDIR /code + +# Copy production requirements and install dependencies +COPY ./requirements-production.txt requirements.txt +RUN pip3 install --no-cache-dir -r requirements.txt + +# Copy source code into the working directory +COPY ./src . + +# Use Gunicorn as the server, configuring it for the Flask app +ENTRYPOINT gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 --forwarded-allow-ips "*" main:app diff --git a/devcontainer/devcontainer.json b/devcontainer/devcontainer.json new file mode 100644 index 0000000..5d452f0 --- /dev/null +++ b/devcontainer/devcontainer.json @@ -0,0 +1,6 @@ +{ + "name": "Python 3", + "image": "mcr.microsoft.com/devcontainers/python:3.11", + "postCreateCommand": "pip3 install --user -r requirements.txt" +} + diff --git a/requirements-production.txt b/requirements-production.txt new file mode 100644 index 0000000..e4a286c --- /dev/null +++ b/requirements-production.txt @@ -0,0 +1,2 @@ +flask +gunicorn diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3d3769d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +gunicorn +# additional dependencies diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..45ea532 --- /dev/null +++ b/src/app.py @@ -0,0 +1,11 @@ +# src/main.py +from datetime import datetime +from flask import Flask + +app = Flask(__name__) + +@app.route('/') +def read_root(): + current_time = datetime.now() + unix_timestamp = int(current_time.timestamp()) + return {"current_time": unix_timestamp} diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..b58292e --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,16 @@ +# tests/test_main.py +import unittest +from src.app import app + +class TestApp(unittest.TestCase): + + def setUp(self): + self.app = app.test_client() + self.app.testing = True + + def test_read_root(self): + response = self.app.get('/') + data = response.get_json() + self.assertEqual(response.status_code, 200) + self.assertTrue('current_time' in data) + self.assertIsInstance(data['current_time'], int)