Commit ca01360da83091581bea682ac47ae5a14abd6030
1 parent
80a8857b
Exists in
master
implementado o tratamento para requisi??es http condicionais (cache)
Showing
1 changed file
with
12 additions
and
3 deletions
Show diff stats
app/barrabrasil.py
| 1 | -from flask import Flask, url_for, render_template, request, make_response | |
| 1 | +from flask import Flask, url_for, render_template, request, Response, make_response | |
| 2 | +import hashlib | |
| 2 | 3 | app = Flask(__name__) |
| 3 | 4 | |
| 4 | 5 | @app.route('/') |
| ... | ... | @@ -25,8 +26,16 @@ def barra(): |
| 25 | 26 | 'verde': '#00500F', |
| 26 | 27 | } |
| 27 | 28 | cor = paleta.get(nome_cor, '#004B82') |
| 28 | - resposta = make_response(render_template('barra-brasil.js', cor=cor)) | |
| 29 | - resposta.headers['Content-type'] = 'application/javascript' | |
| 29 | + conteudo = render_template('barra-brasil.js', cor=cor) | |
| 30 | + etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest() | |
| 31 | + if request.if_none_match and \ | |
| 32 | + etag in request.if_none_match: | |
| 33 | + resposta = Response(status=304) | |
| 34 | + else: # nao esta em cache do navegador | |
| 35 | + resposta = make_response(conteudo) | |
| 36 | + resposta.set_etag(etag) | |
| 37 | + resposta.headers['Content-type'] = 'application/javascript' | |
| 38 | + resposta.headers['Cache-control'] = 'max-age: 3600' | |
| 30 | 39 | return resposta |
| 31 | 40 | |
| 32 | 41 | if __name__ == '__main__': | ... | ... |