33from typing import Text
44
55import connexion
6+ from flask import Flask
67from flask_opentracing import FlaskTracer
78
89from pyms .config .conf import get_conf
9- from pyms .flask .app .swagger import Swagger
1010from pyms .flask .healthcheck import healthcheck_blueprint
11+ from pyms .flask .services .driver import ServicesManager
1112from pyms .logger import CustomJsonFormatter
1213from pyms .tracer .main import init_jaeger_tracer
1314
@@ -19,8 +20,13 @@ class Microservice:
1920 def __init__ (self , service : Text , path = __file__ ):
2021 self .service = service
2122 self .path = os .path .dirname (path )
22- self .swagger = Swagger ()
2323 self .config = get_conf (service = self .service )
24+ self .init_services ()
25+
26+ def init_services (self ):
27+ service_manager = ServicesManager ()
28+ for service_name , service in service_manager .get_services ():
29+ setattr (self , service_name , service )
2430
2531 def init_libs (self ):
2632 return self .application
@@ -39,22 +45,29 @@ def init_logger(self):
3945 self .application .logger .propagate = False
4046 self .application .logger .setLevel (logging .INFO )
4147
48+ def init_app (self ):
49+ if getattr (self , "swagger" , False ):
50+ app = connexion .App (__name__ , specification_dir = os .path .join (self .path , self .swagger .path ))
51+ app .add_api (self .swagger .file ,
52+ arguments = {'title' : self .config .APP_NAME },
53+ base_path = self .config .APPLICATION_ROOT
54+ )
55+
56+ application = app .app
57+ application ._connexion_app = app
58+ else :
59+ application = Flask (__name__ )
60+
61+ return application
62+
4263 def create_app (self ):
4364 """Initialize the Flask app, register blueprints and initialize
4465 all libraries like Swagger, database,
4566 the trace system...
4667 return the app and the database objects.
4768 :return:
4869 """
49-
50- app = connexion .App (__name__ , specification_dir = os .path .join (self .path , self .swagger .path ))
51- app .add_api (self .swagger .file ,
52- arguments = {'title' : self .config .APP_NAME },
53- base_path = self .config .APPLICATION_ROOT
54- )
55-
56- self .application = app .app
57- self .application ._connexion_app = app
70+ self .application = self .init_app ()
5871 self .application .config .from_object (self .config )
5972 self .application .tracer = None
6073
0 commit comments