Use multiple database server type
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michel Roux 2022-03-04 09:46:21 +01:00
parent 365800f441
commit 73d268421a
7 changed files with 20 additions and 65 deletions

View File

@ -1,14 +1,9 @@
FLASK_APP=run.py
FLASK_ENV=development
FLASK_PORT=5000
MYSQL_USER=nyaa
MYSQL_PASSWORD=nyaa
MYSQL_DATABASE=nyaa
MYSQL_SERVER=db
REDIS_SERVER=redis
ADMIN_USERNAME=admin
ADMIN_PASSWORD=secret
REQUESTS_TIMEOUT=5
CACHE_TIMEOUT=3600
MYSQL_ROOT_PASSWORD=root
BLACKLIST_WORDS=Chris44,Vol.,[zza],.ssa,.ass,Ref:rain

View File

@ -38,7 +38,7 @@ After a good rewrite in Python, it's time to show it to the public, and here it
All is managed by environment variables.
Please look into the `.env.dist` file to list all possible environment variables.
You have to install MariaDB (or any MySQL server) to be able to access the admin panel.
You have to have a running database server to be able to access the admin panel.
## Links

View File

@ -1,32 +0,0 @@
version: "2.4"
services:
app:
build: .
ports:
- "5000:5000"
entrypoint: python3 run.py
working_dir: /app
depends_on:
- db
- redis
env_file:
- .env.dist
- .env
volumes:
- .:/app
db:
image: mariadb
ports:
- "3306:3306"
env_file:
- .env.dist
- .env
volumes:
- ./.db:/var/lib/mysql
redis:
image: redis
ports:
- "6379:6379"

View File

@ -5,12 +5,12 @@ from operator import attrgetter, itemgetter
from flask import redirect, render_template, request, url_for, abort
from . import utils
from .config import app, auth, ADMIN_USERNAME, ADMIN_PASSWORD, MYSQL_ENABLED, APP_PORT, IS_DEBUG, TRANSMISSION_ENABLED
from .config import app, auth, ADMIN_USERNAME, ADMIN_PASSWORD, DB_ENABLED, APP_PORT, IS_DEBUG, TRANSMISSION_ENABLED
from .connectors import get_instance, run_all, Nyaa
from .connectors.core import ConnectorLang, ConnectorReturn
from .forms import SearchForm, DeleteForm, EditForm, FolderDeleteForm, FolderEditForm
if MYSQL_ENABLED:
if DB_ENABLED:
from .config import db
from .models import AnimeFolder, AnimeTitle, AnimeLink
@ -18,10 +18,10 @@ if TRANSMISSION_ENABLED:
from .config import transmission
def mysql_required(f):
def db_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not MYSQL_ENABLED:
if not DB_ENABLED:
return abort(404)
return f(*args, **kwargs)
@ -50,7 +50,7 @@ def verify_password(username, password):
def boldify(name):
query = request.args.get('q', '')
name = utils.boldify(name, query)
if MYSQL_ENABLED:
if DB_ENABLED:
for keyword in db.session.query(AnimeTitle.keyword.distinct()).all():
if keyword[0].lower() != query.lower():
name = utils.boldify(name, keyword[0])
@ -69,7 +69,7 @@ def colorify(model):
@app.context_processor
def inject_user():
return dict(mysql_disabled=not MYSQL_ENABLED)
return dict(db_disabled=not DB_ENABLED)
@app.route('/')
@ -108,7 +108,7 @@ def latest(page=1):
@app.route('/list')
@app.route('/list/<url_filters>')
@mysql_required
@db_required
def list_animes(url_filters='nyaa,yggtorrent'):
filters = None
for i, to_filter in enumerate(url_filters.split(',')):
@ -131,7 +131,7 @@ def list_animes(url_filters='nyaa,yggtorrent'):
@app.route('/admin', methods=['GET', 'POST'])
@mysql_required
@db_required
@auth.login_required
def admin():
form = DeleteForm(request.form)
@ -165,7 +165,7 @@ def admin():
@app.route('/admin/folder', methods=['GET', 'POST'])
@mysql_required
@db_required
@auth.login_required
def folder_list():
form = FolderDeleteForm(request.form)
@ -188,7 +188,7 @@ def folder_list():
@app.route('/admin/folder/edit', methods=['GET', 'POST'])
@app.route('/admin/folder/edit/<int:folder_id>', methods=['GET', 'POST'])
@mysql_required
@db_required
@auth.login_required
def folder_edit(folder_id=None):
folder = AnimeFolder.query.filter_by(id=folder_id).first()
@ -213,7 +213,7 @@ def folder_edit(folder_id=None):
@app.route('/admin/edit', methods=['GET', 'POST'])
@app.route('/admin/edit/<int:link_id>', methods=['GET', 'POST'])
@mysql_required
@db_required
@auth.login_required
def admin_edit(link_id=None):
link = AnimeLink.query.filter_by(id=link_id).first()

View File

@ -17,7 +17,7 @@ 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))
BLACKLIST_WORDS = environ.get('BLACKLIST_WORDS', '').split(',') if environ.get('BLACKLIST_WORDS', '') else []
MYSQL_ENABLED = False
DB_ENABLED = False
REDIS_ENABLED = False
TRANSMISSION_ENABLED = False
@ -30,18 +30,10 @@ auth = HTTPBasicAuth()
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:
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
)
db_uri = environ.get('DATABASE_URI')
if db_uri:
DB_ENABLED = True
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_ECHO'] = IS_DEBUG
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {

View File

@ -24,7 +24,7 @@
<i class="fa fa-newspaper-o"></i><i>&nbsp;</i>
<span class="is-hidden-mobile">Latest torrents</span>
</a>
{% if not mysql_disabled %}
{% if not db_disabled %}
<a class="navbar-item has-tooltip-bottom has-tooltip-hidden-desktop" data-tooltip="My seeded torrents"
href="{{ url_for('list_animes') }}">
<i class="fa fa-cloud-download"></i><i>&nbsp;</i>

View File

@ -2,11 +2,11 @@ import re
from datetime import datetime
from dateparser import parse
from .config import MYSQL_ENABLED, BLACKLIST_WORDS
from .config import DB_ENABLED, BLACKLIST_WORDS
def link_exist_in_db(href):
if MYSQL_ENABLED:
if DB_ENABLED:
from .models import AnimeLink
return AnimeLink.query.filter_by(link=href).first()
return False