2023-03-04 13:45:26 +00:00
|
|
|
"""
|
2023-03-11 23:16:27 +00:00
|
|
|
Onlylegs Gallery
|
|
|
|
This is the main app file, it loads all the other files and sets up the app
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
# Import system modules
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# Flask
|
2023-01-25 15:13:56 +00:00
|
|
|
from flask_compress import Compress
|
2023-03-08 09:01:20 +00:00
|
|
|
from flask_caching import Cache
|
2023-03-10 11:10:43 +00:00
|
|
|
from flask_assets import Environment, Bundle
|
2023-03-04 13:45:26 +00:00
|
|
|
from flask import Flask, render_template
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
# Configuration
|
2023-03-02 13:19:10 +00:00
|
|
|
import platformdirs
|
2023-03-20 17:19:42 +00:00
|
|
|
from dotenv import load_dotenv
|
2023-03-23 12:54:00 +00:00
|
|
|
from yaml import safe_load
|
2023-03-20 17:19:42 +00:00
|
|
|
|
|
|
|
# Utils
|
|
|
|
from gallery.utils import theme_manager
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
|
2023-03-10 11:10:43 +00:00
|
|
|
USER_DIR = platformdirs.user_config_dir('onlylegs')
|
2023-03-02 13:19:10 +00:00
|
|
|
|
|
|
|
|
2023-03-12 13:12:38 +00:00
|
|
|
def create_app(test_config=None):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create and configure the main app
|
|
|
|
"""
|
2023-03-11 22:14:03 +00:00
|
|
|
app = Flask(__name__, instance_path=os.path.join(USER_DIR, 'instance'))
|
2023-03-10 11:10:43 +00:00
|
|
|
assets = Environment()
|
2023-03-10 12:32:23 +00:00
|
|
|
cache = Cache(config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 300})
|
2023-03-10 11:10:43 +00:00
|
|
|
compress = Compress()
|
2023-03-20 17:19:42 +00:00
|
|
|
|
2023-03-12 12:29:29 +00:00
|
|
|
# Get environment variables
|
|
|
|
load_dotenv(os.path.join(USER_DIR, '.env'))
|
2023-03-12 13:12:38 +00:00
|
|
|
print("Loaded environment variables")
|
2023-03-12 12:29:29 +00:00
|
|
|
|
|
|
|
# Get config file
|
2023-03-20 17:19:42 +00:00
|
|
|
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8') as file:
|
2023-03-20 17:57:47 +00:00
|
|
|
conf = safe_load(file)
|
2023-03-12 13:12:38 +00:00
|
|
|
print("Loaded gallery config")
|
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-03-04 13:45:26 +00:00
|
|
|
UPLOAD_FOLDER=os.path.join(USER_DIR, 'uploads'),
|
2023-01-11 15:25:35 +00:00
|
|
|
ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'],
|
2023-03-02 17:00:54 +00:00
|
|
|
MAX_CONTENT_LENGTH=1024 * 1024 * conf['upload']['max-size'],
|
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:
|
|
|
|
app.config.from_pyfile('config.py', silent=True)
|
|
|
|
else:
|
|
|
|
app.config.from_mapping(test_config)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-10 12:32:23 +00:00
|
|
|
# Load theme
|
2023-03-12 13:12:38 +00:00
|
|
|
theme_manager.CompileTheme('default', app.root_path)
|
2023-03-20 17:19:42 +00:00
|
|
|
|
2023-03-10 11:10:43 +00:00
|
|
|
# Bundle JS files
|
2023-03-20 17:19:42 +00:00
|
|
|
js_scripts = Bundle('js/*.js', output='gen/packed.js')
|
|
|
|
assets.register('js_all', js_scripts)
|
2023-03-03 00:26:46 +00:00
|
|
|
|
2023-03-12 18:19:43 +00:00
|
|
|
# Error handlers
|
2023-03-25 13:05:16 +00:00
|
|
|
@app.errorhandler(400)
|
|
|
|
@app.errorhandler(401)
|
|
|
|
@app.errorhandler(403)
|
|
|
|
@app.errorhandler(404)
|
|
|
|
@app.errorhandler(405)
|
|
|
|
@app.errorhandler(418)
|
|
|
|
@app.errorhandler(500)
|
2023-03-10 12:32:23 +00:00
|
|
|
def error_page(err):
|
|
|
|
error = err.code
|
2023-03-04 13:45:26 +00:00
|
|
|
msg = err.description
|
2023-03-10 12:32:23 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), err.code
|
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
# Load login, registration and logout manager
|
2023-03-12 15:52:23 +00:00
|
|
|
from gallery import auth
|
2023-01-10 14:40:43 +00:00
|
|
|
app.register_blueprint(auth.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-12 15:52:23 +00:00
|
|
|
# Load the different routes
|
|
|
|
from .routes import api, groups, routing, settings
|
|
|
|
app.register_blueprint(api.blueprint)
|
2023-03-09 23:31:58 +00:00
|
|
|
app.register_blueprint(groups.blueprint)
|
2023-03-12 15:52:23 +00:00
|
|
|
app.register_blueprint(routing.blueprint)
|
2023-03-01 23:29:34 +00:00
|
|
|
app.register_blueprint(settings.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-12 18:19:43 +00:00
|
|
|
# Log to file that the app has started
|
2023-03-11 22:14:03 +00:00
|
|
|
logging.info('Gallery started successfully!')
|
2023-03-10 12:32:23 +00:00
|
|
|
|
2023-03-12 18:19:43 +00:00
|
|
|
# Initialize extensions and return app
|
2023-03-10 11:10:43 +00:00
|
|
|
assets.init_app(app)
|
2023-03-08 09:01:20 +00:00
|
|
|
cache.init_app(app)
|
2023-03-10 11:10:43 +00:00
|
|
|
compress.init_app(app)
|
2023-03-04 13:45:26 +00:00
|
|
|
return app
|