11# encoding: utf-8
22
3+ import logging
34import os
45
56from flasgger import Swagger
67from flask import Flask
8+ from flask_injector import FlaskInjector
9+ from injector import Injector
710
811from project .config import CONFIG
12+ from pyms .healthcheck import healthcheck_blueprint
13+ from pyms .logger import CustomJsonFormatter
14+ from pyms .models import db
15+ from pyms .tracer .main import TracerModule
916
1017__author__ = "Alberto Vara"
1118__email__ = "a.vara.1986@gmail.com"
1219__version__ = "0.0.1"
1320
1421ENVIRONMENT = os .environ .get ("ENVIRONMENT" , "default" )
1522
23+ logger = logging .getLogger ('jaeger_tracing' )
24+ logger .setLevel (logging .DEBUG )
25+
1626SWAGGER_CONFIG = {
1727 "headers" : [
1828 ],
4656
4757
4858class PrefixMiddleware (object ):
59+ """Set a prefix path to all routes. This action is needed if you have a stack of microservices and each of them
60+ exist in the same domain but different path. Por example:
61+ * mydomain.com/ms1/
62+ * mydomain.com/ms2/
63+ """
4964
5065 def __init__ (self , app , prefix = '' ):
5166 self .app = app
5267 self .prefix = prefix
5368
5469 def __call__ (self , environ , start_response ):
55-
5670 if environ ['PATH_INFO' ].startswith (self .prefix ):
5771 environ ['PATH_INFO' ] = environ ['PATH_INFO' ][len (self .prefix ):]
5872 environ ['SCRIPT_NAME' ] = self .prefix
5973 return self .app (environ , start_response )
74+ elif environ ['PATH_INFO' ].startswith ("/healthcheck" ):
75+ return self .app (environ , start_response )
6076 else :
6177 start_response ('404' , [('Content-Type' , 'text/plain' )])
6278 return ["This url does not belong to the app." .encode ()]
6379
6480
6581def create_app ():
66- from project .models import db
82+ """Initialize the Flask app, register blueprints and intialize all libraries like Swagger, database, the trace system...
83+ return the app and the database objects.
84+ :return:
85+ """
6786 from project .views import views_bp as views_blueprint
6887 from project .views .oauth import jwt , bcrypt
6988 environment = os .environ .get ("ENVIRONMENT" , "default" )
@@ -90,7 +109,24 @@ def create_app():
90109 )
91110 Swagger (app , config = SWAGGER_CONFIG )
92111
112+ # Initialize Blueprints
93113 app .register_blueprint (views_blueprint )
114+ app .register_blueprint (healthcheck_blueprint )
115+
116+ # Inject Modules
117+ # Inject Modules
118+ if not app .config ["TESTING" ] and not app .config ["DEBUG" ]:
119+ log_handler = logging .StreamHandler ()
120+ formatter = CustomJsonFormatter ('(timestamp) (level) (name) (module) (funcName) (lineno) (message)' )
121+ formatter .add_service_name (app .config ["APP_NAME" ])
122+ tracer = TracerModule (app )
123+ injector = Injector ([tracer ])
124+ FlaskInjector (app = app , injector = injector )
125+ formatter .add_trace_span (tracer .tracer )
126+ log_handler .setFormatter (formatter )
127+ app .logger .addHandler (log_handler )
128+ app .logger .setLevel (logging .INFO )
129+
94130 with app .test_request_context ():
95131 db .create_all ()
96132 return app , db
0 commit comments