All checks were successful
continuous-integration/drone/push Build is passing
84 lines
2.2 KiB
Python
84 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
import logging
|
|
from flask import Flask
|
|
from flask_restful import Api
|
|
import sentry_sdk
|
|
from sentry_sdk.integrations.flask import FlaskIntegration
|
|
|
|
from config import *
|
|
from db import db
|
|
from marshm import ma
|
|
from fpika import fpika
|
|
from resources import SampleResource, SampleParameterResource
|
|
|
|
"""
|
|
Main Flask RESTful API
|
|
"""
|
|
|
|
__author__ = "@tormakris"
|
|
__copyright__ = "Copyright 2020, Birbnetes Team"
|
|
__module_name__ = "app"
|
|
__version__text__ = "1"
|
|
|
|
if SENTRY_DSN:
|
|
sentry_sdk.init(
|
|
dsn=SENTRY_DSN,
|
|
integrations=[FlaskIntegration()],
|
|
traces_sample_rate=1.0,
|
|
send_default_pii=True,
|
|
release=RELEASE_ID,
|
|
environment=RELEASEMODE,
|
|
_experiments={"auto_enabling_integrations": True}
|
|
)
|
|
|
|
app = Flask(__name__)
|
|
app.config[
|
|
'SQLALCHEMY_DATABASE_URI'] = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOSTNAME}:5432/{POSTGRES_DB}"
|
|
app.config['EXCHANGE_NAME'] = RABBITMQ_EXCHANGE
|
|
app.config['FLASK_PIKA_PARAMS'] = {'host': RABBITMQ_HOST,
|
|
'username': RABBITMQ_USERNAME,
|
|
'password': RABBITMQ_PASSWORD,
|
|
'port': 5672,
|
|
'virtual_host': '/'}
|
|
app.config['FLASK_PIKA_POOL_PARAMS'] = {'pool_size': 4,
|
|
'pool_recycle': 60}
|
|
api = Api(app)
|
|
db.init_app(app)
|
|
ma.init_app(app)
|
|
fpika.init_app(app)
|
|
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
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, "/sample")
|
|
api.add_resource(SampleParameterResource, '/sample/<tag>')
|
|
|
|
|
|
@app.before_first_request
|
|
def before_first_request():
|
|
ch = fpika.channel()
|
|
ch.exchange_declare(exchange=RABBITMQ_EXCHANGE,
|
|
exchange_type='fanout',
|
|
durable=True,
|
|
auto_delete=True)
|
|
fpika.return_channel(ch)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(
|
|
debug=bool(DEBUG),
|
|
host="0.0.0.0",
|
|
port=int(PORT),
|
|
)
|