openmedialibrary/oml/server.py

191 lines
6.2 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-02 22:32:44 +00:00
2014-05-04 17:26:43 +00:00
import os
import sys
import signal
2014-09-05 17:02:19 +00:00
import time
2014-05-21 23:06:08 +00:00
2014-05-04 17:26:43 +00:00
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
2014-08-12 08:16:57 +00:00
from tornado.web import StaticFileHandler, Application
2014-05-04 17:26:43 +00:00
2014-08-12 08:16:57 +00:00
from cache import Cache
from item.handlers import EpubHandler, ReaderHandler, FileHandler
2015-06-06 17:54:44 +00:00
from item.handlers import OMLHandler, UploadHandler
2014-08-12 08:16:57 +00:00
from item.icons import IconHandler
2014-08-09 21:40:05 +00:00
import db
2014-05-04 17:26:43 +00:00
import node.server
2014-05-19 20:14:24 +00:00
import oxtornado
2014-08-12 08:16:57 +00:00
import settings
import setup
import state
2014-05-28 15:36:26 +00:00
import tasks
2014-08-12 08:16:57 +00:00
import websocket
2016-01-12 15:00:51 +00:00
import update
2014-05-21 22:50:59 +00:00
2014-08-12 08:16:57 +00:00
import logging
2014-05-21 23:06:08 +00:00
2015-11-29 14:56:38 +00:00
logger = logging.getLogger(__name__)
2015-03-22 14:04:42 +00:00
2014-05-21 23:06:08 +00:00
class MainHandler(OMLHandler):
def get(self, path):
2015-03-08 12:26:38 +00:00
version = settings.MINOR_VERSION.split('-')[0]
2014-09-05 17:02:19 +00:00
if version == 'git':
version = int(time.mktime(time.gmtime()))
2016-01-12 15:00:51 +00:00
path = os.path.join(settings.static_path, 'html', 'oml.html')
with open(path) as fd:
content = fd.read()
content = content.replace('.js?1', '.js?%s' % version)
if state.update:
content = content.replace('oml.js', 'oml.update.js')
2014-09-05 17:02:19 +00:00
self.set_header('Content-Type', 'text/html')
self.set_header('Content-Length', str(len(content)))
2016-01-14 04:03:26 +00:00
self.set_header('Cache-Control', 'no-cache, no-store, must-revalidate')
self.set_header('Pragma', 'no-cache')
self.set_header('Expires', '0')
2014-09-05 17:02:19 +00:00
self.write(content)
2014-05-04 17:26:43 +00:00
def log_request(handler):
if settings.DEBUG_HTTP:
if handler.get_status() < 400:
log_method = logger.info
elif handler.get_status() < 500:
log_method = logger.warning
else:
log_method = logger.error
request_time = 1000.0 * handler.request.request_time()
log_method("%d %s %.2fms", handler.get_status(),
handler._request_summary(), request_time)
2015-12-02 21:05:23 +00:00
def shutdown():
state.shutdown = True
if state.tor:
state.tor._shutdown = True
if state.nodes:
logger.debug('shutdown nodes')
state.nodes.join()
if state.downloads:
logger.debug('shutdown downloads')
state.downloads.join()
logger.debug('shutdown http_server')
state.http_server.stop()
if state.tasks:
logger.debug('shutdown tasks')
state.tasks.join()
2016-02-10 14:02:32 +00:00
if state.update:
logger.debug('shutdown updates')
state.update.join()
if state.node:
state.node.stop()
if state.tor:
logger.debug('shutdown tor')
state.tor.shutdown()
2016-02-10 14:02:32 +00:00
for peer in state.peers:
state.peers[peer].join()
if state.PID and os.path.exists(state.PID):
logger.debug('remove %s', state.PID)
os.unlink(state.PID)
2014-05-04 17:26:43 +00:00
def run():
PID = sys.argv[2] if len(sys.argv) > 2 else None
if len(sys.argv) > 3 and sys.argv[2] == 'debug':
PID = sys.argv[3]
debug = True
else:
debug = False
2014-05-04 17:26:43 +00:00
log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s'
if debug:
logging.basicConfig(level=logging.DEBUG, format=log_format)
2015-03-07 13:47:12 +00:00
else:
logging.basicConfig(level=logging.DEBUG,
handlers=[logging.FileHandler(settings.log_path, 'w', 'utf-8')],
format=log_format)
2014-05-04 17:26:43 +00:00
options = {
2014-05-26 10:10:56 +00:00
'debug': False,
'log_function': log_request,
2014-05-25 10:59:43 +00:00
'gzip': True
2014-05-04 17:26:43 +00:00
}
handlers = [
(r'/(favicon.ico)', StaticFileHandler, {'path': settings.static_path}),
2015-03-07 18:58:24 +00:00
(r'/static/oxjs/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'oxjs')}),
2015-03-14 07:35:15 +00:00
(r'/static/cbr.js/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'reader', 'cbr.js')}),
2015-03-07 20:08:57 +00:00
(r'/static/epub.js/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'reader', 'epub.js')}),
(r'/static/pdf.js/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'reader', 'pdf.js')}),
(r'/static/txt.js/(.*)', StaticFileHandler, {'path': os.path.join(settings.base_dir, '..', 'reader', 'txt.js')}),
(r'/static/(.*)', StaticFileHandler, {'path': settings.static_path}),
2014-08-09 16:33:59 +00:00
(r'/(.*)/epub/(.*)', EpubHandler),
(r'/(.*?)/reader/', ReaderHandler),
2015-03-14 07:35:15 +00:00
(r'/(.*?)/cbr/', FileHandler),
2014-08-09 16:33:59 +00:00
(r'/(.*?)/pdf/', FileHandler),
(r'/(.*?)/txt/', FileHandler),
2014-12-13 20:01:54 +00:00
(r'/(.*?)/get/', FileHandler, {
2015-05-14 11:08:16 +00:00
'attachment': True
2014-12-13 20:01:54 +00:00
}),
2014-08-09 16:33:59 +00:00
(r'/(.*)/(cover|preview)(\d*).jpg', IconHandler),
2015-06-06 17:54:44 +00:00
(r'/api/upload/', UploadHandler, dict(context=db.session)),
2014-08-09 21:40:05 +00:00
(r'/api/', oxtornado.ApiHandler, dict(context=db.session)),
2014-05-04 17:26:43 +00:00
(r'/ws', websocket.Handler),
2014-08-09 16:33:59 +00:00
(r"(.*)", MainHandler),
2014-05-04 17:26:43 +00:00
]
2016-01-18 05:23:59 +00:00
setup.create_db()
2014-05-04 17:26:43 +00:00
http_server = HTTPServer(Application(handlers, **options))
http_server.listen(settings.server['port'], settings.server['address'])
2014-05-17 00:14:15 +00:00
2014-05-04 17:26:43 +00:00
if PID:
with open(PID, 'w') as pid:
pid.write('%s' % os.getpid())
2016-01-12 15:00:51 +00:00
state.update = update.update_available()
state.PID = PID
state.http_server = http_server
2014-05-17 00:14:15 +00:00
state.main = IOLoop.instance()
2016-01-05 15:12:25 +00:00
state.cache = Cache(ttl=60)
2014-05-17 00:14:15 +00:00
2014-05-04 17:26:43 +00:00
def start_node():
import downloads
import nodes
import tor
2015-11-30 23:26:35 +00:00
import bandwidth
state.bandwidth = bandwidth.Bandwidth()
state.tor = tor.Tor()
2014-08-09 16:33:59 +00:00
state.node = node.server.start()
state.downloads = downloads.Downloads()
state.nodes = nodes.Nodes()
2015-12-02 21:05:23 +00:00
def publish():
if not state.tor.is_online():
2016-01-09 05:46:35 +00:00
state.main.call_later(10, publish)
else:
nodes.publish_node()
2015-12-02 21:05:23 +00:00
state.main.add_callback(publish)
2016-01-24 10:34:29 +00:00
state.main.call_later(10, lambda: state.tasks.queue('scanimport'))
2016-01-12 15:00:51 +00:00
if not state.update:
state.tasks = tasks.Tasks()
state.main.add_callback(start_node)
else:
2016-02-10 14:02:32 +00:00
state.update = update.Update()
2016-01-12 15:00:51 +00:00
2014-05-14 09:57:11 +00:00
if ':' in settings.server['address']:
host = '[%s]' % settings.server['address']
elif not settings.server['address']:
host = '127.0.0.1'
2014-05-14 09:57:11 +00:00
else:
host = settings.server['address']
url = 'http://%s:%s/' % (host, settings.server['port'])
2014-08-11 11:08:28 +00:00
print('open browser at %s' % url)
2015-03-22 14:04:42 +00:00
logger.debug('Starting OML %s at %s', settings.VERSION, url)
signal.signal(signal.SIGTERM, shutdown)
2014-08-09 18:32:41 +00:00
try:
state.main.start()
except:
2014-08-09 21:40:05 +00:00
print('shutting down...')
shutdown()