Skip to content

Commit 5552cfc

Browse files
committed
Added base flask functions
1 parent 88f27db commit 5552cfc

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

pyms/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIGMAP_FILE_ENVIRONMENT = "CONFIGMAP_FILE"
2+
3+
LOGGER_NAME = "pyms"

pyms/exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Exceptions of the lib. Its useful """
2+
3+
4+
class AttrDoesNotExistException(Exception):
5+
pass
6+
7+
8+
class FileDoesNotExistException(Exception):
9+
pass
10+
11+
12+
class ServiceDoesNotExistException(Exception):
13+
pass
14+
15+
16+
class ConfigDoesNotFoundException(Exception):
17+
pass

pyms/flask/__init__.py

Whitespace-only changes.

pyms/flask/app/utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import logging
2+
import os
3+
from typing import Text
4+
5+
import connexion
6+
from flask_opentracing import FlaskTracer
7+
8+
from pyms.config.conf import get_conf
9+
from pyms.flask.healthcheck import healthcheck_blueprint
10+
from pyms.logger import CustomJsonFormatter
11+
from pyms.tracer.main import init_jaeger_tracer
12+
13+
14+
class Microservice:
15+
service = None
16+
17+
def __init__(self, service: Text, path=__file__):
18+
self.service = service
19+
self.path = os.path.dirname(path)
20+
21+
def create_app(self):
22+
"""Initialize the Flask app, register blueprints and initialize
23+
all libraries like Swagger, database,
24+
the trace system...
25+
return the app and the database objects.
26+
:return:
27+
"""
28+
app = connexion.App(__name__, specification_dir=os.path.join(self.path, 'swagger'))
29+
app.add_api('swagger.yaml',
30+
arguments={'title': 'Swagger Example project'}
31+
)
32+
33+
application = app.app
34+
application.config.from_object(get_conf(service=self.service))
35+
36+
# Initialize Blueprints
37+
application.register_blueprint(healthcheck_blueprint)
38+
39+
# Inject Modules
40+
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)')
41+
if not application.config["TESTING"]:
42+
log_handler = logging.StreamHandler()
43+
44+
tracer = FlaskTracer(init_jaeger_tracer(), True, application)
45+
formatter.add_service_name(application.config["APP_NAME"])
46+
formatter.add_trace_span(tracer)
47+
log_handler.setFormatter(formatter)
48+
application.logger.addHandler(log_handler)
49+
application.logger.setLevel(logging.INFO)
50+
51+
return application

pyms/flask/healthcheck/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Init file
2+
"""
3+
from __future__ import unicode_literals, print_function, absolute_import, division
4+
5+
from flask import Blueprint
6+
7+
healthcheck_blueprint = Blueprint('healthcheck', __name__, static_url_path='/static')
8+
9+
from pyms.flask.healthcheck import healthcheck
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Healthcheck
2+
"""
3+
from pyms.flask.healthcheck import healthcheck_blueprint
4+
5+
6+
@healthcheck_blueprint.route('/healthcheck', methods=['GET'])
7+
def healthcheck():
8+
return "OK"

0 commit comments

Comments
 (0)