Improved webstart.py

* Exit with code 1 on port in use
* Log when authentication is enabled
* Cleanup of code
This commit is contained in:
Bas Stottelaar
2014-09-02 21:11:05 +02:00
parent 163e1e3f2e
commit 9d774b4486

View File

@@ -15,25 +15,25 @@
import os
import sys
import cherrypy
import headphones
from headphones import logger
from headphones.webserve import WebInterface
from headphones.helpers import create_https_certificates
def initialize(options={}):
def initialize(options=None):
if options is None:
options = {}
#HTTPS stuff stolen from sickbeard
# HTTPS stuff stolen from sickbeard
enable_https = options['enable_https']
https_cert = options['https_cert']
https_key = options['https_key']
if enable_https:
# If either the HTTPS certificate or key do not exist, make some self-signed ones.
if not (https_cert and os.path.exists(https_cert)) or not (https_key and os.path.exists(https_key)):
if not (https_cert and os.path.exists(https_cert)) or not https_key and os.path.exists(https_key)):
if not create_https_certificates(https_cert, https_key):
logger.warn(u"Unable to create cert/key files, disabling HTTPS")
headphones.ENABLE_HTTPS = False
@@ -45,15 +45,15 @@ def initialize(options={}):
enable_https = False
options_dict = {
'log.screen': False,
'server.thread_pool': 10,
'server.socket_port': options['http_port'],
'server.socket_host': options['http_host'],
'engine.autoreload_on': False,
'tools.encode.on' : True,
'tools.encode.encoding' : 'utf-8',
'tools.decode.on' : True,
}
'server.socket_port': options['http_port'],
'server.socket_host': options['http_host'],
'server.thread_pool': 10,
'tools.encode.on': True,
'tools.encode.encoding': 'utf-8',
'tools.decode.on': True,
'log.screen': False,
'engine.autoreload_on': False,
}
if enable_https:
options_dict['server.ssl_certificate'] = https_cert
@@ -62,7 +62,8 @@ def initialize(options={}):
else:
protocol = "http"
logger.info("Starting Headphones on %s://%s:%d/", protocol, options['http_host'], options['http_port'])
logger.info("Starting Headphones web server on %s://%s:%d/", protocol,
options['http_host'], options['http_port'])
cherrypy.config.update(options_dict)
conf = {
@@ -88,7 +89,8 @@ def initialize(options={}):
},
'/favicon.ico':{
'tools.staticfile.on': True,
'tools.staticfile.filename': os.path.join(os.path.abspath(os.curdir),"images" + os.sep + "favicon.ico")
'tools.staticfile.filename': os.path.join(os.path.abspath(
os.curdir), "images" + os.sep + "favicon.ico")
},
'/cache':{
'tools.staticdir.on': True,
@@ -96,29 +98,29 @@ def initialize(options={}):
}
}
if options['http_password'] != "":
if options['http_password']:
logger.info("Web server authentication is enabled, username is '%s'", options['http_username'])
conf['/'].update({
'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'Headphones',
'tools.auth_basic.checkpassword': cherrypy.lib.auth_basic.checkpassword_dict({
options['http_username']: options['http_password']
})
'tools.auth_basic.realm': 'Headphones web server',
'tools.auth_basic.checkpassword': cherrypy.lib.auth_basic \
.checkpassword_dict({
options['http_username']: options['http_password']
})
})
conf['/api'] = { 'tools.auth_basic.on': False }
# Prevent time-outs
cherrypy.engine.timeout_monitor.unsubscribe()
cherrypy.tree.mount(WebInterface(), options['http_root'], config = conf)
try:
cherrypy.process.servers.check_port(options['http_host'], options['http_port'])
cherrypy.server.start()
except IOError:
print 'Failed to start on port: %i. Is something else running?' % (options['http_port'])
sys.exit(0)
cherrypy.server.wait()
sys.stderr.write('Failed to start on port: %i. Is something else running?\n' % (options['http_port']))
sys.exit(1)
cherrypy.server.wait()