Skip to content

Commit

Permalink
Initialize main-redis-mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
adhishthite authored Mar 16, 2024
0 parents commit e611888
Show file tree
Hide file tree
Showing 17 changed files with 845 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env-t
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SERVICE_NAME="flask-app-template"
ENVIRONMENT="development"
SECRET_TOKEN="secret-token"
SERVER_URL="http://localhost:5000"
59 changes: 59 additions & 0 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# .github/workflows/pytest.yaml
name: PyTest

on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ['3.12']
mongodb-version: ['7.0']

timeout-minutes: 10

name: PyTests

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest-cov diff-cover
- name: Start MongoDB
uses: supercharge/[email protected]

- name: Run tests with PyTest
env:
SERVICE_NAME: "flask-app-template"
ENVIRONMENT: "development"
SECRET_TOKEN: "secret-token"
SERVER_URL: "http://localhost:5000"
MONGO_URL: "localhost"

run: |
set -o pipefail
pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered --cov=app tests/ | tee pytest-coverage.txt
coverage xml
exit ${PIPESTATUS[0]}
- name: PyTest Coverage Commentator
uses: MishaKav/pytest-coverage-comment@main
with:
pytest-coverage-path: ./pytest-coverage.txt
junitxml-path: ./pytest.xml
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
.vscode/
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Use an official Python runtime as a parent image
FROM python:3.12
LABEL authors="adhishthite"

# Install Nginx
RUN apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/*

# Set the working directory in the container to /app
WORKDIR /app

# First, copy only the requirements.txt file
COPY requirements.txt /app/

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy the current directory contents into the container at /app
COPY . /app

# Setup Nginx to forward requests to Gunicorn
COPY nginx.conf /etc/nginx/sites-available/default

# Make port 8000 available to the world outside this container
EXPOSE 8000
EXPOSE 80

# Run app.py when the container launches
CMD service nginx start && gunicorn --workers 8 app.app:app --bind unix:/app/app.sock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Adhish Thite

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Flask App Template

This is a template of a Flask App that uses NGINX and GUNICORN for deployment.

## Overview

The Flask App Template is designed to provide a starting point for developing web applications using the Flask framework. It includes integration with NGINX and GUNICORN for efficient deployment.

## Features

- Integration with NGINX and GUNICORN
- Simplified structure for easy project initiation
- Use of best practices and recommended plugins
- Integration with Docker for easy deployment
- Use of MongoDB for data storage and Redis for caching
- Integrated with GitHub Actions

## Getting Started

To get started with this template, follow these steps:

0. Clone the repository.

```bash
git clone https://github.com/adhishthite/flask-app-template
```

1. Navigate to the repository

```bash
cd flask-app-template
```

2. Rename the `.env-t` file to `.env` and add/update the required environment variables.

```bash
mv .env-t .env
```

3. Build the Docker image using docker-compose.

```bash
docker-compose up --build
```


[WIP]


## License


## Feedback

I welcome feedback and suggestions. Please feel free to open an issue or submit a pull request.

---
Empty file added app/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os

from flask import Flask, jsonify
import elasticapm
from elasticapm.contrib.flask import ElasticAPM
from dotenv import load_dotenv

from . import data

load_dotenv()

app = Flask(__name__)
if os.environ['ENVIRONMENT'] == 'production':
app.config['ELASTIC_APM'] = {
'SERVICE_NAME': os.environ['SERVICE_NAME'],
'SECRET_TOKEN': os.environ['SECRET_TOKEN'],
'SERVER_URL': os.environ['SERVER_URL'],
'ENVIRONMENT': os.environ['ENVIRONMENT'],
}

apm = ElasticAPM(app)


@app.route('/', methods=['GET'])
@elasticapm.capture_span()
def hello_world():
return data.healthcheck()


@app.route('/mongo_status', methods=['GET'])
@elasticapm.capture_span()
def mongo_status():
return jsonify(data.get_mongo_status()), 200


if __name__ == '__main__':
app.run(debug=True, port=6062)
Loading

2 comments on commit e611888

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
app
   app.py21386%14–21, 37
   database.py77495%33, 45–46, 64
TOTAL109794% 

Tests Skipped Failures Errors Time
19 0 💤 0 ❌ 0 🔥 1.097s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
app
   app.py21386%14–21, 37
   database.py77495%33, 45–46, 64
TOTAL109794% 

Tests Skipped Failures Errors Time
19 0 💤 0 ❌ 0 🔥 0.969s ⏱️

Please sign in to comment.