Skip to content

Commit 810bb86

Browse files
authored
Config reload endpoint (#174)
* Fix typo * Support live reload * Fix linters
1 parent 6b5bc4b commit 810bb86

File tree

7 files changed

+57
-1
lines changed

7 files changed

+57
-1
lines changed

pyms/flask/app/create_app.py

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pyms.crypt.driver import CryptResource
1111
from pyms.flask.app.utils import SingletonMeta, ReverseProxied
1212
from pyms.flask.healthcheck import healthcheck_blueprint
13+
from pyms.flask.configreload import configreload_blueprint
1314
from pyms.flask.services.driver import ServicesResource, DriverService
1415
from pyms.logger import CustomJsonFormatter
1516
from pyms.utils import check_package_exists, import_from
@@ -218,6 +219,7 @@ def create_app(self):
218219

219220
# Initialize Blueprints
220221
self.application.register_blueprint(healthcheck_blueprint)
222+
self.application.register_blueprint(configreload_blueprint)
221223

222224
self.init_libs()
223225
self.add_error_handlers()

pyms/flask/configreload/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .configreload import configreload_blueprint
2+
3+
4+
__all__ = ['configreload_blueprint']
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import unicode_literals, print_function, absolute_import, division
2+
from flask import Blueprint
3+
from flask import current_app
4+
5+
configreload_blueprint = Blueprint('configreload', __name__, static_url_path='/static')
6+
7+
8+
@configreload_blueprint.route('/reload-config', methods=['POST'])
9+
def reloadconfig():
10+
"""
11+
Reread configuration from file.
12+
:return:
13+
"""
14+
current_app.ms.reload_conf()
15+
return "OK"

pyms/flask/healthcheck/healthcheck.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@healthcheck_blueprint.route('/healthcheck', methods=['GET'])
99
def healthcheck():
10-
"""Set a healtcheck to help other service to discover this microservice, like Kubernetes, AWS ELB, etc.
10+
"""Set a healthcheck to help other service to discover this microservice, like Kubernetes, AWS ELB, etc.
1111
:return:
1212
"""
1313
return "OK"

tests/config-tests-reload1.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pyms:
2+
config:
3+
DEBUG: true
4+
TESTING: true
5+
APP_NAME: "reload1"

tests/config-tests-reload2.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pyms:
2+
config:
3+
DEBUG: true
4+
TESTING: true
5+
APP_NAME: "reload2"

tests/test_flask.py

+25
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,31 @@ def test_reverse_proxy_zuul(self):
8181
self.assertEqual(200, response.status_code)
8282

8383

84+
class ReloadTests(unittest.TestCase):
85+
"""
86+
Tests for configreload endpoints
87+
"""
88+
89+
file1 = "config-tests-reload1.yml"
90+
file2 = "config-tests-reload2.yml"
91+
92+
def setUp(self):
93+
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(os.path.dirname(os.path.abspath(__file__)),
94+
self.file1)
95+
ms = MyMicroservice(path=__file__)
96+
self.app = ms.create_app()
97+
self.client = self.app.test_client()
98+
self.assertEqual("reload1", self.app.config["APP_NAME"])
99+
100+
def test_configreload(self):
101+
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(os.path.dirname(os.path.abspath(__file__)),
102+
self.file2)
103+
response = self.client.post('/reload-config')
104+
self.assertEqual(b"OK", response.data)
105+
self.assertEqual(200, response.status_code)
106+
self.assertEqual("reload2", config()["APP_NAME"])
107+
108+
84109
class MicroserviceTest(unittest.TestCase):
85110
"""
86111
Tests for Singleton

0 commit comments

Comments
 (0)