From 0be947b3d194f23950cf9421187f6c33b19668ba Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Fri, 25 Mar 2022 20:03:33 +0100 Subject: [PATCH] Add Docker dev environment and initial code --- .gitignore | 164 +++++------------------------------------- Dockerfile.dev | 22 ++++++ LICENSE => LICENSE.md | 0 Makefile | 47 ++++++++++++ Pipfile | 13 ++++ Pipfile.lock | 45 ++++++++++++ README.md | 4 +- docker-compose.yml | 18 +++++ tofu_api/__init__.py | 0 tofu_api/app.py | 10 +++ 10 files changed, 173 insertions(+), 150 deletions(-) create mode 100644 Dockerfile.dev rename LICENSE => LICENSE.md (100%) create mode 100644 Makefile create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 docker-compose.yml create mode 100644 tofu_api/__init__.py create mode 100644 tofu_api/app.py diff --git a/.gitignore b/.gitignore index 55be276..f11a714 100644 --- a/.gitignore +++ b/.gitignore @@ -1,154 +1,22 @@ -# ---> Python -# Byte-compiled / optimized / DLL files +# IDEs / editors +.idea/ +.vscode/ + +# General +/tmp +/_tmp +.cache + +# Python __pycache__/ *.py[cod] -*$py.class -# C extensions -*.so +# Python packaging and dev environments +/.pip +/venv -# 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/ +# Python testing .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 - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow -__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 maintainted 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/ - +.pytest_cache +.tox/ diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 0000000..e67e44c --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,22 @@ +FROM python:3.10 + +# Install pipenv container-wide as root +RUN pip install pipenv + +# Create unprivileged user with UID as specified by the build argument DEV_USER_UID, defaulting to 1000. +# (This should match the UID of your local user. If it doesn't, use an ".env" file to overwrite this variable.) +ARG DEV_USER_UID=1000 +RUN echo Creating dev user with UID $DEV_USER_UID && \ + useradd -m -u $DEV_USER_UID dev +USER dev + +## Create virtual environment using pipenv +WORKDIR /app +COPY Pipfile Pipfile.lock ./ +RUN pipenv install --dev --deploy + +# Set entrypoint to always run commands inside virtual environment +ENTRYPOINT ["pipenv", "run"] + +# Set default command +CMD ["bash"] diff --git a/LICENSE b/LICENSE.md similarity index 100% rename from LICENSE rename to LICENSE.md diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b2befc4 --- /dev/null +++ b/Makefile @@ -0,0 +1,47 @@ +# Define variables +DOCKER_COMPOSE = docker-compose +DOCKER_RUN = $(DOCKER_COMPOSE) run --rm backend + +# Default target +.PHONY: all +all: docker-up + + +# Container management +# -------------------- + +.PHONY: docker-up +docker-up: + $(DOCKER_COMPOSE) up --build + +.PHONY: docker-down +docker-down: + $(DOCKER_COMPOSE) down + +.PHONY: docker-build +docker-build: + $(DOCKER_COMPOSE) build + +.PHONY: docker-rebuild +docker-rebuild: + $(DOCKER_COMPOSE) build --pull --no-cache + +.PHONY: docker-purge +docker-purge: + $(DOCKER_COMPOSE) down --volumes + +.PHONY: docker-restart +docker-restart: + $(DOCKER_COMPOSE) restart $(SERVICE) + +.PHONY: docker-logs +docker-logs: + $(DOCKER_COMPOSE) logs -f $(SERVICE) || true + +.PHONY: docker-run +docker-run: + $(DOCKER_RUN) "$(CMD)" + +.PHONY: docker-shell +docker-shell: + $(DOCKER_RUN) bash diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..ec7dc2e --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +gunicorn = "~=20.1" +werkzeug = "~=2.0" + +[dev-packages] + +[requires] +python_version = "3.10" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..24260f8 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,45 @@ +{ + "_meta": { + "hash": { + "sha256": "574b2898e332128ecb874d71081cf7a8c62e4e219a603d0d76a5d9afb3ac164c" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.10" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "gunicorn": { + "hashes": [ + "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e", + "sha256:e0a968b5ba15f8a328fdfd7ab1fcb5af4470c28aaf7e55df02a99bc13138e6e8" + ], + "index": "pypi", + "version": "==20.1.0" + }, + "setuptools": { + "hashes": [ + "sha256:6221e37dc86fcdc9dad9d9eb2002e9f9798fe4aca1bf18f280e66e50c0eb7fca", + "sha256:ad88b13f3dc60420259c9877486908ddad12c7befaff0d624c7190f742abd64f" + ], + "markers": "python_version >= '3.7'", + "version": "==61.0.0" + }, + "werkzeug": { + "hashes": [ + "sha256:1421ebfc7648a39a5c58c601b154165d05cf47a3cd0ccb70857cbdacf6c8f2b8", + "sha256:b863f8ff057c522164b6067c9e28b041161b4be5ba4d0daceeaa50a163822d3c" + ], + "index": "pypi", + "version": "==2.0.3" + } + }, + "develop": {} +} diff --git a/README.md b/README.md index dcf0061..3a27296 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# tofu-api +# Tofu - REST API -REST API backend for Tofu - a to-do list app with a twist. \ No newline at end of file +REST API backend for Tofu - a to-do list app with a twist. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e6e3655 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3' + +services: + backend: + build: + context: . + dockerfile: Dockerfile.dev + args: + # Use an ".env" file to overwrite this variable if your local user's UID is not 1000. + DEV_USER_UID: ${DEV_USER_UID:-1000} + ports: + - '8080:8080' + volumes: + - ./:/app/ + command: gunicorn --bind=0.0.0.0:8080 --reload tofu_api.app:app + +volumes: + mariadb_data: diff --git a/tofu_api/__init__.py b/tofu_api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tofu_api/app.py b/tofu_api/app.py new file mode 100644 index 0000000..e570081 --- /dev/null +++ b/tofu_api/app.py @@ -0,0 +1,10 @@ +def app(environ, start_response): + """Simplest possible application object""" + data = b'Hello, wooorld!\n' + status = '200 OK' + response_headers = [ + ('Content-type', 'text/plain'), + ('Content-Length', str(len(data))) + ] + start_response(status, response_headers) + return iter([data])