Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
avara1986 committed Apr 17, 2018
2 parents 69a6801 + 580d152 commit f6914c4
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 50 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[run]
include=
*project/*
*pyms/*
omit =
venv/*
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
FROM python:3.6.4-alpine3.7

RUN apk add --update curl gcc g++ libffi-dev openssl-dev python3-dev \
RUN apk add --update curl gcc g++ git libffi-dev openssl-dev python3-dev \
&& rm -rf /var/cache/apk/*
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h

ENV PYTHONUNBUFFERED=1 ENVIRONMENT=pre APP_HOME=/microservice/
RUN mkdir $APP_HOME && adduser -S -D -H python

RUN mkdir $APP_HOME
RUN chown -R python $APP_HOME
WORKDIR $APP_HOME
ADD requirement*.txt $APP_HOME
RUN pip install -r requirements-docker.txt
ADD . $APP_HOME

EXPOSE 5000
USER python

CMD ["gunicorn", "--worker-class", "eventlet", "--workers", "8", "--log-level", "INFO", "--bind", "0.0.0.0:5000", "manage:app"]
8 changes: 0 additions & 8 deletions app.py

This file was deleted.

36 changes: 14 additions & 22 deletions project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

from flasgger import Swagger
from flask import Flask
from flask_opentracing import FlaskTracer
from jaeger_client import Config
from flask_injector import FlaskInjector
from injector import Injector

from project.config import CONFIG
from pyms.healthcheck import healthcheck_blueprint
from pyms.models import db
from pyms.tracer.main import TracerModule

__author__ = "Alberto Vara"
__email__ = "a.vara.1986@gmail.com"
Expand Down Expand Up @@ -47,24 +50,13 @@
}


def init_jaeger_tracer(service_name='your-app-name'):
"""This scaffold is configured whith `Jeager <https://github.com/jaegertracing/jaeger>`_ but you can use
one of the `opentracing tracers <http://opentracing.io/documentation/pages/supported-tracers.html>`_
:param service_name: the name of your application to register in the tracer
:return: opentracing.Tracer
"""
config = Config(config={
'sampler': {'type': 'const', 'param': 1}
}, service_name=service_name)
return config.initialize_tracer()


class PrefixMiddleware(object):
"""Set a prefix path to all routes. This action is needed if you have a stack of microservices and each of them
exist in the same domain but different path. Por example:
* mydomain.com/ms1/
* mydomain.com/ms2/
"""

def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
Expand All @@ -86,20 +78,14 @@ def create_app():
return the app and the database objects.
:return:
"""
from project.models import db

from project.views import views_bp as views_blueprint
from project.views import views_hc as views_hc_blueprint
environment = os.environ.get("ENVIRONMENT", "default")

app = Flask(__name__)
app.config.from_object(CONFIG[environment])
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=app.config["APPLICATION_ROOT"])


if not app.config["TESTING"] and not app.config["DEBUG"]:
j_tracer = init_jaeger_tracer(app.config["APP_NAME"])
FlaskTracer(j_tracer, True, app)

db.init_app(app)

# Initialize Swagger
Expand All @@ -119,7 +105,13 @@ def create_app():

# Initialize Blueprints
app.register_blueprint(views_blueprint)
app.register_blueprint(views_hc_blueprint)
app.register_blueprint(healthcheck_blueprint)

# Inject Modules
if not app.config["TESTING"] and not app.config["DEBUG"]:
injector = Injector([TracerModule(app)])
FlaskInjector(app=app, injector=injector)

with app.test_request_context():
db.create_all()
return app, db
1 change: 0 additions & 1 deletion project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class DevConfig(Config):
"""Configuration to run in local environments"""

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


Expand Down
6 changes: 0 additions & 6 deletions project/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
# encoding: utf-8
from __future__ import absolute_import, print_function, unicode_literals

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
2 changes: 1 addition & 1 deletion project/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from sqlalchemy import Column, Integer, String, DateTime

from project.models import db
from pyms.models import db


def dump_datetime(value: datetime) -> list:
Expand Down
3 changes: 1 addition & 2 deletions project/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
from flask import Blueprint

views_bp = Blueprint('views', __name__, static_url_path='/static')
views_hc = Blueprint('healthcheck', __name__, static_url_path='/static')

from project.views import views, healthcheck
from project.views import views
6 changes: 0 additions & 6 deletions project/views/healthcheck.py

This file was deleted.

3 changes: 2 additions & 1 deletion project/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from flask import request, jsonify

from project.models.models import db, Colors
from pyms.models import db
from project.models.models import Colors
from project.views import views_bp


Expand Down
Empty file added pyms/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions pyms/healthcheck/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# coding=utf-8
from __future__ import unicode_literals, print_function, absolute_import, division

from flask import Blueprint

healthcheck_blueprint = Blueprint('healthcheck', __name__, static_url_path='/static')

from pyms.healthcheck import healthcheck
17 changes: 17 additions & 0 deletions pyms/healthcheck/healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import opentracing
import requests
from flask_opentracing import FlaskTracer

from pyms.healthcheck import healthcheck_blueprint


@healthcheck_blueprint.route('/healthcheck', methods=['GET'])
def healthcheck(tracer: FlaskTracer):
span = tracer.get_span()
headers = {}
tracer._tracer.inject(span, opentracing.Format.HTTP_HEADERS, headers)
result = requests.post(url="http://localhost:8081/oauth/login", data={
"username": "test",
"password": "1234"
}, headers=headers)
return result.content
6 changes: 6 additions & 0 deletions pyms/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# encoding: utf-8
from __future__ import absolute_import, print_function, unicode_literals

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
Empty file added pyms/tracer/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions pyms/tracer/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from flask_opentracing import FlaskTracer
from injector import Module
from jaeger_client import Config


def init_jaeger_tracer(service_name='your-app-name'):
"""This scaffold is configured whith `Jeager <https://github.com/jaegertracing/jaeger>`_ but you can use
one of the `opentracing tracers <http://opentracing.io/documentation/pages/supported-tracers.html>`_
:param service_name: the name of your application to register in the tracer
:return: opentracing.Tracer
"""
config = Config(config={
'sampler': {'type': 'const', 'param': 1}, 'logging': True,
}, service_name=service_name)
return config.initialize_tracer()


class TracerModule(Module):
def __init__(self, app):
self.app = app

def configure(self, binder):
tracer = init_jaeger_tracer(self.app.config["APP_NAME"])
binder.bind(FlaskTracer, to=FlaskTracer(tracer, True, self.app))
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
-e git+https://github.com/jaegertracing/jaeger-client-python.git#egg=jaeger_client
flasgger==0.8.1
Flask==0.12.2
Flask-Injector==0.10.1
Flask-OpenTracing==0.1.8
Flask-Script==2.0.6
Flask-SQLAlchemy==2.3.2
Flask-Testing==0.7.1
SQLAlchemy==1.2.5
requests==2.18.4
jaeger-client==3.7.1

0 comments on commit f6914c4

Please sign in to comment.