Package: wsgi-microservice-middleware
WSGI Microservice MiddlewareThis little library contains middlewares to help quickly turn wsgi apps (e.g., flask, django, bottle, tornado, pyramid) into production-ready microservices for integration into a kubernetes cluster.  These middelewares set up to be configurable warfrom environment variables in accordance with the twelve-factor app methodology.Middlewares in this package include:CORSRequest IdThis project spun out of development for the Presalytics as way to quickly port code across various microserivces built on wsgi-supported frameworks.View on GithubInstallationThis project requires python 3.5+. It is best installed fromt he pypi package repository via pip.pipinstallwsgi_microservice_middlewareThe latest branch of this package can also be installed from git:pipinstallgit+https://github.com/presalytics/WSGI-Microservice-MiddlewareConfigurationPackage configuration is best done by adding configuration values via environment varialble.  This package uses theenvironspackage to load the following values from the system environment or a.envfile in the the working directory:CORS_ALLOWED: Comma-separated string of domain names that will from which CORS requests can be made.  Is empty by defaultREQUEST_ID_HEADER: The http request header containing a the request-id to including in logs. If not supplied, defaults toX-Request-Id.LOG_TOKENS: InstructsRequestIdMiddlewareto add Bearer Authentication tokens to request logs when available. Defaults to True.UsageThese middleware are applied by wrapping the wsgi application object in you code by the middleware class. Examples below:Flask:# app.pyfromflaskimportFlaskfromwsgi_microservice_middleware.corsimportCORSMiddlewareapp=Flask(__name__)app.wsgi_app=CORSMiddleware(app.wsgi_app)# Middleware applied hereapp.run(...)Django:# wsgi.pyfromdjango.core.wsgiimportget_wsgi_applicationfromwsgi_microservice_middleware.request_idimportRequestIdMiddlewareapplication=get_wsgi_application()application=RequestIdMiddleware(application)# Middleware applied hereModulesCORSAdds CORS headers to the responses of request that originate from domains in the
CORS_ALLOWED environment variable.  CORS_ALLOWED should reside in the environment as a
comma-separated string of domain names.Request IdImplements Request Id handling for requests that need to be tracked accross multiple microservices and searched in log records.
TheRequestIdMiddlewareclass.To extend the request Id to your application logs, incorporate theRequestIdFilterinto your logging configuration. the best way to do this
is to uselogging.config.dictConfigto apply the filter to all of your handlers:# log_config.pyfromlogging.configimportdictConfigfromwsgi_microservice_middleware.request_idimportRequestIdFilterdictConfig({'version':1,'filters':{'request_id_filter':{'()':RequestIdFilter,# RequestIdFilter.filter(self, record) called with each log entry}},'formatters':{'default':{'format':'%(asctime)s-%(threadName)s-%(name)s-%(levelname)s-%(request_id)s-%(message)s',}},'handlers':{'wsgi':{'class':'logging.StreamHandler','formatter':'default','filters':['request_id_filter']# add this filter to each handler},},'root':{'level':'DEBUG','handlers':['wsgi']}})If your microservice makes requests to other microservices, you can call thecurrent_request_id()method to get the current request id and incorporate it into your request headers.ContributingWe'd love your help! Open an issue at thegithub repo.  Or even better, you can fork the repo and we'll merge your improvements.