openmedialibrary/oml/server.py

124 lines
3.5 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
2014-09-05 17:02:19 +00:00
from item.handlers import OMLHandler
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
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()
version = settings.MINOR_VERSION
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 run():
2014-08-11 17:32:49 +00:00
setup.create_db()
2014-05-04 17:26:43 +00:00
root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
PID = sys.argv[2] if len(sys.argv) > 2 else None
static_path = os.path.join(root_dir, 'static')
2014-05-27 14:53:06 +00:00
#FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
#logging.basicConfig(format=FORMAT)
#logger = logging.getLogger('oml.app')
#logger.warning('test')
if not PID:
logging.basicConfig(level=logging.DEBUG)
2014-05-04 17:26:43 +00:00
options = {
2014-05-26 10:10:56 +00:00
'debug': False,
2014-05-25 10:59:43 +00:00
'gzip': True
2014-05-04 17:26:43 +00:00
}
handlers = [
(r'/(favicon.ico)', StaticFileHandler, {'path': static_path}),
(r'/static/(.*)', StaticFileHandler, {'path': static_path}),
2014-08-09 16:33:59 +00:00
(r'/(.*)/epub/(.*)', EpubHandler),
(r'/(.*?)/reader/', ReaderHandler),
(r'/(.*?)/pdf/', FileHandler),
(r'/(.*?)/txt/', FileHandler),
(r'/(.*)/(cover|preview)(\d*).jpg', IconHandler),
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
2014-08-09 16:33:59 +00:00
state.node = node.server.start()
state.nodes = nodes.Nodes()
state.downloads = downloads.Downloads()
def add_users():
2014-08-09 16:14:14 +00:00
with db.session():
2014-05-04 17:26:43 +00:00
for p in user.models.User.query.filter_by(peered=True):
state.nodes.queue('add', p.id)
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 = '[::1]'
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)
def shutdown():
if state.downloads:
state.downloads.join()
if state.tasks:
state.tasks.join()
if state.nodes:
state.nodes.join()
http_server.stop()
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()