From b92ac6b19f366071e3f5364d564b5414e6efbc3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Thu, 11 Nov 2021 13:31:27 +0100 Subject: [PATCH] init --- .dockerignore | 140 ++++++++++++++++++++++++++++++++++++++++++++ .gitignore | 134 ++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 19 ++++++ requirements.txt | 7 +++ src/app.py | 48 +++++++++++++++ src/config.py | 22 +++++++ src/marshm.py | 14 +++++ src/redis_client.py | 4 ++ src/schemas.py | 25 ++++++++ 9 files changed, 413 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 requirements.txt create mode 100644 src/app.py create mode 100644 src/config.py create mode 100644 src/marshm.py create mode 100644 src/redis_client.py create mode 100644 src/schemas.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..51f8a0c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,140 @@ +# 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/ +pip-wheel-metadata/ +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/ + +# 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 +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.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 + +# 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/ + +#Pycharm +.idea/ + +*.md +.gitignore +.git/ +*.yml +contrib/* +postman/* +*.wav \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13a10e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,134 @@ +# 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/ +pip-wheel-metadata/ +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/ + +# 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 +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.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 + +# 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/ + +#Pycharm +.idea/ +*.iml +*wav \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..033d6d5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.9-slim + +ENV TZ Europe/Budapest +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +WORKDIR /app + +ARG RELEASE_ID +ENV RELEASE_ID ${RELEASE_ID:-""} + +COPY requirements.txt ./ + +RUN pip install --no-cache-dir -r requirements.txt + +COPY ./src . + +EXPOSE 8080 + +ENTRYPOINT ["gunicorn", "-b", "0.0.0.0:8080", "app:app"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..aac4cc2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +sentry_sdk[flask] +gunicorn +Flask +Flask-RESTful +marshmallow +flask-marshmallow +flask-redis \ No newline at end of file diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..2965c02 --- /dev/null +++ b/src/app.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +from flask import Flask +from flask_restful import Api +import sentry_sdk +from sentry_sdk.integrations.flask import FlaskIntegration + +from redis_client import redis_client +from config import Config +from marshm import ma +from resources import SampleResource + +""" +Main Flask RESTful API +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2021, KMLabz Team" +__module_name__ = "app" +__version__text__ = "1" + +if Config.SENTRY_DSN: + sentry_sdk.init( + dsn=Config.SENTRY_DSN, + integrations=[FlaskIntegration()], + traces_sample_rate=0.0, + send_default_pii=True, + release=Config.RELEASE_ID, + environment=Config.RELEASEMODE + ) + +app = Flask(__name__) +app.config.from_object(Config) + +api = Api(app) +ma.init_app(app) + +redis_client.init_app(app) + + +api.add_resource(SampleResource, "/input") + + +if __name__ != '__main__': + import logging + + gunicorn_logger = logging.getLogger('gunicorn.error') + app.logger.handlers = gunicorn_logger.handlers + app.logger.setLevel(gunicorn_logger.level) diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..0654aef --- /dev/null +++ b/src/config.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +import os + +""" +Main Flask RESTful API +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2021, KMLabz Team" +__module_name__ = "config" +__version__text__ = "1" + + +class Config: + PORT = 8080 + DEBUG = os.environ.get("DEBUG", "true").lower() in ["true", "yes", "1"] + + SENTRY_DSN = os.environ.get("SENTRY_DSN") + RELEASE_ID = os.environ.get("RELEASE_ID", "test") + RELEASEMODE = os.environ.get("RELEASEMODE", "dev") + + REDIS_URL = os.environ['CACHE_REDIS_URL'] diff --git a/src/marshm.py b/src/marshm.py new file mode 100644 index 0000000..a05b227 --- /dev/null +++ b/src/marshm.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +from flask_marshmallow import Marshmallow + +""" +Marshmallow +""" + +__author__ = '@tormakris' +__copyright__ = "Copyright 2021, KMLabz Team" +__module_name__ = "marshm" +__version__text__ = "1" + +ma = Marshmallow() diff --git a/src/redis_client.py b/src/redis_client.py new file mode 100644 index 0000000..3bba60d --- /dev/null +++ b/src/redis_client.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python3 +from flask_redis import FlaskRedis + +redis_client = FlaskRedis() diff --git a/src/schemas.py b/src/schemas.py new file mode 100644 index 0000000..5d1c32f --- /dev/null +++ b/src/schemas.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +from marshm import ma +from marshmallow import fields + + +""" +Schemas of api objects +""" + + +__author__ = "@tormakris" +__copyright__ = "Copyright 2021, KMLabz Team" +__module_name__ = "schemas" +__version__text__ = "1" + + +class SampleSchema(ma.Schema): + """ + Parameters: + - date (date) + - device_id (int) + """ + + date = fields.DateTime(required=True) + device_id = fields.Integer(required=True)