Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added Zuul headers
  • Loading branch information
avara1986 committed Mar 23, 2020
commit 19e13f877de6db6f877da62986a6a9a933e5f4d9
37 changes: 1 addition & 36 deletions pyms/flask/app/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pyms.config import get_conf
from pyms.config.conf import validate_conf
from pyms.constants import LOGGER_NAME, CONFIG_BASE
from pyms.flask.app.utils import SingletonMeta, ReverseProxied
from pyms.flask.healthcheck import healthcheck_blueprint
from pyms.flask.services.driver import ServicesManager
from pyms.logger import CustomJsonFormatter
Expand All @@ -15,42 +16,6 @@
logger = logging.getLogger(LOGGER_NAME)


class SingletonMeta(type):
"""
The Singleton class can be implemented in different ways in Python. Some
possible methods include: base class, decorator, metaclass. We will use the
metaclass because it is best suited for this purpose.
"""
_instances = {}
_singleton = True

def __call__(cls, *args, **kwargs):
if cls not in cls._instances or not cls._singleton:
cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
else:
cls._instances[cls].__init__(*args, **kwargs)

return cls._instances[cls]


class ReverseProxied(object):
def __init__(self, app):
self.app = app

def __call__(self, environ, start_response):
script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
if script_name:
environ['SCRIPT_NAME'] = script_name
path_info = environ['PATH_INFO']
if path_info.startswith(script_name):
environ['PATH_INFO'] = path_info[len(script_name):]

scheme = environ.get('HTTP_X_SCHEME', '')
if scheme:
environ['wsgi.url_scheme'] = scheme
return self.app(environ, start_response)


class Microservice(metaclass=SingletonMeta):
"""The class Microservice is the core of all microservices built with PyMS.
You can create a simple microservice such as:
Expand Down
59 changes: 59 additions & 0 deletions pyms/flask/app/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class SingletonMeta(type):
"""
The Singleton class can be implemented in different ways in Python. Some
possible methods include: base class, decorator, metaclass. We will use the
metaclass because it is best suited for this purpose.
"""
_instances = {}
_singleton = True

def __call__(cls, *args, **kwargs):
if cls not in cls._instances or not cls._singleton:
cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
else:
cls._instances[cls].__init__(*args, **kwargs)

return cls._instances[cls]


class ReverseProxied:
"""
Create a Proxy pattern https://microservices.io/patterns/apigateway.html.
You can run the microservice A in your local machine in http://localhost:5000/my-endpoint/
If you deploy your microservice, in some cases this microservice run behind a cluster, a gateway... and this
gateway redirect traffic to the microservice with a specific path like yourdomian.com/my-ms-a/my-endpoint/.
This class understand this path if the gateway send a specific header
"""
def __init__(self, app):
self.app = app

@staticmethod
def _extract_prefix(environ):
"""
Get Path from environment from:
- Traefik with HTTP_X_SCRIPT_NAME https://docs.traefik.io/v2.0/middlewares/headers/
- Nginx and Ingress with HTTP_X_SCRIPT_NAME https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/
- Apache with HTTP_X_SCRIPT_NAME https://stackoverflow.com/questions/55619013/proxy-and-rewrite-to-webapp
- Zuul with HTTP_X_FORWARDER_PREFIX https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html
:param environ:
:return:
"""
# Get path from Traefik, Nginx and Apache
path = environ.get('HTTP_X_SCRIPT_NAME', '')
if not path:
# Get path from Zuul
path = environ.get('HTTP_X_FORWARDER_PREFIX', '')
return path

def __call__(self, environ, start_response):
script_name = self._extract_prefix(environ)
if script_name:
environ['SCRIPT_NAME'] = script_name
path_info = environ['PATH_INFO']
if path_info.startswith(script_name):
environ['PATH_INFO'] = path_info[len(script_name):]

scheme = environ.get('HTTP_X_SCHEME', '')
if scheme:
environ['wsgi.url_scheme'] = scheme
return self.app(environ, start_response)