diff --git a/main.py b/main.py index ce2d97c..955c7e3 100755 --- a/main.py +++ b/main.py @@ -19,19 +19,12 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import config -import os.path, sys +import sys if __name__ == '__main__': - if not config.check(): - sys.exit(1) + from web import create_application - if not os.path.exists(config.get('base', 'cache_dir')): - os.makedirs(config.get('base', 'cache_dir')) - - import db - from web import app - - db.init_db() - app.run(host = sys.argv[1] if len(sys.argv) > 1 else None, debug = True) + app = create_application() + if app: + app.run(host = sys.argv[1] if len(sys.argv) > 1 else None, debug = True) diff --git a/main.wsgi b/main.wsgi index 9b42dfe..6296a97 100644 --- a/main.wsgi +++ b/main.wsgi @@ -18,18 +18,9 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import os.path, sys +import os.path sys.path.insert(0, '/path/to/the/supysonic/app') -import config -if not config.check(): - sys.exit(1) - -if not os.path.exists(config.get('base', 'cache_dir')): - os.makedirs(config.get('base', 'cache_dir')) - -import db -db.init_db() - -from web import app as application +from web import create_application +application = create_application() diff --git a/web.py b/web.py index 6025b91..1495826 100644 --- a/web.py +++ b/web.py @@ -18,23 +18,11 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import os.path from flask import Flask, request, session, flash, render_template, redirect, url_for -app = Flask(__name__) -app.secret_key = '?9huDM\\H' - import config -if config.get('base', 'log_file'): - import logging - from logging.handlers import TimedRotatingFileHandler - handler = TimedRotatingFileHandler(config.get('base', 'log_file'), when = 'midnight') - handler.setLevel(logging.WARNING) - app.logger.addHandler(handler) -import db -from managers.user import UserManager - -@app.before_request def login_check(): if request.path.startswith('/rest/'): return @@ -51,15 +39,9 @@ def login_check(): flash('Please login') return redirect(url_for('login', returnUrl = request.script_root + request.url[len(request.url_root)-1:])) -@app.teardown_request def teardown(exception): db.session.remove() -@app.template_filter('str') -def to_string(obj): - return str(obj) - -@app.route('/') def index(): stats = { 'artists': db.Artist.query.count(), @@ -68,17 +50,48 @@ def index(): } return render_template('home.html', stats = stats, admin = UserManager.get(session.get('userid'))[1].admin) -import user -import folder -import playlist +def create_application(): + global app, db, UserManager -import api.system -import api.browse -import api.user -import api.albums_songs -import api.media -import api.annotation -import api.chat -import api.search -import api.playlists + if not config.check(): + return None + + if not os.path.exists(config.get('base', 'cache_dir')): + os.makedirs(config.get('base', 'cache_dir')) + + import db + db.init_db() + + app = Flask(__name__) + app.secret_key = '?9huDM\\H' + + if config.get('base', 'log_file'): + import logging + from logging.handlers import TimedRotatingFileHandler + handler = TimedRotatingFileHandler(config.get('base', 'log_file'), when = 'midnight') + handler.setLevel(logging.WARNING) + app.logger.addHandler(handler) + + from managers.user import UserManager + + app.before_request(login_check) + app.teardown_request(teardown) + app.add_template_filter(str) + app.add_url_rule('/', view_func = index) + + import user + import folder + import playlist + + import api.system + import api.browse + import api.user + import api.albums_songs + import api.media + import api.annotation + import api.chat + import api.search + import api.playlists + + return app