You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bazarr/bazarr/app.py

66 lines
2.0 KiB

4 years ago
#!/bin/env python
4 years ago
from flask import Flask, redirect, render_template
4 years ago
from flask_debugtoolbar import DebugToolbarExtension
from flask_socketio import SocketIO
import os
from get_args import args
from config import base_url
socketio = SocketIO()
def create_app():
# Flask Setup
app = Flask(__name__,
template_folder=os.path.join(os.path.dirname(__file__), '..', 'views'),
4 years ago
static_folder=os.path.join(os.path.dirname(__file__), '..', 'static'),
4 years ago
static_url_path=base_url.rstrip('/') + '/static')
4 years ago
app.wsgi_app = ReverseProxied(app.wsgi_app)
4 years ago
app.route = prefix_route(app.route, base_url.rstrip('/'))
app.config["SECRET_KEY"] = 'test'
if args.dev:
app.config["DEBUG"] = True
# Flask-Debuger
app.config["DEBUG_TB_ENABLED"] = True
app.config["DEBUG_TB_PROFILER_ENABLED"] = True
app.config["DEBUG_TB_TEMPLATE_EDITOR_ENABLED"] = True
app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = False
else:
app.config["DEBUG"] = False
# Flask-Debuger
app.config["DEBUG_TB_ENABLED"] = False
toolbar = DebugToolbarExtension(app)
@app.errorhandler(404)
4 years ago
def page_not_found(e):
return render_template('404.html'), 404
4 years ago
4 years ago
socketio.init_app(app, path=base_url.rstrip('/')+'/socket.io', cors_allowed_origins='*')
4 years ago
return app
def prefix_route(route_function, prefix='', mask='{0}{1}'):
# Defines a new route function with a prefix.
# The mask argument is a `format string` formatted with, in that order: prefix, route
def newroute(route, *args, **kwargs):
# New function to prefix the route
return route_function(mask.format(prefix, route), *args, **kwargs)
return newroute
4 years ago
class ReverseProxied(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
scheme = environ.get('HTTP_X_FORWARDED_PROTO')
if scheme:
environ['wsgi.url_scheme'] = scheme
return self.app(environ, start_response)