2014-06-11 04:34:28 -04:00
|
|
|
import os
|
2014-06-16 01:45:52 -04:00
|
|
|
import traceback
|
2014-06-23 11:18:01 -04:00
|
|
|
import time
|
2014-03-10 01:18:05 -04:00
|
|
|
import sickbeard
|
2014-06-11 04:34:28 -04:00
|
|
|
import webserve
|
2014-06-15 17:45:09 -04:00
|
|
|
import webapi
|
2014-06-15 07:08:41 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard.helpers import create_https_certificates
|
2014-06-15 03:16:55 -04:00
|
|
|
from tornado.web import Application, StaticFileHandler, RedirectHandler, HTTPError
|
|
|
|
from tornado.httpserver import HTTPServer
|
2014-06-16 08:19:07 -04:00
|
|
|
from tornado.ioloop import IOLoop
|
2014-06-11 04:34:28 -04:00
|
|
|
|
2014-06-16 06:44:33 -04:00
|
|
|
server = None
|
|
|
|
|
2014-06-18 11:06:50 -04:00
|
|
|
|
2014-06-11 04:34:28 -04:00
|
|
|
class MultiStaticFileHandler(StaticFileHandler):
|
|
|
|
def initialize(self, paths, default_filename=None):
|
|
|
|
self.paths = paths
|
2014-06-18 11:06:50 -04:00
|
|
|
self.default_filename = default_filename
|
2014-06-11 04:34:28 -04:00
|
|
|
|
|
|
|
def get(self, path, include_body=True):
|
|
|
|
for p in self.paths:
|
|
|
|
try:
|
|
|
|
# Initialize the Static file with a path
|
|
|
|
super(MultiStaticFileHandler, self).initialize(p)
|
|
|
|
# Try to get the file
|
|
|
|
return super(MultiStaticFileHandler, self).get(path)
|
|
|
|
except HTTPError as exc:
|
|
|
|
# File not found, carry on
|
|
|
|
if exc.status_code == 404:
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Oops file not found anywhere!
|
|
|
|
raise HTTPError(404)
|
|
|
|
|
2014-06-17 12:33:45 -04:00
|
|
|
|
2014-06-15 15:11:21 -04:00
|
|
|
def initWebServer(options={}):
|
|
|
|
options.setdefault('port', 8081)
|
|
|
|
options.setdefault('host', '0.0.0.0')
|
|
|
|
options.setdefault('log_dir', None)
|
|
|
|
options.setdefault('username', '')
|
|
|
|
options.setdefault('password', '')
|
|
|
|
options.setdefault('web_root', '/')
|
|
|
|
assert isinstance(options['port'], int)
|
|
|
|
assert 'data_root' in options
|
|
|
|
|
|
|
|
# tornado setup
|
|
|
|
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 create_https_certificates(https_cert, https_key):
|
|
|
|
logger.log(u"Unable to create CERT/KEY files, disabling HTTPS")
|
2014-03-10 01:18:05 -04:00
|
|
|
sickbeard.ENABLE_HTTPS = False
|
|
|
|
enable_https = False
|
|
|
|
|
2014-06-15 15:11:21 -04:00
|
|
|
if not (os.path.exists(https_cert) and os.path.exists(https_key)):
|
|
|
|
logger.log(u"Disabled HTTPS because of missing CERT and KEY files", logger.WARNING)
|
|
|
|
sickbeard.ENABLE_HTTPS = False
|
|
|
|
enable_https = False
|
|
|
|
|
|
|
|
# Load the app
|
|
|
|
app = Application([],
|
2014-06-18 16:41:20 -04:00
|
|
|
debug=False,
|
2014-06-15 15:11:21 -04:00
|
|
|
gzip=True,
|
2014-06-18 15:51:33 -04:00
|
|
|
xheaders=sickbeard.HANDLE_REVERSE_PROXY,
|
2014-06-17 15:51:23 -04:00
|
|
|
cookie_secret='61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo='
|
2014-06-15 15:11:21 -04:00
|
|
|
)
|
|
|
|
|
2014-06-18 05:16:14 -04:00
|
|
|
# Main Handler
|
2014-06-15 15:11:21 -04:00
|
|
|
app.add_handlers(".*$", [
|
2014-06-19 11:47:28 -04:00
|
|
|
(r"%s" % options['web_root'], RedirectHandler, {'url': '%s/home/' % options['web_root']}),
|
2014-06-18 02:55:45 -04:00
|
|
|
(r'%s/api/(.*)(/?)' % options['web_root'], webapi.Api),
|
2014-06-19 04:37:44 -04:00
|
|
|
(r'%s/(.*)(/?)' % options['web_root'], webserve.MainHandler)
|
2014-06-15 15:11:21 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
# Static Path Handler
|
|
|
|
app.add_handlers(".*$", [
|
2014-06-18 02:55:45 -04:00
|
|
|
(r'%s/(favicon\.ico)' % options['web_root'], MultiStaticFileHandler,
|
|
|
|
{'paths': [os.path.join(options['data_root'], 'images/ico/favicon.ico')]}),
|
2014-06-17 12:33:45 -04:00
|
|
|
(r'%s/%s/(.*)(/?)' % (options['web_root'], 'images'), MultiStaticFileHandler,
|
2014-06-15 15:11:21 -04:00
|
|
|
{'paths': [os.path.join(options['data_root'], 'images'),
|
2014-06-18 08:56:26 -04:00
|
|
|
os.path.join(sickbeard.CACHE_DIR, 'images')]}),
|
2014-06-17 12:33:45 -04:00
|
|
|
(r'%s/%s/(.*)(/?)' % (options['web_root'], 'css'), MultiStaticFileHandler,
|
2014-06-15 15:11:21 -04:00
|
|
|
{'paths': [os.path.join(options['data_root'], 'css')]}),
|
2014-06-17 12:33:45 -04:00
|
|
|
(r'%s/%s/(.*)(/?)' % (options['web_root'], 'js'), MultiStaticFileHandler,
|
2014-06-18 08:56:26 -04:00
|
|
|
{'paths': [os.path.join(options['data_root'], 'js')]})
|
2014-06-15 15:11:21 -04:00
|
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
global server
|
|
|
|
|
|
|
|
if enable_https:
|
|
|
|
protocol = "https"
|
|
|
|
server = HTTPServer(app, no_keep_alive=True,
|
2014-06-17 12:33:45 -04:00
|
|
|
ssl_options={"certfile": https_cert, "keyfile": https_key})
|
2014-06-15 15:11:21 -04:00
|
|
|
else:
|
|
|
|
protocol = "http"
|
|
|
|
server = HTTPServer(app, no_keep_alive=True)
|
|
|
|
|
|
|
|
logger.log(u"Starting SickRage on " + protocol + "://" + str(options['host']) + ":" + str(
|
|
|
|
options['port']) + "/")
|
|
|
|
|
2014-06-25 23:41:18 -04:00
|
|
|
if not sickbeard.restarted:
|
2014-06-17 00:54:00 -04:00
|
|
|
server.listen(options['port'], options['host'])
|
2014-06-17 12:33:45 -04:00
|
|
|
|
2014-06-16 01:45:52 -04:00
|
|
|
def shutdown():
|
2014-06-16 06:44:33 -04:00
|
|
|
global server
|
|
|
|
|
2014-06-23 11:18:01 -04:00
|
|
|
logger.log('Shutting down tornado io loop')
|
2014-06-16 01:45:52 -04:00
|
|
|
try:
|
|
|
|
IOLoop.current().stop()
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
except:
|
2014-06-23 11:18:01 -04:00
|
|
|
logger.log('Failed shutting down tornado io loop: %s' % traceback.format_exc(), logger.ERROR)
|