2019-11-29 14:04:32 +00:00
|
|
|
from os import environ, urandom
|
|
|
|
from sys import modules
|
|
|
|
|
|
|
|
import pymysql
|
|
|
|
from flask import Flask
|
2019-12-13 17:23:06 +00:00
|
|
|
from flask.cli import load_dotenv
|
2019-11-29 14:04:32 +00:00
|
|
|
from flask_httpauth import HTTPBasicAuth
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
|
|
|
modules["MySQLdb"] = pymysql
|
2019-12-13 17:23:06 +00:00
|
|
|
load_dotenv()
|
2019-11-29 14:04:32 +00:00
|
|
|
|
|
|
|
# init DB and migration
|
|
|
|
db_user = environ.get('MYSQL_USER')
|
|
|
|
db_password = environ.get('MYSQL_PASSWORD')
|
|
|
|
db_name = environ.get('MYSQL_DATABASE')
|
2019-11-30 22:42:35 +00:00
|
|
|
db_host = environ.get('MYSQL_SERVER')
|
2019-11-29 14:04:32 +00:00
|
|
|
if not db_host or not db_user or not db_password or not db_name:
|
|
|
|
print('Missing connection environment variables')
|
|
|
|
exit()
|
|
|
|
|
2019-12-07 09:17:29 +00:00
|
|
|
# load app constants
|
|
|
|
IS_DEBUG = environ.get('FLASK_ENV', 'production') == 'development'
|
|
|
|
ADMIN_USERNAME = environ.get('ADMIN_USERNAME', 'admin')
|
|
|
|
ADMIN_PASSWORD = environ.get('ADMIN_PASSWORD', 'secret')
|
|
|
|
APP_PORT = environ.get('FLASK_PORT', 5000)
|
|
|
|
|
2019-11-29 14:04:32 +00:00
|
|
|
app = Flask(__name__)
|
2019-12-07 09:17:29 +00:00
|
|
|
app.secret_key = urandom(24).hex()
|
2019-12-15 11:16:12 +00:00
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s/%s?charset=utf8mb4' % (db_user, db_password, db_host, db_name)
|
2019-11-29 14:04:32 +00:00
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
|
2019-12-07 09:17:29 +00:00
|
|
|
app.config['SQLALCHEMY_ECHO'] = IS_DEBUG
|
2019-11-29 14:04:32 +00:00
|
|
|
auth = HTTPBasicAuth()
|
|
|
|
db = SQLAlchemy(app)
|