web: add Cache-Control to static files

This change add the Cache-Control header to static files' response.

Change-Id: Ibdf1c35bad378507162d807cf5acdf13fc3fab88
This commit is contained in:
Tristan Cacqueray 2017-11-29 05:36:55 +00:00
parent fe07e42391
commit 3c2d39dd86
4 changed files with 18 additions and 2 deletions

View File

@ -601,6 +601,12 @@ sections of ``zuul.conf`` are used by the web server:
Base URL on which the websocket service is exposed, if different
than the base URL of the web app.
.. attr:: static_cache_expiry
:default: 3600
The Cache-Control max-age response header value for static files served
by the zuul-web. Set to 0 during development to disable Cache-Control.
Operation
~~~~~~~~~

View File

@ -37,6 +37,7 @@ trusted_rw_paths=/opt/zuul-logs
[web]
listen_address=127.0.0.1
port=9000
static_cache_expiry=0
[webapp]
listen_address=0.0.0.0

View File

@ -55,6 +55,9 @@ class WebServer(zuul.cmd.ZuulApp):
'web', 'listen_address',
'127.0.0.1')
params['listen_port'] = get_default(self.config, 'web', 'port', 9000)
params['static_cache_expiry'] = get_default(self.config, 'web',
'static_cache_expiry',
3600)
params['gear_server'] = get_default(self.config, 'gearman', 'server')
params['gear_port'] = get_default(self.config, 'gearman', 'port', 4730)
params['ssl_key'] = get_default(self.config, 'gearman', 'ssl_key')

View File

@ -200,11 +200,13 @@ class ZuulWeb(object):
def __init__(self, listen_address, listen_port,
gear_server, gear_port,
ssl_key=None, ssl_cert=None, ssl_ca=None):
ssl_key=None, ssl_cert=None, ssl_ca=None,
static_cache_expiry=3600):
self.listen_address = listen_address
self.listen_port = listen_port
self.event_loop = None
self.term = None
self.static_cache_expiry = static_cache_expiry
# instanciate handlers
self.rpc = zuul.rpcclient.RPCClient(gear_server, gear_port,
ssl_key, ssl_cert, ssl_ca)
@ -228,7 +230,11 @@ class ZuulWeb(object):
fp = os.path.join(STATIC_DIR, "index.html")
elif request.path.endswith("status.html"):
fp = os.path.join(STATIC_DIR, "status.html")
return web.FileResponse(fp)
headers = {}
if self.static_cache_expiry:
headers['Cache-Control'] = "public, max-age=%d" % \
self.static_cache_expiry
return web.FileResponse(fp, headers=headers)
def run(self, loop=None):
"""