1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-11-11 03:45:01 -05:00
SickRage/sickbeard/webserveInit.py

157 lines
6.1 KiB
Python
Raw Normal View History

import os
import socket
import time
import threading
import sys
import sickbeard
import webserve
2014-06-15 17:45:09 -04:00
import webapi
from sickbeard import logger
from sickbeard.helpers import create_https_certificates
from tornado.web import Application, StaticFileHandler, RedirectHandler, HTTPError
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
class MultiStaticFileHandler(StaticFileHandler):
def initialize(self, paths, default_filename=None):
self.paths = paths
self.default_filename = default_filename
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)
class SRWebServer(threading.Thread):
def __init__(self, options={}, io_loop=None):
threading.Thread.__init__(self)
self.daemon = True
self.alive = True
self.name = "TORNADO"
self.io_loop = io_loop or IOLoop.current()
self.options = options
self.options.setdefault('port', 8081)
self.options.setdefault('host', '0.0.0.0')
self.options.setdefault('log_dir', None)
self.options.setdefault('username', '')
self.options.setdefault('password', '')
self.options.setdefault('web_root', None)
assert isinstance(self.options['port'], int)
assert 'data_root' in self.options
# video root
2014-07-10 20:20:31 -04:00
if sickbeard.ROOT_DIRS:
root_dirs = sickbeard.ROOT_DIRS.split('|')
self.video_root = root_dirs[int(root_dirs[0]) + 1]
else:
self.video_root = None
# web root
self.options['web_root'] = ('/' + self.options['web_root'].lstrip('/')) if self.options[
'web_root'] else ''
# tornado setup
self.enable_https = self.options['enable_https']
self.https_cert = self.options['https_cert']
self.https_key = self.options['https_key']
if self.enable_https:
# If either the HTTPS certificate or key do not exist, make some self-signed ones.
if not (self.https_cert and os.path.exists(self.https_cert)) or not (
self.https_key and os.path.exists(self.https_key)):
if not create_https_certificates(self.https_cert, self.https_key):
logger.log(u"Unable to create CERT/KEY files, disabling HTTPS")
sickbeard.ENABLE_HTTPS = False
self.enable_https = False
if not (os.path.exists(self.https_cert) and os.path.exists(self.https_key)):
logger.log(u"Disabled HTTPS because of missing CERT and KEY files", logger.WARNING)
sickbeard.ENABLE_HTTPS = False
self.enable_https = False
# Load the app
self.app = Application([],
debug=True,
autoreload=False,
gzip=True,
xheaders=sickbeard.HANDLE_REVERSE_PROXY,
cookie_secret='61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo='
)
# Main Handler
self.app.add_handlers(".*$", [
2014-07-09 16:14:53 -04:00
(r'%s/api/(.*)(/?)' % self.options['web_root'], webapi.Api),
(r'%s/(.*)(/?)' % self.options['web_root'], webserve.MainHandler),
(r'(.*)', webserve.MainHandler)
])
# Static Path Handler
self.app.add_handlers(".*$", [
(r'%s/(favicon\.ico)' % self.options['web_root'], MultiStaticFileHandler,
{'paths': [os.path.join(self.options['data_root'], 'images/ico/favicon.ico')]}),
2014-07-09 16:14:53 -04:00
(r'%s/%s/(.*)(/?)' % (self.options['web_root'], 'images'), MultiStaticFileHandler,
{'paths': [os.path.join(self.options['data_root'], 'images'),
os.path.join(sickbeard.CACHE_DIR, 'images')]}),
2014-07-09 16:14:53 -04:00
(r'%s/%s/(.*)(/?)' % (self.options['web_root'], 'css'), MultiStaticFileHandler,
{'paths': [os.path.join(self.options['data_root'], 'css')]}),
2014-07-09 16:14:53 -04:00
(r'%s/%s/(.*)(/?)' % (self.options['web_root'], 'js'), MultiStaticFileHandler,
{'paths': [os.path.join(self.options['data_root'], 'js')]}),
])
# Static Videos Path
if self.video_root:
self.app.add_handlers(".*$", [
(r'%s/%s/(.*)' % (self.options['web_root'], 'videos'), MultiStaticFileHandler,
{'paths': [self.video_root]}),
])
def run(self):
if self.enable_https:
protocol = "https"
self.server = HTTPServer(self.app, no_keep_alive=True,
ssl_options={"certfile": self.https_cert, "keyfile": self.https_key})
else:
protocol = "http"
self.server = HTTPServer(self.app, no_keep_alive=True)
logger.log(u"Starting SickRage on " + protocol + "://" + str(self.options['host']) + ":" + str(
self.options['port']) + "/")
try:
self.server.listen(self.options['port'], self.options['host'])
except:
etype, evalue, etb = sys.exc_info()
logger.log(
"Could not start webserver on %s. Excpeption: %s, Error: %s" % (self.options['port'], etype, evalue),
logger.ERROR)
return
try:
self.io_loop.start()
self.io_loop.close(True)
Fixed issues with editing/saving custom scene exceptions. Fixed charmap issues for anime show names. Fixed issues with display show page and epCat key errors. Fixed duplicate log messages for clearing provider caches. Fixed issues with email notifier ep names not properly being encoded to UTF-8. TVDB<->TVRAGE Indexer ID mapping is now performed on demand to be used when needed such as newznab providers can be searched with tvrage_id's and some will return tvrage_id's that later can be used to create show objects from for faster and more accurate name parsing, mapping is done via Trakt API calls. Added stop event signals to schedualed tasks, SR now waits indefinate till task has been fully stopped before completing a restart or shutdown event. NameParserCache is now persistent and stores 200 parsed results at any given time for quicker lookups and better performance, this helps maintain results between updates or shutdown/startup events. Black and White lists for anime now only get used for anime shows as intended, performance gain for non-anime shows that dont need to load these lists. Internal name cache now builds it self on demand when needed per show request plus checks if show is already in cache and if true exits routine to save time. Schedualer and QueueItems classes are now a sub-class of threading.Thread and a stop threading event signal has been added to each. If I forgot to list something it doesn't mean its not fixed so please test and report back if anything is wrong or has been corrected by this new release.
2014-07-14 22:00:53 -04:00
except (IOError, ValueError):
# Ignore errors like "ValueError: I/O operation on closed kqueue fd". These might be thrown during a reload.
pass
def shutDown(self):
self.alive = False
if self.server:
logger.log("Shutting down tornado")
self.server.stop()
self.io_loop.stop()
self.join()