From fd83e268ef57762f36ce1c26b1b04c8a8b878c97 Mon Sep 17 00:00:00 2001 From: marcsello Date: Wed, 15 Apr 2020 00:33:06 +0200 Subject: [PATCH] Initial commit --- Dockerfile | 31 +++----------- classification_service/app.py | 42 +++++++++++++++++++ classification_service/schemas/__init__.py | 2 + classification_service/tests/__init__.py | 1 + classification_service/utils/__init__.py | 3 ++ .../utils/error_handlers.py | 18 ++++++++ .../utils/require_decorators.py | 16 +++++++ classification_service/views/__init__.py | 2 + classification_service/views/classify_view.py | 11 +++++ requirements.txt | 16 ++++++- 10 files changed, 115 insertions(+), 27 deletions(-) create mode 100644 classification_service/app.py create mode 100644 classification_service/schemas/__init__.py create mode 100644 classification_service/tests/__init__.py create mode 100644 classification_service/utils/__init__.py create mode 100644 classification_service/utils/error_handlers.py create mode 100644 classification_service/utils/require_decorators.py create mode 100644 classification_service/views/__init__.py create mode 100644 classification_service/views/classify_view.py diff --git a/Dockerfile b/Dockerfile index 0283646..8db1da3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,9 @@ -# Use the official lightweight Python image. -# https://hub.docker.com/_/python -FROM python:3.8-slim +FROM python:3 -# Copy local code to the container image. -ENV APP_HOME /app -ENV PIP_NO_CACHE_DIR=true +ADD classification_service requirements.txt /classification_service/ +WORKDIR /classification_service/ -# Set timezone -ENV TZ Europe/Budapest -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +RUN pip3 install -r requirements.txt && pip3 install gunicorn -WORKDIR $APP_HOME - -ARG RELEASE_ID -ENV RELEASE_ID ${RELEASE_ID:-""} - -#Copy requirements to install -COPY requirements.txt ./ - -# Install production dependencies. -RUN pip install -r requirements.txt - -COPY ./src ./src - -EXPOSE 8080 - -ENTRYPOINT ["gunicorn", "-b", "0.0.0.0:8080", "--workers", "1", "--threads", "8", "app:app"] +EXPOSE 8000 +CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"] diff --git a/classification_service/app.py b/classification_service/app.py new file mode 100644 index 0000000..e0506d4 --- /dev/null +++ b/classification_service/app.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +import os +from flask import Flask +import sentry_sdk +from sentry_sdk.integrations.flask import FlaskIntegration + +# import stuff + +from utils import register_all_error_handlers + +# import views +from views import ClassifyView + + +# Setup sentry +SENTRY_DSN = os.environ.get("SENTRY_DSN") +if SENTRY_DSN: + sentry_sdk.init( + dsn=SENTRY_DSN, + integrations=[FlaskIntegration()], + send_default_pii=True, + release=os.environ.get('RELEASE_ID', 'test'), + environment=os.environ.get('RELEASEMODE', 'dev') + ) + +# create flask app +app = Flask(__name__) + +# important stuff +app.secret_key = os.environ.get('SECRET_KEY', os.urandom(12)) + +# register error handlers +register_all_error_handlers(app) + +# register views +for view in [ClassifyView]: + view.register(app, trailing_slash=False) + + +# start debugging if needed +if __name__ == "__main__": + app.run(debug=True) diff --git a/classification_service/schemas/__init__.py b/classification_service/schemas/__init__.py new file mode 100644 index 0000000..63f77b6 --- /dev/null +++ b/classification_service/schemas/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 + diff --git a/classification_service/tests/__init__.py b/classification_service/tests/__init__.py new file mode 100644 index 0000000..21b405d --- /dev/null +++ b/classification_service/tests/__init__.py @@ -0,0 +1 @@ +import os diff --git a/classification_service/utils/__init__.py b/classification_service/utils/__init__.py new file mode 100644 index 0000000..6c6531e --- /dev/null +++ b/classification_service/utils/__init__.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3 +from .require_decorators import json_required +from .error_handlers import register_all_error_handlers diff --git a/classification_service/utils/error_handlers.py b/classification_service/utils/error_handlers.py new file mode 100644 index 0000000..959f8ae --- /dev/null +++ b/classification_service/utils/error_handlers.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + + +def get_standard_error_handler(code: int): + def error_handler(err): + return {"msg": str(err)}, code + + return error_handler + + +# function to register all handlers + + +def register_all_error_handlers(app): + error_codes_to_override = [404, 403, 401, 405, 400, 409, 422] + + for code in error_codes_to_override: + app.register_error_handler(code, get_standard_error_handler(code)) diff --git a/classification_service/utils/require_decorators.py b/classification_service/utils/require_decorators.py new file mode 100644 index 0000000..ba5b9ab --- /dev/null +++ b/classification_service/utils/require_decorators.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +from flask import request, current_app, abort + +from functools import wraps + + +def json_required(f): + @wraps(f) + def call(*args, **kwargs): + + if request.is_json: + return f(*args, **kwargs) + else: + abort(400, "JSON required") + + return call diff --git a/classification_service/views/__init__.py b/classification_service/views/__init__.py new file mode 100644 index 0000000..215034d --- /dev/null +++ b/classification_service/views/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 +from .classify_view import ClassifyView diff --git a/classification_service/views/classify_view.py b/classification_service/views/classify_view.py new file mode 100644 index 0000000..9b2edb5 --- /dev/null +++ b/classification_service/views/classify_view.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +from flask import request, jsonify +from flask_classful import FlaskView +from utils import json_required + + +class ClassifyView(FlaskView): + + @json_required + def post(self): + return jsonify(request.json) diff --git a/requirements.txt b/requirements.txt index 3e4cee2..6ddc380 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,14 @@ -sentry_sdk -gunicorn \ No newline at end of file +requests +blinker +Flask +marshmallow +Flask-Classful +sentry-sdk + +pyAudioanalysis +numpy +eyed3 +pydub +scipy +matplotlib +sklearn \ No newline at end of file