Aaron

Merge branch 'develop' into 'master'

Added ResponseHeaders tool;



See merge request !10
... ... @@ -14,7 +14,7 @@ if __name__ == '__main__':
# Import web framework modules
from src.plugins import SQLAlchemyPlugin
from src.tools import Jinja2Tool, SQLAlchemyTool, DebugTool
from src.tools import Jinja2Tool, SQLAlchemyTool, DebugTool, ResponseHeaders
# Load config before CherryPy
p = Parser()
... ... @@ -46,6 +46,7 @@ if __name__ == '__main__':
cherrypy.tools.render = Jinja2Tool(conf['path']['templates'])
cherrypy.tools.db = SQLAlchemyTool()
cherrypy.tools.debug = DebugTool()
cherrypy.tools.response_headers = ResponseHeaders(conf['response-headers'])
# Import application modules via config file
for classname, pair in conf['apps'].items():
... ...
... ... @@ -35,3 +35,6 @@ port: 1234
name: 'DB_NAME'
user: 'DB_USER'
pass: 'DB_PORT'
[response-headers]
Access-Control-Allow-Origin: 'https://example.com'
... ...
from .debugtool import DebugTool
from .jinja2tool import Jinja2Tool
from .sqlalchemytool import SQLAlchemyTool
from .responseheaders import ResponseHeaders
... ...
import cherrypy
class ResponseHeaders(cherrypy.Tool):
def __init__(self, headers):
""" This Tool adds arbitrary headers to a response object.
These headers are defined in the site configuration under the [response-headers]
section, and passed into this class upon instantiation in Serve.py
"""
self.headers = headers
cherrypy.Tool.__init__(self, 'before_finalize', self.append_headers, priority=20)
def _setup(self):
cherrypy.Tool._setup(self)
def append_headers(self):
for header, value in self.headers.items():
cherrypy.response.headers[header] = value
... ...