Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions pyms/flask/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .create_app import Microservice
from .create_app import Microservice, microservice
from .create_config import config


__all__ = ['Microservice', 'config']
__all__ = ['Microservice', 'config', 'microservice']
10 changes: 10 additions & 0 deletions pyms/flask/app/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pyms.crypt.driver import CryptResource
from pyms.flask.app.utils import SingletonMeta, ReverseProxied
from pyms.flask.healthcheck import healthcheck_blueprint
from pyms.flask.configreload import configreload_blueprint
from pyms.flask.services.driver import ServicesResource, DriverService
from pyms.logger import CustomJsonFormatter
from pyms.utils import check_package_exists, import_from
Expand Down Expand Up @@ -218,6 +219,7 @@ def create_app(self):

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

self.init_libs()
self.add_error_handlers()
Expand Down Expand Up @@ -245,3 +247,11 @@ def add_error_handler(self, code_or_exception, handler):
:param handler: callback for error handler
"""
self.application.connexion_app.add_error_handler(code_or_exception, handler)


def microservice():
"""The behavior of this function is to access to the microservice outer the scope of flask context, to prevent to
raise a `'working outside of application context`
:return:
"""
return Microservice()
4 changes: 4 additions & 0 deletions pyms/flask/configreload/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .configreload import configreload_blueprint


__all__ = ['configreload_blueprint']
16 changes: 16 additions & 0 deletions pyms/flask/configreload/configreload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import unicode_literals, print_function, absolute_import, division
from flask import Blueprint

configreload_blueprint = Blueprint('configreload', __name__, static_url_path='/static')


@configreload_blueprint.route('/reload_config', methods=['GET'])
def reloadconfig():
"""
Reread configuration from file.
:return:
"""
# pylint: disable=import-outside-toplevel
from pyms.flask.app import microservice
microservice().reload_conf()
return "OK"
2 changes: 1 addition & 1 deletion pyms/flask/healthcheck/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@healthcheck_blueprint.route('/healthcheck', methods=['GET'])
def healthcheck():
"""Set a healtcheck to help other service to discover this microservice, like Kubernetes, AWS ELB, etc.
"""Set a healthcheck to help other service to discover this microservice, like Kubernetes, AWS ELB, etc.
:return:
"""
return "OK"
5 changes: 5 additions & 0 deletions tests/config-tests-reload1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pyms:
config:
DEBUG: true
TESTING: true
APP_NAME: "reload1"
5 changes: 5 additions & 0 deletions tests/config-tests-reload2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pyms:
config:
DEBUG: true
TESTING: true
APP_NAME: "reload2"
25 changes: 25 additions & 0 deletions tests/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ def test_reverse_proxy_zuul(self):
self.assertEqual(200, response.status_code)


class ReloadTests(unittest.TestCase):
"""
Tests for configreload endpoints
"""

file1 = "config-tests-reload1.yml"
file2 = "config-tests-reload2.yml"

def setUp(self):
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(os.path.dirname(os.path.abspath(__file__)),
self.file1)
ms = MyMicroservice(path=__file__)
self.app = ms.create_app()
self.client = self.app.test_client()
self.assertEqual("reload1", self.app.config["APP_NAME"])

def test_configreload(self):
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(os.path.dirname(os.path.abspath(__file__)),
self.file2)
response = self.client.get('/reload_config')
self.assertEqual(b"OK", response.data)
self.assertEqual(200, response.status_code)
self.assertEqual("reload2", config()["APP_NAME"])


class MicroserviceTest(unittest.TestCase):
"""
Tests for Singleton
Expand Down