2021-01-19 04:49:51 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2022-06-10 01:02:40 +00:00
|
|
|
from flask import Flask, redirect
|
2022-08-22 19:35:35 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from flask_cors import CORS
|
2020-01-21 00:35:55 +00:00
|
|
|
from flask_socketio import SocketIO
|
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
def create_app():
|
|
|
|
# Flask Setup
|
2022-06-10 01:02:40 +00:00
|
|
|
app = Flask(__name__)
|
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
|
2020-06-20 13:03:48 +00:00
|
|
|
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
|
|
|
|
app.config['JSON_AS_ASCII'] = False
|
2020-01-21 00:35:55 +00:00
|
|
|
|
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
|
|
|
|
|
2021-11-30 04:07:14 +00:00
|
|
|
socketio.init_app(app, path=base_url.rstrip('/')+'/api/socket.io', cors_allowed_origins='*',
|
|
|
|
async_mode='threading', allow_upgrades=False, transports='polling')
|
2020-01-21 00:35:55 +00:00
|
|
|
|
2022-06-10 01:02:40 +00:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(_):
|
|
|
|
return redirect(base_url, code=302)
|
2020-01-21 00:35:55 +00:00
|
|
|
|
2022-06-10 01:02:40 +00:00
|
|
|
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)
|