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-01-10 15:41:39 +00:00
|
|
|
from dotenv import load_dotenv
|
2023-03-02 13:19:10 +00:00
|
|
|
import platformdirs
|
2023-03-12 12:29:29 +00:00
|
|
|
from yaml import FullLoader, load
|
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-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
|
|
|
|
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8') as f:
|
|
|
|
conf = load(f, Loader=FullLoader)
|
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-01-10 12:39:29 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(app.instance_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-10 12:32:23 +00:00
|
|
|
# Load theme
|
2023-03-12 12:29:29 +00:00
|
|
|
from . import theme_manager
|
2023-03-12 13:12:38 +00:00
|
|
|
theme_manager.CompileTheme('default', app.root_path)
|
2023-03-12 12:29:29 +00:00
|
|
|
|
2023-03-10 11:10:43 +00:00
|
|
|
# Bundle JS files
|
|
|
|
js = Bundle('js/*.js', output='gen/packed.js')
|
|
|
|
assets.register('js_all', js)
|
2023-03-03 00:26:46 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
@app.errorhandler(403)
|
2023-03-10 12:32:23 +00:00
|
|
|
@app.errorhandler(404)
|
|
|
|
@app.errorhandler(405)
|
2023-01-10 12:39:29 +00:00
|
|
|
@app.errorhandler(410)
|
|
|
|
@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-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-09 23:31:58 +00:00
|
|
|
|
|
|
|
# Load routes for groups
|
|
|
|
from . import groups
|
|
|
|
app.register_blueprint(groups.blueprint)
|
2023-03-02 13:19:10 +00:00
|
|
|
|
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-03-11 22:14:03 +00:00
|
|
|
logging.info('Gallery started successfully!')
|
2023-03-10 12:32:23 +00:00
|
|
|
|
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
|