diff --git a/Dockerfile b/Dockerfile index 3eb8dea..aca0183 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM debian RUN apt-get update ; apt-get -y upgrade ; \ - apt-get -y install python3 python3-flask python3-flask-sqlalchemy python3-pymysql python3-requests python3-bs4 phantomjs ; \ + apt-get -y install python3 python3-flask python3-flask-sqlalchemy python3-flask-httpauth python3-pymysql python3-requests python3-bs4 phantomjs ; \ rm -rf /var/lib/apt/lists/* diff --git a/app.py b/app.py index c676a31..cdc061b 100644 --- a/app.py +++ b/app.py @@ -1,38 +1,41 @@ -from core import app -from models import * -from flask import Response, request -from werkzeug.security import generate_password_hash, check_password_hash -from functools import wraps from os import environ +from sys import modules + +import pymysql +from flask import Flask +from flask_httpauth import HTTPBasicAuth +from flask_sqlalchemy import SQLAlchemy + +modules["MySQLdb"] = pymysql + +# init DB and migration +db_user = environ.get('MYSQL_USER') +db_password = environ.get('MYSQL_PASSWORD') +db_name = environ.get('MYSQL_DATABASE') +db_host = environ.get('MYSQL_HOST') +if not db_host or not db_user or not db_password or not db_name: + print('Missing connection environment variables') + exit() + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s/%s' % (db_user, db_password, db_host, db_name) +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True +auth = HTTPBasicAuth() +db = SQLAlchemy(app) -# init HTTP basic auth -def check_auth(username, password): - # This function is called to check if a username / password combination is valid. +@auth.verify_password +def verify_password(username, password): admin_username = environ.get('ADMIN_USERNAME', 'admin') admin_password = environ.get('ADMIN_USERNAME', 'secret') - return username == admin_username and password == admin_password - - -def authenticate(): - # Sends a 401 response that enables basic auth - return Response( - 'Could not verify your access level for that URL.\n' - 'You have to login with proper credentials', 401, - {'WWW-Authenticate': 'Basic realm="Login Required"'}) - - -def requires_auth(f): - @wraps(f) - def decorated(*args, **kwargs): - auth = request.authorization - if not auth or not check_auth(auth.username, auth.password): - return authenticate() - return f(*args, **kwargs) - - return decorated + return username is admin_username and password is admin_password @app.route('/') def hello_world(): return 'Hello World !' + + +if __name__ == '__main__': + app_debug = environ.get('FLASK_ENV', 'production') is 'development' + app.run('0.0.0.0', debug=app_debug) diff --git a/core.py b/core.py deleted file mode 100644 index a2f91d0..0000000 --- a/core.py +++ /dev/null @@ -1,21 +0,0 @@ -from sys import modules -from flask import Flask -from os import environ -from flask_sqlalchemy import SQLAlchemy -import pymysql - -modules["MySQLdb"] = pymysql -app = Flask(__name__) - -# init DB and migration -db_user = environ.get('MYSQL_USER') -db_password = environ.get('MYSQL_PASSWORD') -db_name = environ.get('MYSQL_DATABASE') -db_host = environ.get('MYSQL_HOST') -if not db_host or not db_user or not db_password or not db_name: - print('Missing connection environment variables') - exit() - -app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s/%s' % (db_user, db_password, db_host, db_name) -app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True -db = SQLAlchemy(app) diff --git a/docker-compose.yml b/docker-compose.yml index c1d952d..785b82d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: build: . ports: - "5000:5000" - command: flask run --host 0.0.0.0 + entrypoint: flask run -h 0.0.0.0 working_dir: /app environment: FLASK_APP: app.py diff --git a/models.py b/models.py index 5053f3b..06feaa0 100644 --- a/models.py +++ b/models.py @@ -1,4 +1,4 @@ -from core import db +from app import db class AnimeFolder(db.Model): diff --git a/requirements.txt b/requirements.txt index 6e79b50..c6a25ea 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ Flask==1.0.2 Flask-SQLAlchemy==2.1 +Flask-HTTPAuth==3.2.4 PyMySQL==0.9.3 requests==2.21.0 beautifulsoup4==4.7.1