2022-05-04 23:09:46 +00:00
|
|
|
import gevent.monkey
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
gevent.monkey.patch_all()
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
import faulthandler
|
|
|
|
from os import environ
|
|
|
|
from sys import argv, stdout
|
|
|
|
|
|
|
|
import gevent
|
|
|
|
import redis
|
|
|
|
from flask import Flask
|
2022-05-04 23:09:46 +00:00
|
|
|
from flask_caching import Cache
|
|
|
|
from flask_compress import Compress
|
2022-11-15 09:19:08 +00:00
|
|
|
from flask_limiter import Limiter
|
2022-05-04 23:09:46 +00:00
|
|
|
from sqlalchemy import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from sqlalchemy.orm import scoped_session, sessionmaker
|
|
|
|
|
|
|
|
from files.helpers.const import *
|
|
|
|
from files.helpers.const_stateful import const_initialize
|
|
|
|
from files.helpers.settings import reload_settings, start_watching_settings
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
app = Flask(__name__, template_folder='templates')
|
|
|
|
app.url_map.strict_slashes = False
|
|
|
|
app.jinja_env.cache = {}
|
|
|
|
app.jinja_env.auto_reload = True
|
2022-07-09 10:38:45 +00:00
|
|
|
app.jinja_env.add_extension('jinja2.ext.do')
|
2022-05-04 23:09:46 +00:00
|
|
|
faulthandler.enable()
|
|
|
|
|
2022-10-27 17:53:08 +00:00
|
|
|
app.config['SERVER_NAME'] = SITE
|
2022-10-21 23:12:36 +00:00
|
|
|
app.config['SECRET_KEY'] = environ.get('SECRET_KEY').strip()
|
2022-05-04 23:09:46 +00:00
|
|
|
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3153600
|
2022-11-20 22:16:49 +00:00
|
|
|
if not IS_LOCALHOST:
|
2022-11-15 22:22:43 +00:00
|
|
|
app.config['SESSION_COOKIE_DOMAIN'] = f'.{SITE}'
|
2022-11-21 02:28:05 +00:00
|
|
|
app.config["SESSION_COOKIE_SECURE"] = True
|
2022-05-04 23:09:46 +00:00
|
|
|
app.config["SESSION_COOKIE_NAME"] = "session_" + environ.get("SITE_NAME").strip().lower()
|
2022-05-07 06:04:14 +00:00
|
|
|
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
|
2022-05-04 23:09:46 +00:00
|
|
|
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
|
2022-05-27 21:28:10 +00:00
|
|
|
app.config["PERMANENT_SESSION_LIFETIME"] = 60 * 60 * 24 * 365
|
2022-05-19 17:58:18 +00:00
|
|
|
app.config['SESSION_REFRESH_EACH_REQUEST'] = False
|
2022-06-24 15:08:57 +00:00
|
|
|
|
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
2022-10-10 09:06:27 +00:00
|
|
|
app.config['SQLALCHEMY_DATABASE_URL'] = environ.get("DATABASE_URL").strip()
|
2022-06-24 15:08:57 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
app.config["CACHE_TYPE"] = "RedisCache"
|
2022-10-10 09:06:27 +00:00
|
|
|
app.config["CACHE_REDIS_URL"] = environ.get("REDIS_URL").strip()
|
2022-06-24 15:08:57 +00:00
|
|
|
|
2022-10-10 09:06:27 +00:00
|
|
|
r=redis.Redis(host=environ.get("REDIS_URL").strip(), decode_responses=True, ssl_cert_reqs=None)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
def get_CF():
|
|
|
|
with app.app_context():
|
|
|
|
return request.headers.get('CF-Connecting-IP')
|
|
|
|
|
|
|
|
limiter = Limiter(
|
|
|
|
app,
|
|
|
|
key_func=get_CF,
|
2022-11-15 09:19:08 +00:00
|
|
|
default_limits=[DEFAULT_RATELIMIT],
|
2022-05-04 23:09:46 +00:00
|
|
|
application_limits=["10/second;200/minute;5000/hour;10000/day"],
|
|
|
|
storage_uri=environ.get("REDIS_URL", "redis://localhost")
|
|
|
|
)
|
|
|
|
|
2022-06-24 15:08:57 +00:00
|
|
|
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URL'])
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-25 15:04:33 +00:00
|
|
|
db_session = scoped_session(sessionmaker(bind=engine, autoflush=False))
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
const_initialize(db_session)
|
|
|
|
|
|
|
|
reload_settings()
|
|
|
|
start_watching_settings()
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
cache = Cache(app)
|
|
|
|
Compress(app)
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.routes.allroutes import *
|
2022-06-26 06:33:08 +00:00
|
|
|
|
2022-11-13 07:02:25 +00:00
|
|
|
@limiter.request_filter
|
|
|
|
def no_step_on_jc():
|
|
|
|
if request:
|
|
|
|
key = environ.get("NO_STEP_ON_JC", "")
|
|
|
|
if key and key == request.headers.get("X-No-Step", ""): return True
|
|
|
|
return False
|
|
|
|
|
2022-10-04 19:48:52 +00:00
|
|
|
if "load_chat" in argv:
|
2022-05-04 23:09:46 +00:00
|
|
|
from files.routes.chat import *
|
|
|
|
else:
|
2022-05-29 01:55:36 +00:00
|
|
|
from files.routes import *
|
2022-07-17 19:17:46 +00:00
|
|
|
|
2022-09-29 05:43:29 +00:00
|
|
|
stdout.flush()
|