bazarr/bazarr/app/app.py

77 lines
2.2 KiB
Python
Raw Permalink Normal View History

# coding=utf-8
from flask import Flask, redirect, Request
2022-08-22 19:35:35 +00:00
from flask_compress import Compress
2022-08-22 19:35:35 +00:00
from flask_cors import CORS
2020-01-21 00:35:55 +00:00
from flask_socketio import SocketIO
from .database import database
from .get_args import args
from .config import settings, base_url
2020-01-21 00:35:55 +00:00
2020-05-11 19:09:04 +00:00
socketio = SocketIO()
2020-01-21 00:35:55 +00:00
class CustomRequest(Request):
def __init__(self, *args, **kwargs):
super(CustomRequest, self).__init__(*args, **kwargs)
# required to increase form-data size before returning a 413
self.max_form_parts = 10000
2020-01-21 00:35:55 +00:00
def create_app():
# Flask Setup
app = Flask(__name__)
app.request_class = CustomRequest
app.config['COMPRESS_ALGORITHM'] = 'gzip'
Compress(app)
2020-04-06 21:27:36 +00:00
app.wsgi_app = ReverseProxied(app.wsgi_app)
2020-01-21 00:35:55 +00:00
2020-07-05 13:16:17 +00:00
app.config["SECRET_KEY"] = settings.general.flask_secret_key
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
app.config['JSON_AS_ASCII'] = False
2020-01-21 00:35:55 +00:00
app.config['RESTX_MASK_SWAGGER'] = False
2022-08-22 19:35:35 +00:00
if settings.get('cors', 'enabled'):
CORS(app)
2020-01-21 00:35:55 +00:00
if args.dev:
app.config["DEBUG"] = True
else:
app.config["DEBUG"] = False
from engineio.async_drivers import threading # noqa W0611 # required to prevent an import exception in engineio
socketio.init_app(app, path=f'{base_url.rstrip("/")}/api/socket.io', cors_allowed_origins='*',
async_mode='threading', allow_upgrades=False, transports='polling', engineio_logger=False)
2020-01-21 00:35:55 +00:00
@app.errorhandler(404)
def page_not_found(_):
return redirect(base_url, code=302)
2020-01-21 00:35:55 +00:00
# This hook ensures that a connection is opened to handle any queries
# generated by the request.
@app.before_request
def _db_connect():
database.begin()
# This hook ensures that the connection is closed when we've finished
# processing the request.
@app.teardown_request
def _db_close(exc):
database.close()
return app
2020-04-06 21:27:36 +00:00
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)