2021-12-10 21:59:56 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sentry_sdk
|
|
|
|
from sentry_sdk.integrations.flask import FlaskIntegration
|
|
|
|
from flask import Flask
|
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
|
|
|
|
|
|
# import stuff
|
2021-12-10 23:04:41 +01:00
|
|
|
from utils import register_all_error_handlers, register_health_checks, redis_client
|
2021-12-10 21:59:56 +01:00
|
|
|
|
|
|
|
# import views
|
2021-12-10 22:02:30 +01:00
|
|
|
from views import AssignmentView
|
2021-12-10 21:59:56 +01:00
|
|
|
|
|
|
|
from config import Config
|
|
|
|
|
|
|
|
# Tracing stuffs
|
|
|
|
import jaeger_client
|
|
|
|
import opentracing
|
|
|
|
from flask_opentracing import FlaskTracing
|
|
|
|
|
|
|
|
# Setup sentry
|
|
|
|
if Config.SENTRY_DSN:
|
|
|
|
sentry_sdk.init(
|
|
|
|
dsn=Config.SENTRY_DSN,
|
|
|
|
integrations=[FlaskIntegration()],
|
|
|
|
traces_sample_rate=0,
|
|
|
|
send_default_pii=True,
|
|
|
|
release=Config.RELEASE_ID,
|
|
|
|
environment=Config.RELEASEMODE
|
|
|
|
)
|
|
|
|
|
|
|
|
# create flask app
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_object(Config)
|
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1)
|
|
|
|
|
|
|
|
# init stuff
|
2021-12-10 23:04:41 +01:00
|
|
|
redis_client.init_app(app)
|
2021-12-10 21:59:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
# register error handlers
|
|
|
|
register_all_error_handlers(app)
|
|
|
|
|
|
|
|
|
|
|
|
# Setup tracing
|
|
|
|
def initialize_tracer():
|
|
|
|
app.logger.info("Initializing jaeger...")
|
2021-12-10 23:08:58 +01:00
|
|
|
jaeger_cfg = jaeger_client.Config(config={}, service_name='birb-scheduler-teller', validate=True)
|
2021-12-10 21:59:56 +01:00
|
|
|
tracer = jaeger_cfg.initialize_tracer()
|
|
|
|
return tracer
|
|
|
|
|
|
|
|
|
|
|
|
tracing = FlaskTracing(initialize_tracer, True, app)
|
|
|
|
|
|
|
|
# register views
|
2021-12-10 22:02:30 +01:00
|
|
|
for view in [AssignmentView]:
|
2021-12-10 21:59:56 +01:00
|
|
|
view.register(app, trailing_slash=False)
|
|
|
|
|
|
|
|
register_health_checks(app)
|
|
|
|
|
|
|
|
# start debugging if needed
|
|
|
|
if __name__ == "__main__":
|
2021-12-10 23:51:00 +01:00
|
|
|
app.logger.info(f"Hard default: {Config.DEFAULT_WHILE_NOT_DEFINED}")
|
2021-12-10 21:59:56 +01:00
|
|
|
app.run(debug=True)
|
|
|
|
else:
|
|
|
|
import os
|
|
|
|
|
|
|
|
if "gunicorn" in os.environ.get("SERVER_SOFTWARE", ""):
|
|
|
|
import logging
|
|
|
|
|
|
|
|
gunicorn_logger = logging.getLogger('gunicorn.error')
|
|
|
|
app.logger.handlers = gunicorn_logger.handlers
|
|
|
|
app.logger.setLevel(gunicorn_logger.level)
|
|
|
|
|
|
|
|
jaeger_logger = logging.getLogger('jaeger_tracing')
|
|
|
|
jaeger_logger.handlers = gunicorn_logger.handlers
|
|
|
|
jaeger_logger.setLevel(gunicorn_logger.level)
|
|
|
|
|
|
|
|
app.logger.info("Gunicorn environment detected!")
|
|
|
|
else:
|
|
|
|
app.logger.info("Not gunicorn")
|
2021-12-10 23:51:00 +01:00
|
|
|
|
|
|
|
app.logger.info(f"Hard default: {Config.DEFAULT_WHILE_NOT_DEFINED}")
|