2018-07-09 16:47:36 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 18:37:45 +00:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2012-2019 janeczku, OzzieIsaacs, andrerfcsantos, idalin
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-06-06 15:10:22 +00:00
|
|
|
from __future__ import division, print_function, unicode_literals
|
2018-07-09 16:47:36 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2018-09-23 17:15:50 +00:00
|
|
|
import signal
|
2019-06-06 15:10:22 +00:00
|
|
|
import socket
|
2019-06-10 10:18:11 +00:00
|
|
|
import logging
|
2018-09-23 17:15:50 +00:00
|
|
|
|
2018-07-09 16:47:36 +00:00
|
|
|
try:
|
2019-01-26 18:06:54 +00:00
|
|
|
from gevent.pywsgi import WSGIServer
|
2018-07-09 16:47:36 +00:00
|
|
|
from gevent.pool import Pool
|
|
|
|
from gevent import __version__ as geventVersion
|
|
|
|
gevent_present = True
|
|
|
|
except ImportError:
|
|
|
|
from tornado.wsgi import WSGIContainer
|
|
|
|
from tornado.httpserver import HTTPServer
|
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
from tornado import version as tornadoVersion
|
2019-06-11 16:35:20 +00:00
|
|
|
from tornado import log as tornadoLog
|
|
|
|
from tornado import options as tornadoOptions
|
2018-07-09 16:47:36 +00:00
|
|
|
gevent_present = False
|
|
|
|
|
2019-06-06 15:10:22 +00:00
|
|
|
from . import logger, config, global_WorkerThread
|
|
|
|
|
|
|
|
|
2019-06-11 16:35:20 +00:00
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
|
2018-07-09 16:47:36 +00:00
|
|
|
class server:
|
|
|
|
|
|
|
|
wsgiserver = None
|
2019-02-08 19:11:44 +00:00
|
|
|
restart = False
|
2019-02-06 20:52:24 +00:00
|
|
|
app = None
|
2019-06-10 10:18:11 +00:00
|
|
|
access_logger = None
|
2018-07-09 16:47:36 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2018-09-23 17:15:50 +00:00
|
|
|
signal.signal(signal.SIGINT, self.killServer)
|
2018-09-30 07:43:20 +00:00
|
|
|
signal.signal(signal.SIGTERM, self.killServer)
|
2018-07-09 16:47:36 +00:00
|
|
|
|
2019-02-08 19:11:44 +00:00
|
|
|
def init_app(self, application):
|
2019-02-06 20:52:24 +00:00
|
|
|
self.app = application
|
2019-06-06 15:10:22 +00:00
|
|
|
self.port = config.config_port
|
2019-06-10 10:18:11 +00:00
|
|
|
self.listening = config.get_config_ipaddress(readable=True) + ":" + str(self.port)
|
2019-06-11 16:35:20 +00:00
|
|
|
self.access_logger = None
|
2019-06-10 10:18:11 +00:00
|
|
|
if config.config_access_log:
|
2019-06-11 16:35:20 +00:00
|
|
|
if gevent_present:
|
|
|
|
logger.setup(config.config_access_logfile, logger.DEFAULT_ACCESS_LEVEL, "access")
|
|
|
|
self.access_logger = logging.getLogger("access")
|
|
|
|
else:
|
|
|
|
logger.setup(config.config_access_logfile, logger.DEFAULT_ACCESS_LEVEL, "tornado.access")
|
2019-06-06 15:10:22 +00:00
|
|
|
|
|
|
|
self.ssl_args = None
|
|
|
|
certfile_path = config.get_config_certfile()
|
|
|
|
keyfile_path = config.get_config_keyfile()
|
|
|
|
if certfile_path and keyfile_path:
|
|
|
|
if os.path.isfile(certfile_path) and os.path.isfile(keyfile_path):
|
|
|
|
self.ssl_args = {"certfile": certfile_path,
|
|
|
|
"keyfile": keyfile_path}
|
|
|
|
else:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.warning('The specified paths for the ssl certificate file and/or key file seem to be broken. Ignoring ssl.')
|
|
|
|
log.warning('Cert path: %s', certfile_path)
|
|
|
|
log.warning('Key path: %s', keyfile_path)
|
2019-06-06 15:10:22 +00:00
|
|
|
|
|
|
|
def _make_gevent_socket(self):
|
2019-06-10 10:18:11 +00:00
|
|
|
if config.get_config_ipaddress():
|
|
|
|
return (config.get_config_ipaddress(), self.port)
|
2019-06-06 15:10:22 +00:00
|
|
|
if os.name == 'nt':
|
|
|
|
return ('0.0.0.0', self.port)
|
|
|
|
|
|
|
|
try:
|
|
|
|
s = WSGIServer.get_listener(('', self.port), family=socket.AF_INET6)
|
|
|
|
except socket.error as ex:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.error('%s', ex)
|
|
|
|
log.warning('Unable to listen on \'\', trying on IPv4 only...')
|
2019-06-06 15:10:22 +00:00
|
|
|
s = WSGIServer.get_listener(('', self.port), family=socket.AF_INET)
|
2019-06-11 16:35:20 +00:00
|
|
|
log.debug("%r %r", s._sock, s._sock.getsockname())
|
2019-06-06 15:10:22 +00:00
|
|
|
return s
|
2019-02-06 20:52:24 +00:00
|
|
|
|
2018-07-09 16:47:36 +00:00
|
|
|
def start_gevent(self):
|
2019-06-06 15:10:22 +00:00
|
|
|
ssl_args = self.ssl_args or {}
|
2019-06-11 16:35:20 +00:00
|
|
|
log.info('Starting Gevent server')
|
2019-06-06 15:10:22 +00:00
|
|
|
|
2018-07-09 16:47:36 +00:00
|
|
|
try:
|
2019-06-06 15:10:22 +00:00
|
|
|
sock = self._make_gevent_socket()
|
2019-06-10 10:18:11 +00:00
|
|
|
self.wsgiserver = WSGIServer(sock, self.app, log=self.access_logger, spawn=Pool(), **ssl_args)
|
2018-07-09 16:47:36 +00:00
|
|
|
self.wsgiserver.serve_forever()
|
2019-06-10 10:18:11 +00:00
|
|
|
except socket.error:
|
|
|
|
try:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.info('Unable to listen on "", trying on "0.0.0.0" only...')
|
2019-06-10 10:18:11 +00:00
|
|
|
self.wsgiserver = WSGIServer(('0.0.0.0', config.config_port), self.app, spawn=Pool(), **ssl_args)
|
|
|
|
self.wsgiserver.serve_forever()
|
|
|
|
except (OSError, socket.error) as e:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.info("Error starting server: %s", e.strerror)
|
2019-06-10 10:18:11 +00:00
|
|
|
print("Error starting server: %s" % e.strerror)
|
|
|
|
global_WorkerThread.stop()
|
|
|
|
sys.exit(1)
|
2018-08-12 07:29:57 +00:00
|
|
|
except Exception:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.exception("Unknown error while starting gevent")
|
2019-06-10 10:18:11 +00:00
|
|
|
sys.exit(0)
|
2019-06-06 15:10:22 +00:00
|
|
|
|
|
|
|
def start_tornado(self):
|
2019-06-11 16:35:20 +00:00
|
|
|
log.info('Starting Tornado server on %s', self.listening)
|
2019-06-06 15:10:22 +00:00
|
|
|
|
|
|
|
try:
|
2019-06-11 16:35:20 +00:00
|
|
|
# Max Buffersize set to 200MB )
|
2019-06-06 15:10:22 +00:00
|
|
|
http_server = HTTPServer(WSGIContainer(self.app),
|
|
|
|
max_buffer_size = 209700000,
|
|
|
|
ssl_options=self.ssl_args)
|
2019-06-10 10:18:11 +00:00
|
|
|
address = config.get_config_ipaddress()
|
|
|
|
http_server.listen(self.port, address)
|
2019-06-06 15:10:22 +00:00
|
|
|
self.wsgiserver=IOLoop.instance()
|
|
|
|
self.wsgiserver.start()
|
|
|
|
# wait for stop signal
|
|
|
|
self.wsgiserver.close(True)
|
|
|
|
except socket.error as err:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.exception("Error starting tornado server")
|
2019-06-06 15:10:22 +00:00
|
|
|
print("Error starting server: %s" % err.strerror)
|
|
|
|
global_WorkerThread.stop()
|
|
|
|
sys.exit(1)
|
2018-07-09 16:47:36 +00:00
|
|
|
|
|
|
|
def startServer(self):
|
|
|
|
if gevent_present:
|
|
|
|
# leave subprocess out to allow forking for fetchers and processors
|
|
|
|
self.start_gevent()
|
|
|
|
else:
|
2019-06-06 15:10:22 +00:00
|
|
|
self.start_tornado()
|
2018-07-09 16:47:36 +00:00
|
|
|
|
2019-02-08 19:11:44 +00:00
|
|
|
if self.restart is True:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.info("Performing restart of Calibre-Web")
|
2019-02-06 20:52:24 +00:00
|
|
|
global_WorkerThread.stop()
|
2018-07-09 16:47:36 +00:00
|
|
|
if os.name == 'nt':
|
|
|
|
arguments = ["\"" + sys.executable + "\""]
|
|
|
|
for e in sys.argv:
|
|
|
|
arguments.append("\"" + e + "\"")
|
|
|
|
os.execv(sys.executable, arguments)
|
|
|
|
else:
|
|
|
|
os.execl(sys.executable, sys.executable, *sys.argv)
|
|
|
|
else:
|
2019-06-11 16:35:20 +00:00
|
|
|
log.info("Performing shutdown of Calibre-Web")
|
2019-02-06 20:52:24 +00:00
|
|
|
global_WorkerThread.stop()
|
2018-07-09 16:47:36 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
def setRestartTyp(self,starttyp):
|
2019-01-23 17:14:45 +00:00
|
|
|
self.restart = starttyp
|
2018-07-09 16:47:36 +00:00
|
|
|
|
2018-09-23 17:15:50 +00:00
|
|
|
def killServer(self, signum, frame):
|
|
|
|
self.stopServer()
|
|
|
|
|
2018-07-09 16:47:36 +00:00
|
|
|
def stopServer(self):
|
2018-11-24 19:47:11 +00:00
|
|
|
if self.wsgiserver:
|
|
|
|
if gevent_present:
|
|
|
|
self.wsgiserver.close()
|
|
|
|
else:
|
|
|
|
self.wsgiserver.add_callback(self.wsgiserver.stop)
|
2018-07-09 16:47:36 +00:00
|
|
|
|
2018-08-12 07:29:57 +00:00
|
|
|
@staticmethod
|
|
|
|
def getNameVersion():
|
2018-07-09 16:47:36 +00:00
|
|
|
if gevent_present:
|
2019-02-08 19:11:44 +00:00
|
|
|
return {'Gevent': 'v' + geventVersion}
|
2018-07-09 16:47:36 +00:00
|
|
|
else:
|
2019-02-08 19:11:44 +00:00
|
|
|
return {'Tornado': 'v' + tornadoVersion}
|