mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-23 01:16:18 +00:00
Rewrite web.py to use new config.py
This commit is contained in:
parent
1271a35c20
commit
81c2fafe86
@ -1,55 +1,64 @@
|
|||||||
#! /usr/bin/env python
|
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# vim:fenc=utf-8
|
# vim:fenc=utf-8
|
||||||
#
|
#
|
||||||
# This file is part of Supysonic.
|
# This file is part of Supysonic.
|
||||||
# Supysonic is a Python implementation of the Subsonic server API.
|
# Supysonic is a Python implementation of the Subsonic server API.
|
||||||
#
|
#
|
||||||
# Copyright © 2013-2017 Alban 'spl0k' Féron
|
# Copyright (C) 2013-2017 Alban 'spl0k' Féron
|
||||||
# 2017 Óscar García Amor
|
# 2017 Óscar García Amor
|
||||||
#
|
#
|
||||||
# Distributed under terms of the GNU AGPLv3 license.
|
# Distributed under terms of the GNU AGPLv3 license.
|
||||||
|
|
||||||
import os.path
|
|
||||||
from flask import Flask, g
|
from flask import Flask, g
|
||||||
|
from os import makedirs, path
|
||||||
from werkzeug.local import LocalProxy
|
from werkzeug.local import LocalProxy
|
||||||
|
|
||||||
from supysonic import config
|
from supysonic.config import Config
|
||||||
from supysonic.db import get_store
|
from supysonic.db import get_store
|
||||||
|
|
||||||
def get_db_store():
|
# Supysonic database open
|
||||||
store = getattr(g, 'store', None)
|
def get_db():
|
||||||
if store:
|
if not hasattr(g, 'database'):
|
||||||
return store
|
g.database = get_store(Config().get('base', 'database_uri'))
|
||||||
g.store = get_store(config.get('base', 'database_uri'))
|
return g.database
|
||||||
return g.store
|
|
||||||
|
|
||||||
store = LocalProxy(get_db_store)
|
# Supysonic database close
|
||||||
|
def close_db(error):
|
||||||
|
if hasattr(g, 'database'):
|
||||||
|
g.database.close()
|
||||||
|
|
||||||
def teardown_db(exception):
|
store = LocalProxy(get_db)
|
||||||
store = getattr(g, 'store', None)
|
|
||||||
if store:
|
|
||||||
store.close()
|
|
||||||
|
|
||||||
def create_application():
|
def create_application():
|
||||||
global app
|
global app
|
||||||
|
|
||||||
if not config.check():
|
# Check config for mandatory fields
|
||||||
return None
|
Config().check()
|
||||||
|
|
||||||
if not os.path.exists(config.get('webapp', 'cache_dir')):
|
# Test for the cache directory
|
||||||
os.makedirs(config.get('webapp', 'cache_dir'))
|
if not path.exists(Config().get('webapp', 'cache_dir')):
|
||||||
|
os.makedirs(Config().get('webapp', 'cache_dir'))
|
||||||
|
|
||||||
|
# Flask!
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = '?9huDM\\H'
|
|
||||||
|
|
||||||
app.teardown_appcontext(teardown_db)
|
# Set a secret key for sessions
|
||||||
|
secret_key = Config().get('base', 'secret_key')
|
||||||
|
# If secret key is not defined in config, set develop key
|
||||||
|
if secret_key is None:
|
||||||
|
app.secret_key = 'd3v3l0p'
|
||||||
|
else:
|
||||||
|
app.secret_key = secret_key
|
||||||
|
|
||||||
if config.get('webapp', 'log_file'):
|
# Close database connection on teardown
|
||||||
|
app.teardown_appcontext(close_db)
|
||||||
|
|
||||||
|
# Set loglevel
|
||||||
|
if Config().get('webapp', 'log_file'):
|
||||||
import logging
|
import logging
|
||||||
from logging.handlers import TimedRotatingFileHandler
|
from logging.handlers import TimedRotatingFileHandler
|
||||||
handler = TimedRotatingFileHandler(config.get('webapp', 'log_file'), when = 'midnight')
|
handler = TimedRotatingFileHandler(Config().get('webapp', 'log_file'), when = 'midnight')
|
||||||
if config.get('webapp', 'log_level'):
|
if Config().get('webapp', 'log_level'):
|
||||||
mapping = {
|
mapping = {
|
||||||
'DEBUG': logging.DEBUG,
|
'DEBUG': logging.DEBUG,
|
||||||
'INFO': logging.INFO,
|
'INFO': logging.INFO,
|
||||||
@ -57,11 +66,11 @@ def create_application():
|
|||||||
'ERROR': logging.ERROR,
|
'ERROR': logging.ERROR,
|
||||||
'CRTICAL': logging.CRITICAL
|
'CRTICAL': logging.CRITICAL
|
||||||
}
|
}
|
||||||
handler.setLevel(mapping.get(config.get('webapp', 'log_level').upper(), logging.NOTSET))
|
handler.setLevel(mapping.get(Config().get('webapp', 'log_level').upper(), logging.NOTSET))
|
||||||
app.logger.addHandler(handler)
|
app.logger.addHandler(handler)
|
||||||
|
|
||||||
|
# Import app sections
|
||||||
from supysonic import frontend
|
from supysonic import frontend
|
||||||
from supysonic import api
|
from supysonic import api
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user