commit 7a3cb2afd8bae3caca20802a6dff3106b69e3953 Author: marcsello Date: Wed Apr 14 19:22:22 2021 +0200 Added skeleton diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..9c99cac --- /dev/null +++ b/.drone.yml @@ -0,0 +1,25 @@ +kind: pipeline +type: docker +name: default +steps: + - + - name: code-analysis + image: aosapps/drone-sonar-plugin + settings: + sonar_host: + from_secret: SONAR_HOST + sonar_token: + from_secret: SONAR_CODE + + - name: kaniko + image: banzaicloud/drone-kaniko + settings: + registry: registry.kmlabz.com + repo: universalrobots/${DRONE_REPO_NAME} + username: + from_secret: DOCKER_USERNAME + password: + from_secret: DOCKER_PASSWORD + tags: + - latest + - ${DRONE_BUILD_NUMBER} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53fd5d2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.swp +venv +*.pyc +__pycache__/* +__pycache__ +*.wpr +*.log +.idea diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fa741f5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.9 + +ADD program_service requirements.txt /program_service/ +WORKDIR /program_service/ + +RUN pip3 install -r requirements.txt && pip3 install gunicorn + +EXPOSE 8000 +CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"] + diff --git a/program_service/app.py b/program_service/app.py new file mode 100644 index 0000000..3153f37 --- /dev/null +++ b/program_service/app.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +from config import Config +import sentry_sdk +from sentry_sdk.integrations.flask import FlaskIntegration +from flask import Flask + +from utils import register_all_error_handlers + +# import views +from views import ProgramView + +# Setup sentry + +if Config.SENTRY_DSN: + sentry_sdk.init( + dsn=Config.SENTRY_DSN, + integrations=[FlaskIntegration()], + send_default_pii=True, + release=Config.RELEASE_ID, + environment=Config.RELEASEMODE + ) + +# create flask app +app = Flask(__name__) +app.config.from_object(Config) +register_all_error_handlers(app) + + +# register views +for view in [ProgramView]: + view.register(app, trailing_slash=False) + +# start debuggig if needed +if __name__ == "__main__": + app.run(debug=True) diff --git a/program_service/config.py b/program_service/config.py new file mode 100644 index 0000000..532830c --- /dev/null +++ b/program_service/config.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +import os + +""" +Configuration +""" + + +class Config: + SECRET_KEY = os.environ.get("SECRET_KEY", os.urandom(12)) + CORS_ORIGINS = os.environ.get("ALLOWED_ORIGINS", "*") + + SENTRY_DSN = os.environ.get("SENTRY_DSN") + RELEASE_ID = os.environ.get("RELEASE_ID", "test") + RELEASEMODE = os.environ.get("RELEASEMODE", "dev") diff --git a/program_service/schemas/__init__.py b/program_service/schemas/__init__.py new file mode 100644 index 0000000..5f7ce86 --- /dev/null +++ b/program_service/schemas/__init__.py @@ -0,0 +1 @@ +#!/usr/bin/env python3 \ No newline at end of file diff --git a/program_service/utils/__init__.py b/program_service/utils/__init__.py new file mode 100644 index 0000000..a1e0203 --- /dev/null +++ b/program_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 \ No newline at end of file diff --git a/program_service/utils/error_handlers.py b/program_service/utils/error_handlers.py new file mode 100644 index 0000000..959f8ae --- /dev/null +++ b/program_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/program_service/utils/require_decorators.py b/program_service/utils/require_decorators.py new file mode 100644 index 0000000..ba5b9ab --- /dev/null +++ b/program_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/program_service/views/__init__.py b/program_service/views/__init__.py new file mode 100644 index 0000000..f330c9c --- /dev/null +++ b/program_service/views/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 +from .program_view import ProgramView diff --git a/program_service/views/program_view.py b/program_service/views/program_view.py new file mode 100644 index 0000000..9dd7bc2 --- /dev/null +++ b/program_service/views/program_view.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +from flask import request, jsonify, current_app, abort, Response +from flask_classful import FlaskView, route + + +class ProgramView(FlaskView): + + def index(self): + pass + + def get(self, _id: str): + pass + + def post(self): + pass + + @route('<_id>/details') + def get_details(self, _id: str): + pass + + def delete(self, _id: str): + pass diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d0408c6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +pymongo~=3.11.3 +pyyaml + +blinker +Flask +marshmallow +Flask-Classful +sentry-sdk +