Skip to content

Commit 4a8532e

Browse files
committed
Merge branch 'develop'
2 parents 2e587e3 + c40a251 commit 4a8532e

File tree

16 files changed

+136
-16
lines changed

16 files changed

+136
-16
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ celerybeat-schedule
8686
.venv
8787
venv/
8888
ENV/
89-
89+
venv2
9090
# Spyder project settings
9191
.spyderproject
9292
.spyproject
@@ -104,4 +104,4 @@ ENV/
104104

105105
pylintReport.txt
106106
db.sqlite3
107-
_build
107+
_build

project/__init__.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
# encoding: utf-8
22

3+
import logging
34
import os
45

56
from flasgger import Swagger
67
from flask import Flask
8+
from flask_injector import FlaskInjector
9+
from injector import Injector
710

811
from 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

1421
ENVIRONMENT = os.environ.get("ENVIRONMENT", "default")
1522

23+
logger = logging.getLogger('jaeger_tracing')
24+
logger.setLevel(logging.DEBUG)
25+
1626
SWAGGER_CONFIG = {
1727
"headers": [
1828
],
@@ -46,24 +56,33 @@
4656

4757

4858
class 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

6581
def 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

project/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class DevConfig(Config):
2828
"""Configuration to run in local environments"""
2929

3030
DEBUG = True
31-
TESTING = True
3231
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(BASE_DIR, "db.sqlite3")
3332

3433

project/models/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
# encoding: utf-8
2-
from __future__ import absolute_import, print_function, unicode_literals
3-
4-
from flask_sqlalchemy import SQLAlchemy
5-
6-
db = SQLAlchemy()

project/models/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from flask_login import UserMixin
77
from sqlalchemy import Column, Integer, String, Boolean
88

9-
from project.models import db
9+
from pyms.models import db
1010

1111

1212
class User(db.Model, UserMixin):

project/views/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from flask import request, jsonify
55
from flask_jwt import jwt_required, current_identity
66

7-
from project.models.models import User, db
87
from project.views import views_bp
98
from project.views.oauth import jwt, authenticate
109

@@ -34,6 +33,7 @@ def login():
3433
400:
3534
description: User login failed.
3635
"""
36+
# current_app.logger.info("{}".format(request.headers))
3737
try:
3838
username = request.form.get("username")
3939
password = request.form.get("password")
@@ -53,6 +53,7 @@ def login():
5353
except UserNotFoundException:
5454
resp = jsonify({"message": "Bad username and/or password"})
5555
resp.status_code = 401
56+
print(resp.headers)
5657
return resp
5758

5859

@@ -73,4 +74,4 @@ def protected():
7374
resp = jsonify({"protected": "{}".format(current_identity)})
7475
resp.status_code = 200
7576

76-
return resp
77+
return resp

pyms/__init__.py

Whitespace-only changes.

pyms/healthcheck/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# coding=utf-8
2+
from __future__ import unicode_literals, print_function, absolute_import, division
3+
4+
from flask import Blueprint
5+
6+
healthcheck_blueprint = Blueprint('healthcheck', __name__, static_url_path='/static')
7+
8+
from pyms.healthcheck import healthcheck

pyms/healthcheck/healthcheck.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pyms.healthcheck import healthcheck_blueprint
2+
3+
4+
@healthcheck_blueprint.route('/healthcheck', methods=['GET'])
5+
def healthcheck():
6+
return "OK"

pyms/logger/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from pyms.logger.logger import CustomJsonFormatter

0 commit comments

Comments
 (0)