barrabrasil.py 7.35 KB
from flask import Flask, url_for, render_template, request, Response, make_response
from config import Config
import hashlib, zlib, datetime, os

# Criar Key e certificado
# openssl genrsa 1024 > ssl.key
# openssl req -new -x509 -nodes -sha1 -days 365 -key ssl.key > ssl.cert

#from OpenSSL import SSL
#ctx = SSL.Context(SSL.SSLv23_METHOD)
#ctx.use_privatekey_file('ssl.key')
#ctx.use_certificate_file('ssl.cert')

app = Flask(__name__,static_url_path="/fonts", static_folder="fonts")

if not app.debug:
	import logging
	from logging.handlers import SMTPHandler
	f = app.open_resource('config')
	cfg = Config(f)
	mail_handler = SMTPHandler(cfg.server, cfg.email, cfg.ADMINS, cfg.subject)
   	mail_handler.setLevel(logging.ERROR)
	app.logger.addHandler(mail_handler)


@app.route('/')
def pagina_teste():
    try:
        with app.open_resource('templates/exemplo.html') as f:
            conteudo = f.read().decode('utf-8')
        resposta = make_response(conteudo)
        resposta.headers['Content-type'] = 'text/html; charset=utf-8'
        return resposta
    except IOError:
        return make_response("<h1>403 Forbidden</h1>", 403)

@app.route('/exemplo_antiga.html')
def pagina_teste_antiga():
    try:
        with app.open_resource('templates/exemplo_antiga.html') as f:
            conteudo = f.read().decode('utf-8')
        resposta = make_response(conteudo)
        resposta.headers['Content-type'] = 'text/html; charset=utf-8'
        return resposta
    except IOError:
        return make_response("<h1>403 Forbidden</h1>", 403)

@app.route('/imagens/vlibras.gif')
def vlibras_teste():
    try:
        with app.open_resource('static/vlibras.gif') as f:
            conteudo = f.read()
        resposta = make_response(conteudo)
        resposta.headers['Content-type'] = 'image/gif'
        return resposta
    except IOError:
        return make_response("<h1>403 Forbidden</h1>", 403)

@app.route('/barra.js')
def barra2_0():
    with app.open_resource('templates/'+os.getenv('PROFILE', 'default')+'/barra-brasil.js') as f:
        conteudo = f.read().decode('utf-8')
    etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
    accept_encoding = request.headers.get('Accept-Encoding', '')
    if request.if_none_match and \
              etag in request.if_none_match:
        resposta = Response(status=304)
	if accept_encoding and \
		'gzip' in accept_encoding:
	   conteudo = zlib.compress(conteudo.encode('utf8'))
	   resposta.headers['Content-Encoding'] = 'gzip'
    else: # nao esta em cache do navegador
        resposta = make_response(conteudo)
        resposta.set_etag(etag)
    resposta.headers['Content-type'] = 'application/x-javascript'
    resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
    resposta.headers['Last-Modified'] = datetime.datetime.now()
    return resposta

@app.route('/barra_2.0.js')
def barra():
    with app.open_resource('templates/default/barra-brasil.js') as f:
        conteudo = f.read().decode('utf-8')
    etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
    accept_encoding = request.headers.get('Accept-Encoding', '')
    if request.if_none_match and \
              etag in request.if_none_match:
        resposta = Response(status=304)
	if accept_encoding and \
		'gzip' in accept_encoding:
	   conteudo = zlib.compress(conteudo.encode('utf8'))
	   resposta.headers['Content-Encoding'] = 'gzip'
    else: # nao esta em cache do navegador
        resposta = make_response(conteudo)
        resposta.set_etag(etag)
    resposta.headers['Content-type'] = 'application/x-javascript'
    resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
    resposta.headers['Last-Modified'] = datetime.datetime.now()
    return resposta

@app.route('/vlibras-plugin.js')
def vlibras():
    with app.open_resource('static/vlibras-plugin.js') as f:
        conteudo = f.read().decode('utf-8')
    etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
    accept_encoding = request.headers.get('Accept-Encoding', '')
    if request.if_none_match and \
              etag in request.if_none_match:
        resposta = Response(status=304)
	if accept_encoding and \
		'gzip' in accept_encoding:
	   conteudo = zlib.compress(conteudo.encode('utf8'))
	   resposta.headers['Content-Encoding'] = 'gzip'
    else: # nao esta em cache do navegador
        resposta = make_response(conteudo)
        resposta.set_etag(etag)
    resposta.headers['Content-type'] = 'application/x-javascript'
    resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
    resposta.headers['Last-Modified'] = datetime.datetime.now()
    return resposta

@app.route('/app/target/UnityLoader.js')
def unity():
    with app.open_resource('static/UnityLoader.js') as f:
        conteudo = f.read().decode('utf-8')
    etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
    accept_encoding = request.headers.get('Accept-Encoding', '')
    if request.if_none_match and \
              etag in request.if_none_match:
        resposta = Response(status=304)
	if accept_encoding and \
		'gzip' in accept_encoding:
	   conteudo = zlib.compress(conteudo.encode('utf8'))
	   resposta.headers['Content-Encoding'] = 'gzip'
    else: # nao esta em cache do navegador
        resposta = make_response(conteudo)
        resposta.set_etag(etag)
    resposta.headers['Content-type'] = 'application/x-javascript'
    resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
    resposta.headers['Last-Modified'] = datetime.datetime.now()
    return resposta

@app.route('/app/target/playerweb.json')
def playerweb():
    with app.open_resource('static/playerweb.json') as f:
        conteudo = f.read().decode('utf-8')
    etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
    accept_encoding = request.headers.get('Accept-Encoding', '')
    if request.if_none_match and \
              etag in request.if_none_match:
        resposta = Response(status=304)
	if accept_encoding and \
		'gzip' in accept_encoding:
	   conteudo = zlib.compress(conteudo.encode('utf8'))
	   resposta.headers['Content-Encoding'] = 'gzip'
    else: # nao esta em cache do navegador
        resposta = make_response(conteudo)
        resposta.set_etag(etag)
    resposta.headers['Content-type'] = 'application/json'
    resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
    resposta.headers['Last-Modified'] = datetime.datetime.now()
    return resposta



@app.route('/static/jquery.js')
def jquery():
    with app.open_resource('static/jquery-2.1.3.min.js') as f:
        conteudo = f.read().decode('utf-8')
    etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
    accept_encoding = request.headers.get('Accept-Encoding', '')
    if request.if_none_match and \
              etag in request.if_none_match:
        resposta = Response(status=304)
	if accept_encoding and \
		'gzip' in accept_encoding:
	   conteudo = zlib.compress(conteudo.encode('utf8'))
	   resposta.headers['Content-Encoding'] = 'gzip'
    else: # nao esta em cache do navegador
        resposta = make_response(conteudo)
        resposta.set_etag(etag)
    resposta.headers['Content-type'] = 'application/x-javascript'
    resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
    resposta.headers['Last-Modified'] = datetime.datetime.now()
    return resposta

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
    #webbrowser.open("https://127.0.0.1:5000/",new=2)
    #app.run(debug=False,ssl_context=ctx)