complete post request handler
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2020-03-31 14:46:46 +02:00
parent afd8f4a9fd
commit 355ea26621
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
5 changed files with 49 additions and 10 deletions

View File

@ -9,3 +9,4 @@ filetype
sqlalchemy sqlalchemy
flask_sqlalchemy flask_sqlalchemy
rstr rstr
pike

View File

@ -4,8 +4,9 @@ import sentry_sdk
from flask import Flask from flask import Flask
from flask_restful import Api from flask_restful import Api
from sentry_sdk.integrations.flask import FlaskIntegration from sentry_sdk.integrations.flask import FlaskIntegration
import pika
from config import SENTRY_DSN, RELEASE_ID, RELEASEMODE, PORT, DEBUG from config import *
import db import db
from resources import * from resources import *
@ -27,13 +28,19 @@ if SENTRY_DSN:
environment=RELEASEMODE environment=RELEASEMODE
) )
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOSTNAME}:5432/{POSTGRES_DB}"
api = Api(app) api = Api(app)
db.init_app(app) db.init_app(app)
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
rabbitmq = pika.BlockingConnection(pika.ConnectionParameters(host=RABBITMQ_HOST))
rabbitmq_channel = rabbitmq.channel()
rabbitmq_channel.queue_declare(RABBITMQ_QUEUE)
formatter = logging.Formatter( formatter = logging.Formatter(
fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s" fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s"
) )

View File

@ -20,3 +20,13 @@ DEBUG = os.environ.get("INPUT_SERVICE_DEBUG", True)
SENTRY_DSN = os.environ.get("SENTRY_DSN") SENTRY_DSN = os.environ.get("SENTRY_DSN")
RELEASE_ID = os.environ.get("RELEASE_ID") RELEASE_ID = os.environ.get("RELEASE_ID")
RELEASEMODE = os.environ.get("INPUT_SERVICE_RELEASEMODE") RELEASEMODE = os.environ.get("INPUT_SERVICE_RELEASEMODE")
RABBITMQ_HOST = os.getenv("INPUT_RABBITMQ_HOSTNAME", "localhost")
RABBITMQ_EXCHANGE = os.getenv("INPUT_RABBITMQ_EXCHANGE", "wave-extract")
RABBITMQ_QUEUE = os.getenv("INPUT_RABBITMQ_QUEUE", "wave-extract")
POSTGRES_HOSTNAME = os.getenv("INPUT_POSTGRES_HOSTNAME", "localhost")
POSTGRES_USERNAME = os.getenv("INPUT_POSTGRES_USERNAME", "input-service")
POSTGRES_PASSWORD = os.getenv("INPUT_POSTGRES_PASSWORD", "input-service")
POSTGRES_DB = os.getenv("INPUT_POSTGRES_DB", "input-service")
STORAGE_HOSTNAME = os.getenv("INPUT_STORAGE_HOSTNAME", "localhost:8042")

View File

@ -22,4 +22,4 @@ class SampleMetadata(db.Model):
device_id = db.Column(db.Integer, nullable=False) device_id = db.Column(db.Integer, nullable=False)
device_date = db.Column(db.DateTime, nullable=False) device_date = db.Column(db.DateTime, nullable=False)
tag = db.Column(db.String(32), nullable=False) tag = db.Column(db.String(32), nullable=False, unique=True)

View File

@ -1,13 +1,16 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from app import rabbitmq_channel
import logging import logging
import json import json
import rstr import rstr
from flask_restful import Resource, reqparse from flask_restful import Resource, reqparse
from werkzeug.datastructures import FileStorage from werkzeug.datastructures import FileStorage
import requests
import filetype import filetype
import db import db
from models import SampleMetadata from models import SampleMetadata
from schemas import * from schemas import *
from config import *
""" """
Flask Restful endpoints Flask Restful endpoints
@ -20,7 +23,6 @@ __version__text__ = "1"
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
class SampleResource(Resource): class SampleResource(Resource):
""" """
Sample endpoint Sample endpoint
@ -58,10 +60,29 @@ class SampleResource(Resource):
'message': 'Input JSON schema invalid'}, 417 'message': 'Input JSON schema invalid'}, 417
generated_tag = rstr.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$', 2, 32) generated_tag = rstr.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$', 2, 32)
record = SampleMetadata(device_id=desc['device_id'], device_date=desc['date'], tag=generated_tag) record = SampleMetadata(
device_id=desc['device_id'],
device_date=desc['date'],
tag=generated_tag)
try:
db.session.add(record) db.session.add(record)
requests.post(
f"http://{STORAGE_HOSTNAME}/object",
files={
'tag': (None, generated_tag),
'file': (
'wave.wav',
soundFile,
kind.mime)})
rabbitmq_channel.basic_publish(
exchange=RABBITMQ_EXCHANGE,
routing_key='feature',
body=generated_tag)
except Exception as e:
LOGGER.exception(e)
db.session.rollback()
return {"status": "exception", "message": str(
e), "hint": "DB or downstream service error"}, 569
db.session.commit() db.session.commit()
return {"status": "ok", "message": generated_tag}, 200
# TODO: Pass data and tag to ObjectStorage
# TODO: Pass tag to MQ