Titan/webapp/titanembeds/app.py

113 lines
4.0 KiB
Python
Raw Normal View History

from config import config
try:
import uwsgi
2017-08-20 21:56:54 +02:00
from gevent import monkey
monkey.patch_all()
except:
if config.get("websockets-mode", None) == "eventlet":
import eventlet
eventlet.monkey_patch()
elif config.get("websockets-mode", None) == "gevent":
from gevent import monkey
monkey.patch_all()
from .database import db
from flask import Flask, render_template, request, session, url_for, redirect, jsonify, g
from flask_sslify import SSLify
2018-12-30 21:55:24 +01:00
from titanembeds.utils import rate_limiter, discord_api, socketio, babel, redis_store, language_code_list#, sentry
from .blueprints import api, user, admin, embed, gateway
import os
2019-10-13 07:36:37 +02:00
from titanembeds.database import get_administrators_list, init_application_settings, get_application_settings
import titanembeds.constants as constants
from datetime import timedelta
2018-03-24 07:03:33 +01:00
import datetime
2019-10-13 07:36:37 +02:00
import random
2019-11-21 05:26:34 +01:00
import time
app_start_stamp = time.time()
os.chdir(config['app-location'])
app = Flask(__name__, static_folder="static")
2017-09-05 08:54:54 +02:00
app.config['SQLALCHEMY_DATABASE_URI'] = config['database-uri']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Suppress the warning/no need this on for now.
app.config['RATELIMIT_HEADERS_ENABLED'] = True
2018-01-23 20:01:17 +01:00
app.config['SQLALCHEMY_POOL_RECYCLE'] = 100
2018-12-02 21:22:10 +01:00
app.config['SQLALCHEMY_POOL_SIZE'] = 40
2018-12-30 22:52:33 +01:00
app.config['SQLALCHEMY_MAX_OVERFLOW'] = 60
app.config['RATELIMIT_STORAGE_URL'] = config["redis-uri"]
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=3)
app.config['REDIS_URL'] = config["redis-uri"]
2018-08-17 06:02:47 +02:00
app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024 # Limit upload size to 4mb
app.config['SESSION_COOKIE_SAMESITE'] = "None"
app.secret_key = config['app-secret']
2018-12-30 21:55:24 +01:00
#sentry.init_app(app)
db.init_app(app)
2017-03-27 01:37:27 +02:00
rate_limiter.init_app(app)
2017-12-20 02:01:50 +01:00
if config.get("enable-ssl", False):
sslify = SSLify(app, permanent=True)
2017-08-20 21:56:54 +02:00
socketio.init_app(app, message_queue=config["redis-uri"], path='gateway', async_mode=config.get("websockets-mode", None))
babel.init_app(app)
redis_store.init_app(app)
app.register_blueprint(api.api, url_prefix="/api", template_folder="/templates")
app.register_blueprint(admin.admin, url_prefix="/admin", template_folder="/templates")
app.register_blueprint(user.user, url_prefix="/user", template_folder="/templates")
app.register_blueprint(embed.embed, url_prefix="/embed", template_folder="/templates")
2017-08-19 07:09:13 +02:00
socketio.on_namespace(gateway.Gateway('/gateway'))
@babel.localeselector
def get_locale():
param_lang = request.args.get("lang", None)
if param_lang in language_code_list():
return param_lang
return request.accept_languages.best_match(language_code_list())
@app.route("/")
def index():
return render_template("index.html.j2")
2017-05-11 04:12:26 +02:00
@app.route("/about")
def about():
return render_template("about.html.j2")
2017-09-12 06:06:29 +02:00
@app.route("/terms")
def terms():
return render_template("terms_and_conditions.html.j2")
@app.route("/privacy")
def privacy():
return render_template("privacy_policy.html.j2")
2018-02-27 08:22:58 +01:00
@app.route("/vote")
def vote():
2018-02-27 22:22:25 +01:00
return render_template("discordbotsorg_vote.html.j2", referrer=request.args.get("referrer", None))
2018-02-27 08:22:58 +01:00
@app.route("/global_banned_words")
def global_banned_words():
return render_template("global_banned_words.html.j2")
@app.before_first_request
def before_first_request():
2019-10-13 07:36:37 +02:00
init_application_settings()
discord_api.init_discordrest()
@app.context_processor
def context_processor():
return {
2019-10-13 07:36:37 +02:00
"random": random,
"application_settings": get_application_settings(),
"devs": get_administrators_list(),
"sentry_js_dsn": config.get("sentry-js-dsn", None),
"constants": constants,
"af_mode_enabled": datetime.datetime.now().date() == datetime.date(datetime.datetime.now().year, 4, 1),
2019-11-21 05:26:34 +01:00
"dbl_voted": session.get("unauthenticated", True) == False and bool(redis_store.get("DiscordBotsOrgVoted/" + str(session.get("user_id", -1)))),
"app_start_stamp": app_start_stamp
}
2018-12-30 21:55:24 +01:00
# @app.errorhandler(500)
# def internal_server_error(error):
# return render_template('500.html.j2',
# event_id=g.sentry_event_id,
# public_dsn=sentry.client.get_public_dsn('https')
# ), 500