Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1114817
WIP: docker and tbump doc
pritam001 Jan 9, 2021
3a784ac
Merge branch 'master' of https://github.com/pritam001/pyflask-microse…
pritam001 Jan 9, 2021
7879e71
Merge branch 'development' of https://github.com/pritam001/pyflask-mi…
pritam001 Jan 9, 2021
54d22b9
Update Makefile
pritam001 Jan 24, 2021
39da388
Add pyflask-preferences.yaml and modify init script to read prefs
pritam001 Jan 26, 2021
aae5a0f
Check python version and env managers from init script
pritam001 Jan 26, 2021
be46fe8
Create env from init script based on pref
pritam001 Jan 26, 2021
e1bca35
Create wipe script
pritam001 Jan 26, 2021
ea5cb02
Modify service name and env and setup script
pritam001 Jan 27, 2021
f061a78
Modify pre commit hooks
pritam001 Jan 27, 2021
8116187
Update pre-commit hook versions
pritam001 Jan 30, 2021
48d5e1f
Add make upgrade script
pritam001 Jan 30, 2021
9885cf9
Fix upgrade script and commit requirement versions
pritam001 Jan 30, 2021
0205459
Update dynaconf code to v3
pritam001 Jan 30, 2021
b4212dc
Update git pythonpackage flow
pritam001 Jan 30, 2021
ec146e5
Fix git pythonpackage flow
pritam001 Jan 30, 2021
47cf6b2
Modify scripts to support python versions
pritam001 Jan 30, 2021
6601934
Fix isort warning
pritam001 Jan 30, 2021
4d70260
Add pyflask-config markers
pritam001 Jan 30, 2021
ddca232
Add application run scripts
pritam001 Jan 30, 2021
482477b
Create personalize.py
pritam001 Jan 30, 2021
666e360
Read prefs yaml in personalize.py
pritam001 Jan 30, 2021
21dc8dc
Read inputs for prefs
pritam001 Jan 31, 2021
68fee1c
Minor changes in readme, pref script
pritam001 Jan 31, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]
python-version: [3.7, 3.8]

steps:
- uses: actions/checkout@v2
Expand All @@ -19,9 +19,10 @@ jobs:
- name: Install pip
run: |
python -m pip install --upgrade pip
- name: Setup project
- name: Install dependencies
run: |
make setup
pip3 install pip-tools
pip-sync requirements/requirements.txt requirements/dev-requirements.txt
- name: Lint
run: |
pip install flake8
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ venv/
ENV/
env.bak/
venv.bak/
pyflask-base-env/

# Spyder project settings
.spyderproject
Expand Down
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/ambv/black
rev: stable
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
# pyflask_config: python_version
language_version: python3.8
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.3
rev: 3.8.4
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.3.2
rev: v5.7.0
hooks:
- id: isort
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# pyflask_config: python_version
FROM tiangolo/uwsgi-nginx:python3.8

# pyflask_config: docker_maintainer_details
LABEL maintainer="User <user@gmail.com>"

#RUN make setup
#
## Move the base entrypoint to reuse it
#RUN mv /entrypoint.sh /uwsgi-nginx-entrypoint.sh
## Copy the entrypoint that will generate Nginx additional configs
#COPY entrypoint.sh /entrypoint.sh
#RUN chmod +x /entrypoint.sh
#
#ENTRYPOINT ["/entrypoint.sh"]

# Run the start script provided by the parent image tiangolo/uwsgi-nginx.
# It will check for an /app/prestart.sh script (e.g. for migrations)
# And then will start Supervisor, which in turn will start Nginx and uWSGI
#CMD ["/prestart.sh"]
141 changes: 101 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
CONDA_ENV_NAME=pyflask-base
APP_NAME=pyflask-service
# pyflask_config: python_env_name
PYTHON_ENV_NAME=pyflask-base-env
# pyflask_config: service_name
APPLICATION_NAME=pyflask-microservice

APP_DIR=src
TEST_DIR=test
SCRIPTS_DIR=scripts
HOME_DIR_PY_FILES=*.py

# echo _FormatCode_ guide:
# 0 Reset all styles
# 1 Bold
# 31 Red
# 32 Green
# 34 Blue
# 35 Magenta
# 36 Cyan
RESET_STYLES=\033[0m
BOLD_RED=\033[1m\033[31m
BOLD_GREEN=\033[1m\033[32m
BOLD_BLUE=\033[1m\033[34m
BOLD_MAGENTA=\033[1m\033[35m
BOLD_CYAN=\033[1m\033[36m

# Signifies our desired python version
Expand All @@ -21,7 +29,7 @@ PYTHON = python3

# .PHONY defines parts of the makefile that are not dependant on any specific file
# This is most often used to store functions
.PHONY = all help setup format lint format-n-lint test-n-cover pre-commit clean
.PHONY = all help init setup upgrade format lint format-n-lint test-n-cover pre-commit debug-run dev-run prod-run docker-build docker-run clean wipe clean-n-wipe

# Defining an array variable
IGNORED_FILES_AND_FOLDERS = .mypy_cache/ .pytest_cache/ htmlcov/ logs/ .coverage
Expand All @@ -34,91 +42,144 @@ IGNORED_FILES_AND_FOLDERS = .mypy_cache/ .pytest_cache/ htmlcov/ logs/ .coverage
# The @ makes sure that the command itself isn't echoed in the terminal
help:
@echo "$(BOLD_BLUE)-----------------------------MAKE GUIDE----------------------------$(RESET_STYLES)"
@echo "$(BOLD_CYAN)make setup$(RESET_STYLES) : Setup pyflask-service"
@echo "$(BOLD_CYAN)make format$(RESET_STYLES) : Format and fix python code in pyflask-service"
@echo "$(BOLD_CYAN)make lint$(RESET_STYLES) : Lint pyflask-service"
@echo "$(BOLD_CYAN)make format-n-lint$(RESET_STYLES) : Format and lint python code in pyflask-service"
@echo "$(BOLD_CYAN)make test-n-cover$(RESET_STYLES) : Test and code coverage pyflask-service"
@echo "$(BOLD_CYAN)make pre-commit$(RESET_STYLES) : Run pre-commit checks for pyflask-service"
@echo "$(BOLD_CYAN)make debug$(RESET_STYLES) : Debug pyflask-service"
@echo "$(BOLD_CYAN)make clean$(RESET_STYLES) : Clean pyflask-service"
@echo "$(BOLD_CYAN)make dev-run$(RESET_STYLES) : Run pyflask-service in environment=development"
@echo "$(BOLD_CYAN)make prod-run$(RESET_STYLES) : Run pyflask-service in environment=prod"
@echo "$(BOLD_CYAN)make init$(RESET_STYLES) : Initialize pyflask-microservice"
@echo "$(BOLD_CYAN)make setup$(RESET_STYLES) : Setup pyflask-microservice"
@echo "$(BOLD_CYAN)make upgrade$(RESET_STYLES) : Upgrade dependencies of pyflask-microservice"
@echo "$(BOLD_CYAN)make format$(RESET_STYLES) : Format and fix python code in pyflask-microservice"
@echo "$(BOLD_CYAN)make lint$(RESET_STYLES) : Lint pyflask-microservice"
@echo "$(BOLD_CYAN)make format-n-lint$(RESET_STYLES) : Format and lint python code in pyflask-microservice"
@echo "$(BOLD_CYAN)make test-n-cover$(RESET_STYLES) : Test and code coverage pyflask-microservice"
@echo "$(BOLD_CYAN)make pre-commit$(RESET_STYLES) : Run pre-commit checks for pyflask-microservice"
@echo "$(BOLD_CYAN)make debug-run$(RESET_STYLES) : Debug pyflask-microservice"
@echo "$(BOLD_CYAN)make dev-run$(RESET_STYLES) : Run pyflask-microservice in environment=development"
@echo "$(BOLD_CYAN)make prod-run$(RESET_STYLES) : Run pyflask-microservice in environment=prod"
@echo "$(BOLD_CYAN)make docker-build$(RESET_STYLES) : Build docker image for pyflask-microservice"
@echo "$(BOLD_CYAN)make docker-run$(RESET_STYLES) : Run docker image for pyflask-microservice"
@echo "$(BOLD_CYAN)make clean$(RESET_STYLES) : Clean pyflask-microservice"
@echo "$(BOLD_CYAN)make wipe$(RESET_STYLES) : Wipe pyflask-microservice"
@echo "$(BOLD_CYAN)make clean-n-wipe$(RESET_STYLES) : Clean and wipe pyflask-microservice"
@echo "$(BOLD_BLUE)-------------------------------------------------------------------$(RESET_STYLES)"


init: #: Initialize and personalize python environment
@echo "\n$(BOLD_BLUE)Initialize pyflask-microservice$(RESET_STYLES)"
sh scripts/dev/service_init.sh


setup: #: Use pip-tools, pip-compile, pip install
@echo "\n$(BOLD_CYAN)Setting up pyflask base$(RESET_STYLES)"
# Check for venv, conda else exit
@echo "\n$(BOLD_BLUE)Setting up pyflask-microservice$(RESET_STYLES)"
@echo "\n$(BOLD_CYAN)Activating python environment . . .$(RESET_STYLES)"
sh scripts/dev/activate_python_env.sh
sh scripts/dev/check_python_env.sh
@echo "\n$(BOLD_CYAN)Checking dependencies . . .$(RESET_STYLES)"
sh scripts/dev/check_env_manager.sh
sh scripts/dev/check_python_version.sh
@echo "\n$(BOLD_CYAN)Installing pip-tools . . .$(RESET_STYLES)"
pip install pip-tools
pip3 install pip-tools
@echo "\n$(BOLD_CYAN)Generating requirements$(RESET_STYLES)"
pip-compile -q --build-isolation --output-file=requirements/requirements.txt requirements/requirements.in
@echo "\n$(BOLD_CYAN)Generating dev requirements$(RESET_STYLES)"
pip-compile -q --build-isolation --output-file=requirements/dev-requirements.txt requirements/dev-requirements.in
@echo "\n$(BOLD_CYAN)Syncing requirements$(RESET_STYLES)"
pip-sync -q requirements/requirements.txt requirements/dev-requirements.txt
@echo "\n$(BOLD_CYAN)Installing requirements$(RESET_STYLES)"
pip install -r requirements/requirements.txt
@echo "\n$(BOLD_CYAN)Installing dev requirements$(RESET_STYLES)"
pip install -r requirements/dev-requirements.txt
pip-sync requirements/requirements.txt requirements/dev-requirements.txt
@echo "\n$(BOLD_CYAN)Adding pre-commit hooks$(RESET_STYLES)"
pre-commit install
@echo "\n$(BOLD_GREEN)Setup complete$(RESET_STYLES)"


upgrade: #: upgrade requirements to latest versions
@echo "\n$(BOLD_BLUE)Upgrading dependencies of pyflask-microservice$(RESET_STYLES)"
python3 -m pip install --upgrade pip
pip-compile --upgrade requirements/requirements.in --output-file requirements/requirements.txt
pip-compile --upgrade requirements/dev-requirements.in --output-file requirements/dev-requirements.txt
@echo "\n$(BOLD_GREEN)Upgrade complete$(RESET_STYLES)"


format: #: Format and fix python code with black, isort, autoflake
@echo "\n$(BOLD_CYAN)Blackifying$(RESET_STYLES) 🍳"
@echo "\n$(BOLD_BLUE)Blackifying$(RESET_STYLES) 🍳"
black --version
black $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES)
black $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES) $(SCRIPTS_DIR)
@echo "\n$(BOLD_CYAN)ISorting$(RESET_STYLES) 〽️️"
isort --recursive $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES)
isort $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES) $(SCRIPTS_DIR)
@echo "\n$(BOLD_CYAN)Flaking$(RESET_STYLES) ❄️"
flake8 --version
autoflake --remove-all-unused-imports --remove-unused-variables --remove-duplicate-keys --ignore-init-module-imports -i -r $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES)
@echo "\n$(BOLD_CYAN)Running pre-commit hooks$(RESET_STYLES) 🏁️️️"
pre-commit run --all-files
@echo "\n$(BOLD_CYAN)All checks passed$(RESET_STYLES) 🏳️️️️"
@echo "\n"
autoflake --remove-all-unused-imports --remove-unused-variables --remove-duplicate-keys --ignore-init-module-imports -i -r $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES) $(SCRIPTS_DIR)


lint: #: Run static analysis with flake8, radon, mypy and bandit
@echo "\n$(BOLD_CYAN)Linting with flake8$(RESET_STYLES) ❄️"
flake8 --version
flake8 $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES)
flake8 $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES) $(SCRIPTS_DIR)
@echo "\n$(BOLD_CYAN)Checking cyclomatic complexity with radon$(RESET_STYLES) 💫️"
radon --version
radon cc $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES) --total-average -nc
radon cc $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES) $(SCRIPTS_DIR) --total-average -nc
@echo "\n$(BOLD_CYAN)Static typing with mypy$(RESET_STYLES) ⌨️"
mypy --version
mypy $(APP_DIR) $(HOME_DIR_PY_FILES)
@echo "\n$(BOLD_CYAN)Securing with bandit$(RESET_STYLES) 🕵️️"
bandit --version
bandit -l -i -r . --format=custom -c .bandit.yml -x ./$(TEST_DIR)
bandit -l -i -r . --format=custom -c .bandit.yml -x ./$(TEST_DIR),./$(PYTHON_ENV_NAME)
@echo "\n"


format-n-lint: #: Format and lint
make format
make lint
make --no-print-directory format
make --no-print-directory lint
@echo "\n"


test-n-cover: #: Test with pytest, Code coverage with pytest-cov plugin
@echo "\n$(BOLD_CYAN)Testing with pytest$(RESET_STYLES) 📊️"
@echo "\n$(BOLD_BLUE)Testing with pytest$(RESET_STYLES) 📊️"
pytest --version
pytest
@echo "\n"


pre-commit: #: Run pre-commit checks : format, lint, test, cover
make format-n-lint
make test-n-cover
@echo "\n$(BOLD_CYAN)Pre commit jobs completed$(RESET_STYLES) 👍"
@echo "\n"
@echo "\n$(BOLD_BLUE)Starting pre-commit jobs . . .$(RESET_STYLES)"
make --no-print-directory format-n-lint
@echo "\n$(BOLD_CYAN)Running pre-commit hooks$(RESET_STYLES) 🏁️️️"
pre-commit run --all-files
@echo "\n$(BOLD_GREEN)All checks passed$(RESET_STYLES) 🏳️️️️"
make --no-print-directory test-n-cover
@echo "\n$(BOLD_GREEN)Pre-commit jobs completed$(RESET_STYLES) 👍"


debug-run: #: Run application in debug mode
@echo "\n$(BOLD_BLUE)Starting application in debug mode . . .$(RESET_STYLES)"
FLASK_ENV=development python3 service_master.py


dev-run: #: Run application in dev mode
@echo "\n$(BOLD_BLUE)Starting application in dev mode . . .$(RESET_STYLES)"
FLASK_ENV=development python3 service_master.py


prod-run: #: Run application in prod mode
@echo "\n$(BOLD_BLUE)Starting application in prod mode . . .$(RESET_STYLES)"
FLASK_ENV=prod python3 service_master.py


docker-build: #: Build docker image for the application
@echo "\n$(BOLD_BLUE)Building docker image . . .$(RESET_STYLES)"


docker-start: #: Start docker image for the application
@echo "\n$(BOLD_BLUE)Starting dockerized application . . .$(RESET_STYLES)"


clean: #: Clean unnecessary files
@echo "\n$(BOLD_CYAN)Cleaning unnecessary files$(RESET_STYLES) 🚿"
rm -rf $(IGNORED_FILES_AND_FOLDERS)
@echo "\n"


wipe: #: Wipe python environment
@echo "\n$(BOLD_CYAN)Wiping environment$(RESET_STYLES) 🚿"
sh scripts/dev/service_wipe.sh


clean-n-wipe: #: Clean project and wipe environment
@echo "\n$(BOLD_BLUE)Cleaning up project and wiping environment$(RESET_STYLES)"
make --no-print-directory clean
make --no-print-directory wipe

48 changes: 21 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[//]: <> (pyflask_config: service_name)
pyflask-microservice-base
------------------------------------------------------------------------------
An "optionally opinionated and structured" flask boilerplate microservice for jump-starting development
Expand All @@ -17,7 +18,8 @@ Project status

Tools
------------------------------------------------------------------------------
[![Min Python Version 3.8+](https://img.shields.io/badge/python-3.8+-3776AB.svg?style=flat)](https://www.python.org/download/releases/3.8.0/)
[![Min Python Version 3.7](https://img.shields.io/badge/python-3.7+-3776AB.svg?style=flat)](https://www.python.org/download/releases/3.8.0/)
[![Min GNU Make Version 4.3](https://img.shields.io/badge/make-4.3+-222222.svg?style=flat)](https://www.gnu.org/software/make/manual/make.html)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat)](https://github.com/psf/black)
[![Static Analysis: flake8](https://img.shields.io/badge/static%20analysis-flake8-cccccc.svg?style=flat)](https://github.com/PyCQA/flake8/)
[![Cyclomatic Complexity: radon](https://img.shields.io/badge/cyclomatic%20complexity-radon-ff5252.svg?style=flat)](https://github.com/rubik/radon)
Expand All @@ -40,10 +42,11 @@ This is a template project hosted on GitHub which can be used to create new repo
1. Create a new repository named "my-pyflask-project" using this template repository *+
1. `git clone https://www.github.com/username/my-pyflask-project.git`
1. `cd my-pyflask-project`
1. Create and activate conda environment
`conda init my-conda-venv`
`conda activate my-conda-venv` *^
1. `make setup` : Use pip-tools, pip-compile, pip install to setup python packages
1. `make init` : Initialize and personalize project
1. `make setup` : Use pip-tools, pip-compile, pip install to set up python packages
1. `make upgrade` : Upgrade dependencies to latest version
1. `make pre-commit` : Run format, lint, test and cover
1. Run `git add .` and `git commit -m "Personalize service"`

*+ [GitHub Guide: Creating a repository from a template](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template)
<br>
Expand Down Expand Up @@ -71,44 +74,35 @@ Type `make pre-commit` before committing your changes to run formatters, linters


### Documentation
Flasgger docs can be found at http://0.0.0.0:8420/api/v1/pyflask-service/swagger
[//]: <> (pyflask_config: service_name)
Flasgger docs can be found at http://0.0.0.0:8420/api/v1/pyflask-microservice/swagger

API specs can be found at http://0.0.0.0:8420/api/v1/pyflask-service/swagger_spec
[//]: <> (pyflask_config: service_name)
API specs can be found at http://0.0.0.0:8420/api/v1/pyflask-microservice/swagger_spec

Flasgger UI version: v2 | OpenAPI version: 2

<p align="center"><img src="documentation/assets/flasgger_intro.png" /></p>


Development Setup
------------------------------------------------------------------------------
To be updated


Contributing
------------------------------------------------------------------------------
1. Stargaze this repository
1. Fork this repository
1. Add this project as `upstream`
1. Commit your changes
1. Create pull request to `development` branch

[![](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/images/0)](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/links/0)
[![](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/images/1)](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/links/1)
[![](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/images/2)](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/links/2)
[![](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/images/3)](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/links/3)
[![](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/images/4)](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/links/4)
[![](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/images/5)](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/links/5)
[![](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/images/6)](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/links/6)
[![](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/images/7)](https://sourcerer.io/fame/pritam001/pritam001/pyflask-microservice-base/links/7)

1. Create pull request to `upstream/development` branch

TODO
Roadmap
-----------------------------------------------------------------------------
- [x] Swagger support
- [ ] Update linting documentation
- [x] Introduction of testing tools
- [ ] Testing tools documentation
- [x] Code coverage
- [ ] Version handling by tbump
- [x] Version handling by tbump
- [ ] Personalization script
- [ ] Docker support
- [ ] uwsgi server
- [ ] Add CoC, Contribution guidelines, PR and issue templates
- [ ] Update linting documentation
- [ ] Testing tools documentation

6 changes: 6 additions & 0 deletions docker_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# pyflask_config: service_name
app="docker.image.pyflask-microservice"
docker build -t ${app} .
docker run -d -p 56733:80 --name=${app} -v "$PWD":/app ${app}
Loading