This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/pynyaata/config.py

69 lines
2.4 KiB
Python
Raw Normal View History

2021-01-29 20:32:28 +00:00
import logging
2019-11-29 14:04:32 +00:00
from os import environ, urandom
from flask import Flask
2019-12-13 17:23:06 +00:00
from flask.cli import load_dotenv
2021-01-29 20:32:28 +00:00
from flask_apscheduler import APScheduler
2019-11-29 14:04:32 +00:00
from flask_httpauth import HTTPBasicAuth
from flask_sqlalchemy import SQLAlchemy
2021-07-10 07:25:35 +00:00
from redis import Redis
2021-07-10 21:15:06 +00:00
from transmission_rpc.client import Client
2019-11-29 14:04:32 +00:00
2019-12-15 11:42:46 +00:00
load_dotenv()
2019-11-29 14:04:32 +00:00
2019-12-07 09:17:29 +00:00
IS_DEBUG = environ.get('FLASK_ENV', 'production') == 'development'
ADMIN_USERNAME = environ.get('ADMIN_USERNAME', 'admin')
2020-12-08 16:17:24 +00:00
ADMIN_PASSWORD = environ.get('ADMIN_PASSWORD', 'secret')
2020-10-19 21:15:58 +00:00
APP_PORT = int(environ.get('FLASK_PORT', 5000))
CACHE_TIMEOUT = int(environ.get('CACHE_TIMEOUT', 60 * 60))
REQUESTS_TIMEOUT = int(environ.get('REQUESTS_TIMEOUT', 5))
2020-04-27 18:47:54 +00:00
BLACKLIST_WORDS = environ.get('BLACKLIST_WORDS', '').split(',') if environ.get('BLACKLIST_WORDS', '') else []
2020-10-21 20:13:57 +00:00
CLOUDPROXY_ENDPOINT = environ.get('CLOUDPROXY_ENDPOINT')
MYSQL_ENABLED = False
2021-07-10 07:25:35 +00:00
REDIS_ENABLED = False
2021-07-10 21:15:06 +00:00
TRANSMISSION_ENABLED = False
2019-12-07 09:17:29 +00:00
2019-11-29 14:04:32 +00:00
app = Flask(__name__)
2019-12-26 22:12:56 +00:00
app.name = 'PyNyaaTa'
app.debug = IS_DEBUG
2019-12-07 09:17:29 +00:00
app.secret_key = urandom(24).hex()
2020-01-05 11:24:23 +00:00
app.url_map.strict_slashes = False
2019-11-29 14:04:32 +00:00
auth = HTTPBasicAuth()
2021-01-29 20:32:28 +00:00
scheduler = APScheduler(app=app)
logging.basicConfig(level=(logging.DEBUG if IS_DEBUG else logging.INFO))
logger = logging.getLogger(app.name)
db_host = environ.get('MYSQL_SERVER')
if db_host:
MYSQL_ENABLED = True
db_user = environ.get('MYSQL_USER')
db_password = environ.get('MYSQL_PASSWORD')
db_name = environ.get('MYSQL_DATABASE')
if not db_user or not db_password or not db_name:
2021-01-29 20:32:28 +00:00
logger.error('Missing connection environment variables')
exit()
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://%s:%s@%s/%s?charset=utf8mb4' % (
db_user, db_password, db_host, db_name
)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
2021-07-11 08:10:25 +00:00
app.config['SQLALCHEMY_ECHO'] = IS_DEBUG
2020-04-11 12:34:34 +00:00
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
'pool_recycle': 200
}
db = SQLAlchemy(app)
2021-07-10 15:56:14 +00:00
from .models import create_all
2021-07-10 15:28:45 +00:00
create_all()
2021-07-10 07:25:35 +00:00
cache_host = environ.get('REDIS_SERVER')
if cache_host:
REDIS_ENABLED = True
cache = Redis(cache_host)
2021-07-10 21:15:06 +00:00
transmission_host = environ.get('TRANSMISSION_SERVER')
if transmission_host:
TRANSMISSION_ENABLED = True
transmission_username = environ.get('TRANSMISSION_RPC_USERNAME')
transmission_password = environ.get('TRANSMISSION_RPC_PASSWORD')
transmission = Client(username=transmission_username, password=transmission_password, host=transmission_host)