2023-01-10 12:39:29 +00:00
|
|
|
print("""
|
|
|
|
___ _ _
|
|
|
|
/ _ \\ _ __ | |_ _| | ___ __ _ ___
|
|
|
|
| | | | '_ \\| | | | | | / _ \\/ _` / __|
|
|
|
|
| |_| | | | | | |_| | |__| __/ (_| \\__ \\
|
|
|
|
\\___/|_| |_|_|\\__, |_____\\___|\\__, |___/
|
|
|
|
|___/ |___/
|
2023-03-01 23:29:34 +00:00
|
|
|
Created by Fluffy Bean - Version 23.03.01
|
2023-01-10 12:39:29 +00:00
|
|
|
""")
|
|
|
|
|
2023-01-17 22:13:52 +00:00
|
|
|
from flask import Flask, render_template
|
2023-01-25 15:13:56 +00:00
|
|
|
from flask_compress import Compress
|
2023-03-01 23:29:34 +00:00
|
|
|
from flask.helpers import get_root_path
|
2023-01-10 15:41:39 +00:00
|
|
|
from dotenv import load_dotenv
|
2023-01-17 22:13:52 +00:00
|
|
|
import yaml
|
|
|
|
import os
|
2023-01-10 15:41:39 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
print(f"Running at {get_root_path(__name__)}\n")
|
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
def create_app(test_config=None):
|
2023-01-10 12:39:29 +00:00
|
|
|
# create and configure the app
|
|
|
|
app = Flask(__name__)
|
2023-01-31 23:44:44 +00:00
|
|
|
compress = Compress()
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 15:41:39 +00:00
|
|
|
# Get environment variables
|
|
|
|
load_dotenv(os.path.join(app.root_path, 'user', '.env'))
|
2023-03-01 23:29:34 +00:00
|
|
|
print("Loaded environment variables")
|
|
|
|
|
2023-01-11 15:25:35 +00:00
|
|
|
# Get config file
|
|
|
|
with open(os.path.join(app.root_path, 'user', 'conf.yml'), 'r') as f:
|
|
|
|
conf = yaml.load(f, Loader=yaml.FullLoader)
|
2023-03-01 23:29:34 +00:00
|
|
|
print("Loaded gallery config")
|
2023-01-10 18:12:55 +00:00
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
# App configuration
|
2023-01-10 12:39:29 +00:00
|
|
|
app.config.from_mapping(
|
|
|
|
SECRET_KEY=os.environ.get('FLASK_SECRET'),
|
|
|
|
DATABASE=os.path.join(app.instance_path, 'gallery.sqlite'),
|
2023-01-10 15:41:39 +00:00
|
|
|
UPLOAD_FOLDER=os.path.join(app.root_path, 'user', 'uploads'),
|
2023-01-25 15:13:56 +00:00
|
|
|
MAX_CONTENT_LENGTH=1024 * 1024 * conf['upload']['max-size'],
|
2023-01-11 15:25:35 +00:00
|
|
|
ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'],
|
2023-03-01 23:29:34 +00:00
|
|
|
WEBSITE=conf['website'],
|
2023-01-10 12:39:29 +00:00
|
|
|
)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
if test_config is None:
|
|
|
|
# load the instance config, if it exists, when not testing
|
|
|
|
app.config.from_pyfile('config.py', silent=True)
|
|
|
|
else:
|
|
|
|
# load the test config if passed in
|
|
|
|
app.config.from_mapping(test_config)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
# ensure the instance folder exists
|
|
|
|
try:
|
|
|
|
os.makedirs(app.instance_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
# Load database
|
|
|
|
from . import db
|
|
|
|
db.init_app(app)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
# Load theme
|
2023-01-10 15:41:39 +00:00
|
|
|
from . import sassy
|
|
|
|
sassy.compile('default', app.root_path)
|
2023-03-01 23:29:34 +00:00
|
|
|
|
|
|
|
# Load logger
|
|
|
|
from .logger import logger
|
|
|
|
logger.innit_logger(app)
|
2023-01-10 14:40:43 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
@app.errorhandler(405)
|
|
|
|
def method_not_allowed(e):
|
|
|
|
error = '405'
|
2023-03-01 23:29:34 +00:00
|
|
|
msg = e.description
|
|
|
|
return render_template('error.html', error=error, msg=e), 404
|
2023-01-10 12:39:29 +00:00
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(e):
|
|
|
|
error = '404'
|
2023-03-01 23:29:34 +00:00
|
|
|
msg = e.description
|
2023-01-10 12:39:29 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 404
|
|
|
|
|
|
|
|
@app.errorhandler(403)
|
|
|
|
def forbidden(e):
|
|
|
|
error = '403'
|
2023-03-01 23:29:34 +00:00
|
|
|
msg = e.description
|
2023-01-10 12:39:29 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 403
|
|
|
|
|
|
|
|
@app.errorhandler(410)
|
|
|
|
def gone(e):
|
|
|
|
error = '410'
|
2023-03-01 23:29:34 +00:00
|
|
|
msg = e.description
|
2023-01-10 12:39:29 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 410
|
|
|
|
|
|
|
|
@app.errorhandler(500)
|
|
|
|
def internal_server_error(e):
|
|
|
|
error = '500'
|
2023-03-01 23:29:34 +00:00
|
|
|
msg = e.description
|
2023-01-10 12:39:29 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 500
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
# Load login, registration and logout manager
|
2023-01-10 12:39:29 +00:00
|
|
|
from . import auth
|
2023-01-10 14:40:43 +00:00
|
|
|
app.register_blueprint(auth.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
# Load routes for home and images
|
2023-01-31 22:08:37 +00:00
|
|
|
from . import routing
|
|
|
|
app.register_blueprint(routing.blueprint)
|
2023-01-10 12:39:29 +00:00
|
|
|
app.add_url_rule('/', endpoint='index')
|
2023-03-01 23:29:34 +00:00
|
|
|
|
|
|
|
# Load routes for settings
|
|
|
|
from . import settings
|
|
|
|
app.register_blueprint(settings.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 18:12:55 +00:00
|
|
|
# Load APIs
|
|
|
|
from . import api
|
|
|
|
app.register_blueprint(api.blueprint)
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-01-31 23:44:44 +00:00
|
|
|
compress.init_app(app)
|
2023-01-10 12:39:29 +00:00
|
|
|
return app
|