Refactor app
This commit is contained in:
parent
ab76d99377
commit
57718e5f4e
@ -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/*
|
||||
|
59
app.py
59
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)
|
||||
|
21
core.py
21
core.py
@ -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)
|
@ -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
|
||||
|
@ -1,4 +1,4 @@
|
||||
from core import db
|
||||
from app import db
|
||||
|
||||
|
||||
class AnimeFolder(db.Model):
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user