openmedialibrary/oml/server.py

89 lines
2.7 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
import os
import sys
2014-05-21 23:06:08 +00:00
from tornado.web import StaticFileHandler, Application
2014-05-04 17:26:43 +00:00
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from app import app
import settings
import websocket
import state
import node.server
2014-05-19 20:14:24 +00:00
import oxtornado
2014-05-26 11:41:59 +00:00
from cache import Cache
2014-05-21 22:50:59 +00:00
2014-05-21 00:02:21 +00:00
from item.icons import IconHandler
2014-05-21 23:06:08 +00:00
from item.handlers import EpubHandler, ReaderHandler, FileHandler
from item.handlers import OMLHandler, serve_static
class MainHandler(OMLHandler):
def get(self, path):
path = os.path.join(settings.static_path, 'html/oml.html')
serve_static(self, path, 'text/html')
2014-05-04 17:26:43 +00:00
def run():
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')
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-05-19 22:49:04 +00:00
(r'/(.*)/epub/(.*)', EpubHandler, dict(app=app)),
2014-05-21 23:06:08 +00:00
(r'/(.*?)/reader/', ReaderHandler, dict(app=app)),
(r'/(.*?)/pdf/', FileHandler, dict(app=app)),
(r'/(.*?)/txt/', FileHandler, dict(app=app)),
2014-05-21 00:02:21 +00:00
(r'/(.*)/(cover|preview)(\d*).jpg', IconHandler, dict(app=app)),
2014-05-19 20:14:24 +00:00
(r'/api/', oxtornado.ApiHandler, dict(app=app)),
2014-05-04 17:26:43 +00:00
(r'/ws', websocket.Handler),
2014-05-21 23:06:08 +00:00
(r"(.*)", MainHandler, dict(app=app)),
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-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 tasks
state.tasks = tasks.Tasks(app)
2014-05-04 17:26:43 +00:00
state.node = node.server.start(app)
state.nodes = nodes.Nodes(app)
state.downloads = downloads.Downloads(app)
def add_users(app):
with app.app_context():
for p in user.models.User.query.filter_by(peered=True):
state.nodes.queue('add', p.id)
state.main.add_callback(add_users, app)
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'])
print 'open browser at %s' % url
2014-05-04 17:26:43 +00:00
state.main.start()