Aaron

Merge branch 'develop' into 'master'

Added ResponseHeaders tool;



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