Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions pyms/flask/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

class Microservice:
service = None
application = None

def __init__(self, service: Text, path=__file__):
self.service = service
self.path = os.path.dirname(path)

def init_libs(self, app):
return app
def init_libs(self):
return self.application

def create_app(self):
"""Initialize the Flask app, register blueprints and initialize
Expand All @@ -35,23 +36,42 @@ def create_app(self):
base_path=config.APPLICATION_ROOT
)

application = app.app
application.config.from_object(get_conf(service=self.service))
application.tracer = None
self.application = app.app
self.application._connexion_app = app
self.application.config.from_object(get_conf(service=self.service))
self.application.tracer = None

# Initialize Blueprints
application.register_blueprint(healthcheck_blueprint)
self.init_libs(application)
self.application.register_blueprint(healthcheck_blueprint)

self.init_libs()
self.add_error_handlers()

# Inject Modules
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)')
if not application.config["TESTING"]:
if not self.application.config["TESTING"]:
log_handler = logging.StreamHandler()

application.tracer = FlaskTracer(init_jaeger_tracer(), True, application)
formatter.add_service_name(application.config["APP_NAME"])
formatter.add_trace_span(application.tracer)
self.application.tracer = FlaskTracer(init_jaeger_tracer(), True, self.application)
formatter.add_service_name(self.application.config["APP_NAME"])
formatter.add_trace_span(self.application.tracer)
log_handler.setFormatter(formatter)
application.logger.addHandler(log_handler)
application.logger.setLevel(logging.INFO)
self.application.logger.addHandler(log_handler)
self.application.logger.setLevel(logging.INFO)

return self.application

def add_error_handlers(self):
"""Subclasses will override this method in order to add specific error handlers. This should be done with
calls to add_error_handler method.
"""
pass

def add_error_handler(self, code_or_exception, handler):
"""Add custom handler for an error code or exception in the connexion app.

:param code_or_exception: HTTP error code or exception
:param handler: callback for error handler
"""

return application
self.application._connexion_app.add_error_handler(code_or_exception, handler)