From 03bf58bab310d2347748c234ec4c49f360dfde4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Wed, 25 Mar 2020 01:19:22 +0100 Subject: [PATCH] project layout done --- Dockerfile | 15 +++----------- k8s/deployment.yaml | 30 +++++++++++++++++++++++++++ k8s/service.yaml | 16 +++++++++++++++ requirements.txt | 6 +++++- src/app.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ src/config.py | 22 ++++++++++++++++++++ src/resources.py | 26 +++++++++++++++++++++++ src/schemas.py | 25 +++++++++++++++++++++++ 8 files changed, 177 insertions(+), 13 deletions(-) create mode 100644 k8s/deployment.yaml create mode 100644 k8s/service.yaml create mode 100644 src/app.py create mode 100644 src/config.py create mode 100644 src/resources.py create mode 100644 src/schemas.py diff --git a/Dockerfile b/Dockerfile index 0283646..8e04625 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,27 +1,18 @@ -# Use the official lightweight Python image. -# https://hub.docker.com/_/python FROM python:3.8-slim -# Copy local code to the container image. -ENV APP_HOME /app -ENV PIP_NO_CACHE_DIR=true - -# Set timezone ENV TZ Europe/Budapest RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone -WORKDIR $APP_HOME +WORKDIR /app ARG RELEASE_ID ENV RELEASE_ID ${RELEASE_ID:-""} -#Copy requirements to install COPY requirements.txt ./ -# Install production dependencies. -RUN pip install -r requirements.txt +RUN pip install --no-cache-dir -r requirements.txt -COPY ./src ./src +COPY ./src . EXPOSE 8080 diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml new file mode 100644 index 0000000..c9a0f2d --- /dev/null +++ b/k8s/deployment.yaml @@ -0,0 +1,30 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: input-service + namespace: birbnetes + labels: + app: input-service +spec: + replicas: 1 + selector: + matchLabels: + app: input-service + strategy: + type: Recreate + template: + metadata: + labels: + app: input-service + spec: + containers: + - image: + name: registry.kmlabz.com/tormakris/input-service + env: + - name: SENTRY_DSN + valueFrom: + secretKeyRef: + name: sentry-dsn + key: input-service + ports: + - containerPort: 8080 \ No newline at end of file diff --git a/k8s/service.yaml b/k8s/service.yaml new file mode 100644 index 0000000..c5b1e30 --- /dev/null +++ b/k8s/service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: input-service + namespace: birbnetes + labels: + app: input-service +spec: + ports: + - name: input-service + port: 8080 + targetPort: 8080 + protocol: TCP + selector: + app: input-service + type: ClusterIP \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3e4cee2..cb01866 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,6 @@ sentry_sdk -gunicorn \ No newline at end of file +gunicorn +marshmallow +Flask +Flask-RESTful +requests \ No newline at end of file diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..0103e8b --- /dev/null +++ b/src/app.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +import logging +import sentry_sdk +from flask import Flask +from flask_restful import Api +from sentry_sdk.integrations.flask import FlaskIntegration + +from config import SENTRY_DSN, RELEASE_ID, RELEASEMODE, PORT, DEBUG +from resources import * + +""" +Main Flask RESTful API +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2019, KSZK" +__module_name__ = "app" +__version__text__ = "1" + +if SENTRY_DSN: + sentry_sdk.init( + dsn=SENTRY_DSN, + integrations=[FlaskIntegration()], + send_default_pii=True, + release=RELEASE_ID, + environment=RELEASEMODE + ) + +app = Flask(__name__) +api = Api(app) + +formatter = logging.Formatter( + fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s" +) + +handler = logging.StreamHandler() +handler.setFormatter(formatter) + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) +logger.addHandler(handler) + +api.add_resource(SampleResource, "/v1/input/sample") + +if __name__ == "__main__": + app.run( + debug=bool(DEBUG), + host="0.0.0.0", + port=int(PORT), + ) diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..f4135f1 --- /dev/null +++ b/src/config.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +import os + + +""" +Main Flask RESTful API +""" + + +__author__ = "@tormakris" +__copyright__ = "Copyright 2019, KSZK" +__module_name__ = "app" +__version__text__ = "1" + + +PORT = os.environ.get("INPUT_SERVICE_PORT", 8080) +DEBUG = os.environ.get("INPUT_SERVICE_DEBUG", True) + + +SENTRY_DSN = os.environ.get("SENTRY_DSN") +RELEASE_ID = os.environ.get("RELEASE_ID") +RELEASEMODE = os.environ.get("INPUT_SERVICE_RELEASEMODE") diff --git a/src/resources.py b/src/resources.py new file mode 100644 index 0000000..62bd44c --- /dev/null +++ b/src/resources.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import logging +import os +from flask import request +from flask_restful import Resource +from schemas import * + +""" +Flask Restful endpoints +""" + +__author__ = '@tormakris' +__copyright__ = "Copyright 2019, KSZK" +__module_name__ = "endpoints" +__version__text__ = "1" + +LOGGER = logging.getLogger(__name__) + + +class SampleResource(Resource): + """ + Sample endpoint + See: https://swagger.kmlabz.com/?urls.primaryName=Input%20Service + """ + def post(self): + pass diff --git a/src/schemas.py b/src/schemas.py new file mode 100644 index 0000000..726d137 --- /dev/null +++ b/src/schemas.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +from marshmallow import Schema, fields + + +""" +Schemas of input objects +""" + + +__author__ = "@tormakris" +__copyright__ = "Copyright 2019, KSZK" +__module_name__ = "schemas" +__version__text__ = "1" + + +class SampleSchema(Schema): + """ /v1/email/pay - POST + + Parameters: + - date (date) + - device_id (str) + """ + + date = fields.Date(required=True) + device_id = fields.Str(required=True)