openmedialibrary/oml/server.py

177 lines
6 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
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):
path = os.path.join(settings.static_path, 'html', 'oml.html')
2014-09-05 17:02:19 +00:00
with open(path) as fd:
content = fd.read()
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()))
content = content.replace('oml.js?1', 'oml.js?%s' % version)
self.set_header('Content-Type', 'text/html')
self.set_header('Content-Length', str(len(content)))
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)
2014-05-04 17:26:43 +00:00
def run():
2014-08-11 17:32:49 +00:00
setup.create_db()
2014-05-04 17:26:43 +00:00
PID = sys.argv[2] if len(sys.argv) > 2 else None
log_format='%(asctime)s:%(levelname)s:%(name)s:%(message)s'
2014-05-27 14:53:06 +00:00
if not PID:
logging.basicConfig(level=logging.DEBUG, format=log_format)
2015-03-07 13:47:12 +00:00
else:
logging.basicConfig(level=logging.DEBUG,
filename=settings.log_path, filemode='w',
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
]
http_server = HTTPServer(Application(handlers, **options))
2014-05-17 00:14:15 +00:00
2014-05-04 17:26:43 +00:00
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())
2014-05-17 00:14:15 +00:00
state.main = IOLoop.instance()
2014-05-26 11:41:59 +00:00
state.cache = Cache(ttl=10)
2014-08-09 16:33:59 +00:00
state.tasks = tasks.Tasks()
2014-05-17 00:14:15 +00:00
2014-05-04 17:26:43 +00:00
def start_node():
import user
import downloads
import nodes
import tor
state.tor = tor.Tor()
2014-08-09 16:33:59 +00:00
state.node = node.server.start()
state.downloads = downloads.Downloads()
2015-03-07 16:24:07 +00:00
state.scraping = downloads.ScrapeThread()
state.nodes = nodes.Nodes()
2014-08-09 16:33:59 +00:00
def add_users():
if not state.tor.is_online():
state.main.add_callback(add_users)
else:
with db.session():
for u in user.models.User.query.filter_by(peered=True):
2015-11-29 14:56:38 +00:00
if 'local' in u.info:
del u.info['local']
u.save()
state.nodes.queue('add', u.id)
for u in user.models.User.query.filter_by(queued=True):
logger.debug('adding queued node... %s', u.id)
state.nodes.queue('add', u.id)
nodes.publish_node()
2014-08-09 16:33:59 +00:00
state.main.add_callback(add_users)
2014-05-04 17:26:43 +00:00
state.main.add_callback(start_node)
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)
def shutdown():
if state.tor:
state.tor._shutdown = True
if state.downloads:
2015-11-19 17:40:42 +00:00
logger.debug('shutdown downloads')
state.downloads.join()
2015-03-07 16:24:07 +00:00
if state.scraping:
2015-11-19 17:40:42 +00:00
logger.debug('shutdown scraping')
2015-03-07 16:24:07 +00:00
state.scraping.join()
2015-11-19 17:40:42 +00:00
logger.debug('shutdown http_server')
http_server.stop()
2015-11-18 00:27:53 +00:00
if state.tasks:
2015-11-19 17:40:42 +00:00
logger.debug('shutdown tasks')
2015-11-18 00:27:53 +00:00
state.tasks.join()
2015-11-19 12:51:26 +00:00
if state.nodes:
2015-11-19 17:40:42 +00:00
logger.debug('shutdown nodes')
2015-11-19 12:51:26 +00:00
state.nodes.join()
if state.node:
state.node.stop()
if state.tor:
logger.debug('shutdown tor')
state.tor.shutdown()
if PID and os.path.exists(PID):
2015-11-19 17:40:42 +00:00
logger.debug('remove %s', PID)
os.unlink(PID)
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()