model-service/model_service/app.py

71 lines
2.1 KiB
Python
Raw Normal View History

2020-04-13 21:14:06 +02:00
#!/usr/bin/env python3
import os
2020-04-14 13:02:17 +02:00
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
2020-04-13 21:14:06 +02:00
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
2020-11-19 01:28:21 +01:00
from healthcheck import HealthCheck
2020-04-13 21:14:06 +02:00
# import stuff
from model import db
2020-11-19 01:28:21 +01:00
from utils import register_all_error_handlers, storage, health_database_status
2020-04-13 21:14:06 +02:00
# import views
2020-10-02 03:28:40 +02:00
from views import SVMView, CNNView, RootView
2020-04-13 21:14:06 +02:00
2020-04-14 13:02:17 +02:00
# Setup sentry
SENTRY_DSN = os.environ.get("SENTRY_DSN")
if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[FlaskIntegration()],
2020-10-19 22:30:34 +02:00
_experiments={"auto_enabling_integrations": True},
traces_sample_rate=1.0,
2020-04-14 13:02:17 +02:00
send_default_pii=True,
release=os.environ.get('RELEASE_ID', 'test'),
environment=os.environ.get('RELEASEMODE', 'dev')
)
2020-04-13 21:14:06 +02:00
# create flask app
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1)
# configure flask app
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URI', "sqlite://") # Default to memory db
2020-04-14 13:02:17 +02:00
app.config['MINIO_ENDPOINT'] = os.environ['MINIO_ENDPOINT']
app.config['MINIO_ACCESS_KEY'] = os.environ['MINIO_ACCESS_KEY']
app.config['MINIO_SECRET_KEY'] = os.environ['MINIO_SECRET_KEY']
app.config['MINIO_SVM_BUCKET_NAME'] = os.environ.get('MINIO_SVM_BUCKET_NAME', 'svm')
app.config['MINIO_CNN_BUCKET_NAME'] = os.environ.get('MINIO_CNN_BUCKET_NAME', 'cnn')
2020-04-21 18:45:45 +02:00
app.config['MINIO_SECURE'] = os.environ.get('MINIO_SECURE', False)
app.config['MINIO_REGION'] = os.environ.get('MINIO_REGION', None)
2020-04-13 21:14:06 +02:00
# important stuff
app.secret_key = os.environ.get('SECRET_KEY', os.urandom(12))
# disable this for better performance
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# initialize stuff
db.init_app(app)
2020-04-14 17:03:15 +02:00
storage.init_app(app)
2020-11-19 01:28:21 +01:00
health = HealthCheck(app, "/healthz")
2020-04-13 21:14:06 +02:00
2020-10-01 20:20:55 +02:00
@app.before_first_request
def create_db():
2020-04-13 21:14:06 +02:00
db.create_all()
2020-10-01 20:20:55 +02:00
2020-04-13 21:14:06 +02:00
# register error handlers
register_all_error_handlers(app)
# register views
2020-10-02 03:28:40 +02:00
for view in [SVMView, CNNView, RootView]:
2020-07-28 20:19:52 +02:00
view.register(app, trailing_slash=False, route_prefix='/model')
2020-04-13 21:14:06 +02:00
2020-11-19 01:28:21 +01:00
health.add_check(health_database_status)
2020-04-13 21:14:06 +02:00
# start debuggig if needed
if __name__ == "__main__":
app.run(debug=True)