mirror of
https://github.com/spl0k/supysonic.git
synced 2024-11-10 04:02:17 +00:00
Improved application factory a bit
This commit is contained in:
parent
b7f0494361
commit
28852c1de1
15
main.py
15
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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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 = create_application()
|
||||
if app:
|
||||
app.run(host = sys.argv[1] if len(sys.argv) > 1 else None, debug = True)
|
||||
|
||||
|
15
main.wsgi
15
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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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()
|
||||
|
||||
|
51
web.py
51
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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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,6 +50,35 @@ def index():
|
||||
}
|
||||
return render_template('home.html', stats = stats, admin = UserManager.get(session.get('userid'))[1].admin)
|
||||
|
||||
def create_application():
|
||||
global app, db, UserManager
|
||||
|
||||
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
|
||||
@ -82,3 +93,5 @@ import api.chat
|
||||
import api.search
|
||||
import api.playlists
|
||||
|
||||
return app
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user