2017-01-28 01:34:21 +00:00
|
|
|
import logging
|
2017-04-11 10:59:07 +01:00
|
|
|
import logging.config
|
2017-02-09 01:37:32 +00:00
|
|
|
import re
|
2017-01-31 01:04:51 +00:00
|
|
|
import warnings
|
2017-03-16 05:27:13 +00:00
|
|
|
from asyncio import get_event_loop, ensure_future, CancelledError
|
2017-02-11 14:30:17 +00:00
|
|
|
from collections import deque, defaultdict
|
2016-10-21 12:11:18 +01:00
|
|
|
from functools import partial
|
2016-11-19 01:06:16 +00:00
|
|
|
from inspect import isawaitable, stack, getmodulename
|
2016-10-15 20:59:00 +01:00
|
|
|
from traceback import format_exc
|
2017-02-02 17:21:14 +00:00
|
|
|
from urllib.parse import urlencode, urlunparse
|
2017-04-08 21:31:11 +01:00
|
|
|
from ssl import create_default_context, Purpose
|
2016-10-15 20:59:00 +01:00
|
|
|
|
2017-04-13 05:49:45 +01:00
|
|
|
from sanic.config import Config, LOGGING
|
2017-02-16 02:54:00 +00:00
|
|
|
from sanic.constants import HTTP_METHODS
|
2017-02-17 12:54:26 +00:00
|
|
|
from sanic.exceptions import ServerError, URLBuildError, SanicException
|
2017-02-16 02:54:00 +00:00
|
|
|
from sanic.handlers import ErrorHandler
|
2017-04-12 10:08:06 +01:00
|
|
|
from sanic.log import log
|
2017-02-21 16:28:45 +00:00
|
|
|
from sanic.response import HTTPResponse, StreamingHTTPResponse
|
2017-02-16 02:54:00 +00:00
|
|
|
from sanic.router import Router
|
2017-04-19 03:19:01 +01:00
|
|
|
from sanic.server import serve, serve_multiple, HttpProtocol, Signal
|
2017-02-16 02:54:00 +00:00
|
|
|
from sanic.static import register as static_register
|
2017-03-08 00:39:26 +00:00
|
|
|
from sanic.testing import SanicTestClient
|
2017-02-16 02:54:00 +00:00
|
|
|
from sanic.views import CompositionView
|
2017-02-20 22:32:14 +00:00
|
|
|
from sanic.websocket import WebSocketProtocol, ConnectionClosed
|
2016-10-15 20:59:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Sanic:
|
2017-02-11 14:30:17 +00:00
|
|
|
|
2017-03-22 01:37:46 +00:00
|
|
|
def __init__(self, name=None, router=None, error_handler=None,
|
2017-04-12 10:01:12 +01:00
|
|
|
load_env=True, request_class=None,
|
2017-04-13 05:49:45 +01:00
|
|
|
log_config=LOGGING):
|
2017-04-12 10:44:47 +01:00
|
|
|
if log_config:
|
|
|
|
logging.config.dictConfig(log_config)
|
2017-04-12 10:52:01 +01:00
|
|
|
# Only set up a default log handler if the
|
|
|
|
# end-user application didn't set anything up.
|
2017-07-24 04:53:11 +01:00
|
|
|
if not logging.root.handlers and log.level == logging.NOTSET:
|
2017-01-20 22:09:27 +00:00
|
|
|
formatter = logging.Formatter(
|
|
|
|
"%(asctime)s: %(levelname)s: %(message)s")
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
log.addHandler(handler)
|
|
|
|
log.setLevel(logging.INFO)
|
2017-02-14 19:10:19 +00:00
|
|
|
|
|
|
|
# Get name from previous stack frame
|
2016-11-19 01:06:16 +00:00
|
|
|
if name is None:
|
|
|
|
frame_records = stack()[1]
|
|
|
|
name = getmodulename(frame_records[1])
|
2017-02-14 19:10:19 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
self.name = name
|
|
|
|
self.router = router or Router()
|
2017-04-09 18:29:21 +01:00
|
|
|
self.request_class = request_class
|
2017-01-31 01:04:51 +00:00
|
|
|
self.error_handler = error_handler or ErrorHandler()
|
2017-03-22 01:37:46 +00:00
|
|
|
self.config = Config(load_env=load_env)
|
2017-04-12 10:44:47 +01:00
|
|
|
self.log_config = log_config
|
2016-10-24 10:09:07 +01:00
|
|
|
self.request_middleware = deque()
|
|
|
|
self.response_middleware = deque()
|
2016-10-16 05:48:49 +01:00
|
|
|
self.blueprints = {}
|
|
|
|
self._blueprint_order = []
|
2016-10-21 12:11:18 +01:00
|
|
|
self.debug = None
|
2017-01-07 23:46:43 +00:00
|
|
|
self.sock = None
|
2017-02-11 14:30:17 +00:00
|
|
|
self.listeners = defaultdict(list)
|
2017-02-17 12:54:26 +00:00
|
|
|
self.is_running = False
|
2017-05-05 12:09:32 +01:00
|
|
|
self.is_request_stream = False
|
2017-02-20 22:32:14 +00:00
|
|
|
self.websocket_enabled = False
|
2017-03-16 05:27:13 +00:00
|
|
|
self.websocket_tasks = []
|
2016-10-15 20:59:00 +01:00
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
# Register alternative method names
|
|
|
|
self.go_fast = self.run
|
|
|
|
|
2017-02-12 00:27:38 +00:00
|
|
|
@property
|
|
|
|
def loop(self):
|
2017-02-18 18:29:25 +00:00
|
|
|
"""Synonymous with asyncio.get_event_loop().
|
|
|
|
|
|
|
|
Only supported when using the `app.run` method.
|
|
|
|
"""
|
2017-02-17 12:54:26 +00:00
|
|
|
if not self.is_running:
|
|
|
|
raise SanicException(
|
|
|
|
'Loop can only be retrieved after the app has started '
|
2017-02-18 18:29:25 +00:00
|
|
|
'running. Not supported with `create_server` function')
|
2017-02-12 00:27:38 +00:00
|
|
|
return get_event_loop()
|
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
# -------------------------------------------------------------------- #
|
|
|
|
# Registration
|
|
|
|
# -------------------------------------------------------------------- #
|
|
|
|
|
2017-02-12 20:07:59 +00:00
|
|
|
def add_task(self, task):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Schedule a task to run later, after the loop has started.
|
2017-02-11 22:52:40 +00:00
|
|
|
Different from asyncio.ensure_future in that it does not
|
|
|
|
also return a future, and the actual ensure_future call
|
|
|
|
is delayed until before server start.
|
|
|
|
|
2017-02-14 19:10:19 +00:00
|
|
|
:param task: future, couroutine or awaitable
|
2017-02-11 22:52:40 +00:00
|
|
|
"""
|
2017-02-13 22:17:58 +00:00
|
|
|
@self.listener('before_server_start')
|
2017-02-11 22:52:40 +00:00
|
|
|
def run(app, loop):
|
|
|
|
if callable(task):
|
2017-02-12 20:07:59 +00:00
|
|
|
loop.create_task(task())
|
2017-02-11 22:52:40 +00:00
|
|
|
else:
|
2017-02-12 20:07:59 +00:00
|
|
|
loop.create_task(task)
|
2017-02-11 22:52:40 +00:00
|
|
|
|
2017-02-11 14:30:17 +00:00
|
|
|
# Decorator
|
|
|
|
def listener(self, event):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Create a listener from a decorated function.
|
2017-02-11 14:30:17 +00:00
|
|
|
|
2017-02-14 19:10:19 +00:00
|
|
|
:param event: event to listen to
|
2017-02-11 14:30:17 +00:00
|
|
|
"""
|
|
|
|
def decorator(listener):
|
|
|
|
self.listeners[event].append(listener)
|
|
|
|
return listener
|
|
|
|
return decorator
|
2017-02-11 22:52:40 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
# Decorator
|
2017-03-16 04:11:45 +00:00
|
|
|
def route(self, uri, methods=frozenset({'GET'}), host=None,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=False, stream=False, version=None):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Decorate a function to be registered as a route
|
2016-12-25 09:43:45 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
:param uri: path of the URL
|
|
|
|
:param methods: list or tuple of methods allowed
|
2017-01-31 01:04:51 +00:00
|
|
|
:param host:
|
2017-05-05 12:09:32 +01:00
|
|
|
:param strict_slashes:
|
|
|
|
:param stream:
|
2016-10-15 20:59:00 +01:00
|
|
|
:return: decorated function
|
|
|
|
"""
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
# Fix case where the user did not prefix the URL with a /
|
|
|
|
# and will probably get confused as to why it's not working
|
|
|
|
if not uri.startswith('/'):
|
|
|
|
uri = '/' + uri
|
|
|
|
|
2017-05-05 12:09:32 +01:00
|
|
|
if stream:
|
|
|
|
self.is_request_stream = True
|
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
def response(handler):
|
2017-05-08 15:04:45 +01:00
|
|
|
if stream:
|
|
|
|
handler.is_stream = stream
|
2017-01-08 23:48:12 +00:00
|
|
|
self.router.add(uri=uri, methods=methods, handler=handler,
|
2017-07-13 04:18:56 +01:00
|
|
|
host=host, strict_slashes=strict_slashes,
|
|
|
|
version=version)
|
2016-10-15 20:59:00 +01:00
|
|
|
return handler
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2017-01-20 07:47:07 +00:00
|
|
|
# Shorthand method decorators
|
2017-07-13 04:18:56 +01:00
|
|
|
def get(self, uri, host=None, strict_slashes=False, version=None):
|
2017-03-16 04:11:45 +00:00
|
|
|
return self.route(uri, methods=frozenset({"GET"}), host=host,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=strict_slashes, version=version)
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-07-13 04:18:56 +01:00
|
|
|
def post(self, uri, host=None, strict_slashes=False, stream=False,
|
|
|
|
version=None):
|
2017-03-16 04:11:45 +00:00
|
|
|
return self.route(uri, methods=frozenset({"POST"}), host=host,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=strict_slashes, stream=stream,
|
|
|
|
version=version)
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-07-13 04:18:56 +01:00
|
|
|
def put(self, uri, host=None, strict_slashes=False, stream=False,
|
|
|
|
version=None):
|
2017-03-16 04:11:45 +00:00
|
|
|
return self.route(uri, methods=frozenset({"PUT"}), host=host,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=strict_slashes, stream=stream,
|
|
|
|
version=version)
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-07-13 04:18:56 +01:00
|
|
|
def head(self, uri, host=None, strict_slashes=False, version=None):
|
2017-03-16 04:11:45 +00:00
|
|
|
return self.route(uri, methods=frozenset({"HEAD"}), host=host,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=strict_slashes, version=version)
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-07-13 04:18:56 +01:00
|
|
|
def options(self, uri, host=None, strict_slashes=False, version=None):
|
2017-03-16 04:11:45 +00:00
|
|
|
return self.route(uri, methods=frozenset({"OPTIONS"}), host=host,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=strict_slashes, version=version)
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-07-13 04:18:56 +01:00
|
|
|
def patch(self, uri, host=None, strict_slashes=False, stream=False,
|
|
|
|
version=None):
|
2017-03-16 04:11:45 +00:00
|
|
|
return self.route(uri, methods=frozenset({"PATCH"}), host=host,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=strict_slashes, stream=stream,
|
|
|
|
version=version)
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-07-13 04:18:56 +01:00
|
|
|
def delete(self, uri, host=None, strict_slashes=False, version=None):
|
2017-03-16 04:11:45 +00:00
|
|
|
return self.route(uri, methods=frozenset({"DELETE"}), host=host,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=strict_slashes, version=version)
|
2017-01-28 04:05:46 +00:00
|
|
|
|
2017-03-16 04:11:45 +00:00
|
|
|
def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=False, version=None):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""A helper method to register class instance or
|
2016-11-25 07:29:25 +00:00
|
|
|
functions as a handler to the application url
|
|
|
|
routes.
|
2016-12-25 09:43:45 +00:00
|
|
|
|
2016-11-25 07:10:25 +00:00
|
|
|
:param handler: function or class instance
|
|
|
|
:param uri: path of the URL
|
2017-01-28 04:05:46 +00:00
|
|
|
:param methods: list or tuple of methods allowed, these are overridden
|
|
|
|
if using a HTTPMethodView
|
2017-01-31 01:04:51 +00:00
|
|
|
:param host:
|
2016-11-25 07:10:25 +00:00
|
|
|
:return: function or class instance
|
|
|
|
"""
|
2017-05-08 15:04:45 +01:00
|
|
|
stream = False
|
2017-01-28 04:05:46 +00:00
|
|
|
# Handle HTTPMethodView differently
|
|
|
|
if hasattr(handler, 'view_class'):
|
2017-02-02 21:24:16 +00:00
|
|
|
methods = set()
|
|
|
|
|
|
|
|
for method in HTTP_METHODS:
|
2017-05-08 15:04:45 +01:00
|
|
|
_handler = getattr(handler.view_class, method.lower(), None)
|
|
|
|
if _handler:
|
2017-02-02 21:24:16 +00:00
|
|
|
methods.add(method)
|
2017-05-08 15:04:45 +01:00
|
|
|
if hasattr(_handler, 'is_stream'):
|
|
|
|
stream = True
|
2017-02-02 21:24:16 +00:00
|
|
|
|
|
|
|
# handle composition view differently
|
|
|
|
if isinstance(handler, CompositionView):
|
|
|
|
methods = handler.handlers.keys()
|
2017-05-08 15:04:45 +01:00
|
|
|
for _handler in handler.handlers.values():
|
|
|
|
if hasattr(_handler, 'is_stream'):
|
|
|
|
stream = True
|
|
|
|
break
|
2017-02-02 21:24:16 +00:00
|
|
|
|
2017-03-16 04:11:45 +00:00
|
|
|
self.route(uri=uri, methods=methods, host=host,
|
2017-07-13 04:18:56 +01:00
|
|
|
strict_slashes=strict_slashes, stream=stream,
|
|
|
|
version=version)(handler)
|
2016-11-25 07:10:25 +00:00
|
|
|
return handler
|
|
|
|
|
2017-02-20 20:25:44 +00:00
|
|
|
# Decorator
|
2017-03-16 04:11:45 +00:00
|
|
|
def websocket(self, uri, host=None, strict_slashes=False):
|
2017-02-20 20:25:44 +00:00
|
|
|
"""Decorate a function to be registered as a websocket route
|
|
|
|
:param uri: path of the URL
|
|
|
|
:param host:
|
|
|
|
:return: decorated function
|
|
|
|
"""
|
2017-03-16 05:27:13 +00:00
|
|
|
self.enable_websocket()
|
2017-02-20 20:25:44 +00:00
|
|
|
|
|
|
|
# Fix case where the user did not prefix the URL with a /
|
|
|
|
# and will probably get confused as to why it's not working
|
|
|
|
if not uri.startswith('/'):
|
|
|
|
uri = '/' + uri
|
|
|
|
|
|
|
|
def response(handler):
|
|
|
|
async def websocket_handler(request, *args, **kwargs):
|
2017-02-28 06:33:18 +00:00
|
|
|
request.app = self
|
2017-05-15 03:50:13 +01:00
|
|
|
try:
|
|
|
|
protocol = request.transport.get_protocol()
|
|
|
|
except AttributeError:
|
|
|
|
# On Python3.5 the Transport classes in asyncio do not
|
|
|
|
# have a get_protocol() method as in uvloop
|
|
|
|
protocol = request.transport._protocol
|
2017-02-20 20:25:44 +00:00
|
|
|
ws = await protocol.websocket_handshake(request)
|
2017-03-16 05:27:13 +00:00
|
|
|
|
|
|
|
# schedule the application handler
|
|
|
|
# its future is kept in self.websocket_tasks in case it
|
|
|
|
# needs to be cancelled due to the server being stopped
|
|
|
|
fut = ensure_future(handler(request, ws, *args, **kwargs))
|
|
|
|
self.websocket_tasks.append(fut)
|
2017-02-20 20:25:44 +00:00
|
|
|
try:
|
2017-03-16 05:27:13 +00:00
|
|
|
await fut
|
|
|
|
except (CancelledError, ConnectionClosed):
|
2017-02-20 20:25:44 +00:00
|
|
|
pass
|
2017-03-16 05:27:13 +00:00
|
|
|
self.websocket_tasks.remove(fut)
|
2017-02-20 20:25:44 +00:00
|
|
|
await ws.close()
|
|
|
|
|
|
|
|
self.router.add(uri=uri, handler=websocket_handler,
|
2017-03-16 04:11:45 +00:00
|
|
|
methods=frozenset({'GET'}), host=host,
|
|
|
|
strict_slashes=strict_slashes)
|
2017-02-20 20:25:44 +00:00
|
|
|
return handler
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2017-03-16 04:11:45 +00:00
|
|
|
def add_websocket_route(self, handler, uri, host=None,
|
|
|
|
strict_slashes=False):
|
2017-02-20 22:32:14 +00:00
|
|
|
"""A helper method to register a function as a websocket route."""
|
2017-03-16 04:11:45 +00:00
|
|
|
return self.websocket(uri, host=host,
|
|
|
|
strict_slashes=strict_slashes)(handler)
|
2017-02-20 22:32:14 +00:00
|
|
|
|
|
|
|
def enable_websocket(self, enable=True):
|
|
|
|
"""Enable or disable the support for websocket.
|
|
|
|
|
|
|
|
Websocket is enabled automatically if websocket routes are
|
|
|
|
added to the application.
|
|
|
|
"""
|
2017-03-16 05:27:13 +00:00
|
|
|
if not self.websocket_enabled:
|
|
|
|
# if the server is stopped, we want to cancel any ongoing
|
|
|
|
# websocket tasks, to allow the server to exit promptly
|
|
|
|
@self.listener('before_server_stop')
|
|
|
|
def cancel_websocket_tasks(app, loop):
|
|
|
|
for task in self.websocket_tasks:
|
|
|
|
task.cancel()
|
|
|
|
|
2017-02-20 22:32:14 +00:00
|
|
|
self.websocket_enabled = enable
|
2017-02-20 20:25:44 +00:00
|
|
|
|
2017-01-08 23:48:12 +00:00
|
|
|
def remove_route(self, uri, clean_cache=True, host=None):
|
|
|
|
self.router.remove(uri, clean_cache, host)
|
2016-12-30 09:36:57 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
# Decorator
|
|
|
|
def exception(self, *exceptions):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Decorate a function to be registered as a handler for exceptions
|
2016-12-25 09:43:45 +00:00
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
:param exceptions: exceptions
|
2016-10-15 20:59:00 +01:00
|
|
|
:return: decorated function
|
|
|
|
"""
|
|
|
|
|
|
|
|
def response(handler):
|
|
|
|
for exception in exceptions:
|
2017-03-08 00:22:23 +00:00
|
|
|
if isinstance(exception, (tuple, list)):
|
|
|
|
for e in exception:
|
|
|
|
self.error_handler.add(e, handler)
|
|
|
|
else:
|
|
|
|
self.error_handler.add(exception, handler)
|
2016-10-15 20:59:00 +01:00
|
|
|
return handler
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2017-05-18 05:34:28 +01:00
|
|
|
def register_middleware(self, middleware, attach_to='request'):
|
|
|
|
if attach_to == 'request':
|
|
|
|
self.request_middleware.append(middleware)
|
|
|
|
if attach_to == 'response':
|
|
|
|
self.response_middleware.appendleft(middleware)
|
|
|
|
return middleware
|
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
# Decorator
|
2017-02-11 12:27:25 +00:00
|
|
|
def middleware(self, middleware_or_request):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Decorate and register middleware to be called before a request.
|
|
|
|
Can either be called as @app.middleware or @app.middleware('request')
|
2016-10-15 20:59:00 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Detect which way this was called, @middleware or @middleware('AT')
|
2017-02-11 12:27:25 +00:00
|
|
|
if callable(middleware_or_request):
|
2017-05-18 05:34:28 +01:00
|
|
|
return self.register_middleware(middleware_or_request)
|
2017-02-11 12:27:25 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
else:
|
2017-05-18 21:08:44 +01:00
|
|
|
return partial(self.register_middleware,
|
2017-02-11 12:39:04 +00:00
|
|
|
attach_to=middleware_or_request)
|
2016-10-15 20:59:00 +01:00
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
# Static Files
|
2017-04-13 05:11:38 +01:00
|
|
|
def static(self, uri, file_or_directory, pattern=r'/?.+',
|
2017-05-18 09:30:54 +01:00
|
|
|
use_modified_since=True, use_content_range=False,
|
|
|
|
stream_large_files=False):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Register a root to serve files from. The input can either be a
|
|
|
|
file or a directory. See
|
2016-10-24 09:21:06 +01:00
|
|
|
"""
|
2016-10-25 10:45:28 +01:00
|
|
|
static_register(self, uri, file_or_directory, pattern,
|
2017-05-18 09:30:54 +01:00
|
|
|
use_modified_since, use_content_range,
|
|
|
|
stream_large_files)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
def blueprint(self, blueprint, **options):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Register a blueprint on the application.
|
2016-12-25 09:43:45 +00:00
|
|
|
|
2016-10-15 20:38:12 +01:00
|
|
|
:param blueprint: Blueprint object
|
|
|
|
:param options: option dictionary with blueprint defaults
|
|
|
|
:return: Nothing
|
|
|
|
"""
|
|
|
|
if blueprint.name in self.blueprints:
|
|
|
|
assert self.blueprints[blueprint.name] is blueprint, \
|
2016-10-16 05:48:49 +01:00
|
|
|
'A blueprint with the name "%s" is already registered. ' \
|
|
|
|
'Blueprint names must be unique.' % \
|
|
|
|
(blueprint.name,)
|
2016-10-15 20:38:12 +01:00
|
|
|
else:
|
|
|
|
self.blueprints[blueprint.name] = blueprint
|
|
|
|
self._blueprint_order.append(blueprint)
|
|
|
|
blueprint.register(self, options)
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
def register_blueprint(self, *args, **kwargs):
|
|
|
|
# TODO: deprecate 1.0
|
2017-01-28 01:59:08 +00:00
|
|
|
if self.debug:
|
|
|
|
warnings.simplefilter('default')
|
|
|
|
warnings.warn("Use of register_blueprint will be deprecated in "
|
|
|
|
"version 1.0. Please use the blueprint method"
|
|
|
|
" instead",
|
|
|
|
DeprecationWarning)
|
2016-10-24 09:21:06 +01:00
|
|
|
return self.blueprint(*args, **kwargs)
|
|
|
|
|
2017-02-02 17:21:14 +00:00
|
|
|
def url_for(self, view_name: str, **kwargs):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Build a URL based on a view name and the values provided.
|
2017-02-02 17:52:48 +00:00
|
|
|
|
|
|
|
In order to build a URL, all request parameters must be supplied as
|
|
|
|
keyword arguments, and each parameter must pass the test for the
|
|
|
|
specified parameter type. If these conditions are not met, a
|
|
|
|
`URLBuildError` will be thrown.
|
|
|
|
|
|
|
|
Keyword arguments that are not request parameters will be included in
|
|
|
|
the output URL's query string.
|
|
|
|
|
2017-02-14 19:10:19 +00:00
|
|
|
:param view_name: string referencing the view name
|
2017-03-16 05:52:18 +00:00
|
|
|
:param \*\*kwargs: keys and values that are used to build request
|
2017-02-02 17:52:48 +00:00
|
|
|
parameters and query string arguments.
|
|
|
|
|
|
|
|
:return: the built URL
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
URLBuildError
|
|
|
|
"""
|
|
|
|
# find the route by the supplied view name
|
2017-02-02 17:21:14 +00:00
|
|
|
uri, route = self.router.find_route_by_view_name(view_name)
|
|
|
|
|
|
|
|
if not uri or not route:
|
|
|
|
raise URLBuildError(
|
|
|
|
'Endpoint with name `{}` was not found'.format(
|
|
|
|
view_name))
|
|
|
|
|
2017-02-23 16:45:50 +00:00
|
|
|
if uri != '/' and uri.endswith('/'):
|
2017-02-20 23:58:27 +00:00
|
|
|
uri = uri[:-1]
|
|
|
|
|
2017-02-02 17:21:14 +00:00
|
|
|
out = uri
|
2017-02-02 17:52:48 +00:00
|
|
|
|
|
|
|
# find all the parameters we will need to build in the URL
|
2017-02-02 17:21:14 +00:00
|
|
|
matched_params = re.findall(
|
|
|
|
self.router.parameter_pattern, uri)
|
|
|
|
|
2017-02-09 08:44:23 +00:00
|
|
|
# _method is only a placeholder now, don't know how to support it
|
|
|
|
kwargs.pop('_method', None)
|
|
|
|
anchor = kwargs.pop('_anchor', '')
|
|
|
|
# _external need SERVER_NAME in config or pass _server arg
|
|
|
|
external = kwargs.pop('_external', False)
|
|
|
|
scheme = kwargs.pop('_scheme', '')
|
|
|
|
if scheme and not external:
|
|
|
|
raise ValueError('When specifying _scheme, _external must be True')
|
|
|
|
|
2017-02-09 10:26:17 +00:00
|
|
|
netloc = kwargs.pop('_server', None)
|
|
|
|
if netloc is None and external:
|
|
|
|
netloc = self.config.get('SERVER_NAME', '')
|
|
|
|
|
2017-02-02 17:21:14 +00:00
|
|
|
for match in matched_params:
|
|
|
|
name, _type, pattern = self.router.parse_parameter_string(
|
|
|
|
match)
|
2017-02-02 17:52:48 +00:00
|
|
|
# we only want to match against each individual parameter
|
2017-02-02 17:21:14 +00:00
|
|
|
specific_pattern = '^{}$'.format(pattern)
|
|
|
|
supplied_param = None
|
2017-02-02 17:52:48 +00:00
|
|
|
|
2017-02-02 17:21:14 +00:00
|
|
|
if kwargs.get(name):
|
|
|
|
supplied_param = kwargs.get(name)
|
|
|
|
del kwargs[name]
|
|
|
|
else:
|
|
|
|
raise URLBuildError(
|
|
|
|
'Required parameter `{}` was not passed to url_for'.format(
|
|
|
|
name))
|
|
|
|
|
|
|
|
supplied_param = str(supplied_param)
|
2017-02-02 17:52:48 +00:00
|
|
|
# determine if the parameter supplied by the caller passes the test
|
|
|
|
# in the URL
|
2017-02-02 17:21:14 +00:00
|
|
|
passes_pattern = re.match(specific_pattern, supplied_param)
|
|
|
|
|
|
|
|
if not passes_pattern:
|
|
|
|
if _type != str:
|
|
|
|
msg = (
|
|
|
|
'Value "{}" for parameter `{}` does not '
|
|
|
|
'match pattern for type `{}`: {}'.format(
|
|
|
|
supplied_param, name, _type.__name__, pattern))
|
|
|
|
else:
|
|
|
|
msg = (
|
|
|
|
'Value "{}" for parameter `{}` '
|
|
|
|
'does not satisfy pattern {}'.format(
|
|
|
|
supplied_param, name, pattern))
|
|
|
|
raise URLBuildError(msg)
|
|
|
|
|
2017-02-02 17:52:48 +00:00
|
|
|
# replace the parameter in the URL with the supplied value
|
2017-02-02 17:21:14 +00:00
|
|
|
replacement_regex = '(<{}.*?>)'.format(name)
|
|
|
|
|
|
|
|
out = re.sub(
|
|
|
|
replacement_regex, supplied_param, out)
|
|
|
|
|
|
|
|
# parse the remainder of the keyword arguments into a querystring
|
2017-02-14 02:26:30 +00:00
|
|
|
query_string = urlencode(kwargs, doseq=True) if kwargs else ''
|
|
|
|
# scheme://netloc/path;parameters?query#fragment
|
|
|
|
out = urlunparse((scheme, netloc, out, '', query_string, anchor))
|
2017-02-02 17:21:14 +00:00
|
|
|
|
|
|
|
return out
|
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
# -------------------------------------------------------------------- #
|
|
|
|
# Request Handling
|
|
|
|
# -------------------------------------------------------------------- #
|
|
|
|
|
2016-10-21 12:11:18 +01:00
|
|
|
def converted_response_type(self, response):
|
|
|
|
pass
|
|
|
|
|
2017-02-21 16:28:45 +00:00
|
|
|
async def handle_request(self, request, write_callback, stream_callback):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Take a request from the HTTP Server and return a response object
|
|
|
|
to be sent back The HTTP Server only expects a response object, so
|
2016-10-16 14:01:59 +01:00
|
|
|
exception handling must be done here
|
2016-12-25 09:43:45 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
:param request: HTTP Request object
|
2017-02-21 16:28:45 +00:00
|
|
|
:param write_callback: Synchronous response function to be
|
|
|
|
called with the response as the only argument
|
|
|
|
:param stream_callback: Coroutine that handles streaming a
|
|
|
|
StreamingHTTPResponse if produced by the handler.
|
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
:return: Nothing
|
|
|
|
"""
|
|
|
|
try:
|
2016-10-21 12:11:18 +01:00
|
|
|
# -------------------------------------------- #
|
|
|
|
# Request Middleware
|
|
|
|
# -------------------------------------------- #
|
2017-04-12 10:52:01 +01:00
|
|
|
|
2017-02-22 17:34:14 +00:00
|
|
|
request.app = self
|
2017-04-04 15:55:38 +01:00
|
|
|
response = await self._run_request_middleware(request)
|
2016-10-15 20:59:00 +01:00
|
|
|
# No middleware results
|
|
|
|
if not response:
|
2016-10-21 12:11:18 +01:00
|
|
|
# -------------------------------------------- #
|
|
|
|
# Execute Handler
|
|
|
|
# -------------------------------------------- #
|
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
# Fetch handler from router
|
2017-04-28 20:06:59 +01:00
|
|
|
handler, args, kwargs, uri = self.router.get(request)
|
|
|
|
request.uri_template = uri
|
2016-10-15 20:59:00 +01:00
|
|
|
if handler is None:
|
2016-10-16 14:01:59 +01:00
|
|
|
raise ServerError(
|
|
|
|
("'None' was returned while requesting a "
|
|
|
|
"handler from the router"))
|
2016-10-15 20:59:00 +01:00
|
|
|
|
|
|
|
# Run response handler
|
|
|
|
response = handler(request, *args, **kwargs)
|
|
|
|
if isawaitable(response):
|
|
|
|
response = await response
|
|
|
|
except Exception as e:
|
2016-10-21 12:11:18 +01:00
|
|
|
# -------------------------------------------- #
|
|
|
|
# Response Generation Failed
|
|
|
|
# -------------------------------------------- #
|
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
try:
|
|
|
|
response = self.error_handler.response(request, e)
|
|
|
|
if isawaitable(response):
|
|
|
|
response = await response
|
|
|
|
except Exception as e:
|
|
|
|
if self.debug:
|
2016-10-16 14:01:59 +01:00
|
|
|
response = HTTPResponse(
|
|
|
|
"Error while handling error: {}\nStack: {}".format(
|
|
|
|
e, format_exc()))
|
2016-10-15 20:59:00 +01:00
|
|
|
else:
|
2016-10-16 14:01:59 +01:00
|
|
|
response = HTTPResponse(
|
2017-01-19 09:04:16 +00:00
|
|
|
"An error occurred while handling an error")
|
2017-04-04 15:55:38 +01:00
|
|
|
finally:
|
|
|
|
# -------------------------------------------- #
|
|
|
|
# Response Middleware
|
|
|
|
# -------------------------------------------- #
|
|
|
|
try:
|
2017-04-04 17:19:45 +01:00
|
|
|
response = await self._run_response_middleware(request,
|
|
|
|
response)
|
2017-04-04 15:55:38 +01:00
|
|
|
except:
|
|
|
|
log.exception(
|
|
|
|
'Exception occured in one of response middleware handlers'
|
|
|
|
)
|
|
|
|
|
2017-02-21 16:28:45 +00:00
|
|
|
# pass the response to the correct callback
|
|
|
|
if isinstance(response, StreamingHTTPResponse):
|
|
|
|
await stream_callback(response)
|
|
|
|
else:
|
|
|
|
write_callback(response)
|
2016-10-15 20:59:00 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
# -------------------------------------------------------------------- #
|
|
|
|
# Testing
|
|
|
|
# -------------------------------------------------------------------- #
|
|
|
|
|
|
|
|
@property
|
|
|
|
def test_client(self):
|
2017-03-08 00:39:26 +00:00
|
|
|
return SanicTestClient(self)
|
2017-02-14 19:51:20 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
# -------------------------------------------------------------------- #
|
|
|
|
# Execution
|
|
|
|
# -------------------------------------------------------------------- #
|
|
|
|
|
2017-05-21 11:14:58 +01:00
|
|
|
def run(self, host=None, port=None, debug=False, ssl=None,
|
2017-05-03 09:52:19 +01:00
|
|
|
sock=None, workers=1, protocol=None,
|
2017-04-11 10:59:07 +01:00
|
|
|
backlog=100, stop_event=None, register_sys_signals=True,
|
2017-06-15 18:39:00 +01:00
|
|
|
log_config=None):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Run the HTTP Server and listen until keyboard interrupt or term
|
|
|
|
signal. On termination, drain connections before closing.
|
2016-12-25 09:43:45 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
:param host: Address to host on
|
|
|
|
:param port: Port to host on
|
|
|
|
:param debug: Enables debug output (slows server)
|
2017-03-08 03:54:02 +00:00
|
|
|
:param ssl: SSLContext, or location of certificate and key
|
|
|
|
for SSL encryption of worker(s)
|
2016-10-18 09:22:49 +01:00
|
|
|
:param sock: Socket for the server to accept connections from
|
|
|
|
:param workers: Number of processes
|
2017-03-08 03:54:02 +00:00
|
|
|
received before it is respected
|
2017-01-31 01:04:51 +00:00
|
|
|
:param backlog:
|
|
|
|
:param stop_event:
|
|
|
|
:param register_sys_signals:
|
2016-12-29 04:11:27 +00:00
|
|
|
:param protocol: Subclass of asyncio protocol class
|
2016-10-15 20:59:00 +01:00
|
|
|
:return: Nothing
|
|
|
|
"""
|
2017-05-23 04:28:12 +01:00
|
|
|
if sock is None:
|
|
|
|
host, port = host or "127.0.0.1", port or 8000
|
|
|
|
|
2017-04-12 10:44:47 +01:00
|
|
|
if log_config:
|
2017-06-15 19:21:08 +01:00
|
|
|
self.log_config = log_config
|
2017-04-12 10:44:47 +01:00
|
|
|
logging.config.dictConfig(log_config)
|
2017-02-20 20:25:44 +00:00
|
|
|
if protocol is None:
|
2017-02-20 22:32:14 +00:00
|
|
|
protocol = (WebSocketProtocol if self.websocket_enabled
|
|
|
|
else HttpProtocol)
|
2017-02-27 00:31:39 +00:00
|
|
|
if stop_event is not None:
|
|
|
|
if debug:
|
|
|
|
warnings.simplefilter('default')
|
|
|
|
warnings.warn("stop_event will be removed from future versions.",
|
|
|
|
DeprecationWarning)
|
2017-01-29 22:01:00 +00:00
|
|
|
server_settings = self._helper(
|
2017-05-02 14:18:47 +01:00
|
|
|
host=host, port=port, debug=debug, ssl=ssl, sock=sock,
|
2017-05-03 09:52:19 +01:00
|
|
|
workers=workers, protocol=protocol, backlog=backlog,
|
2017-04-12 09:39:17 +01:00
|
|
|
register_sys_signals=register_sys_signals,
|
2017-06-15 19:21:08 +01:00
|
|
|
has_log=self.log_config is not None)
|
2017-02-11 14:30:17 +00:00
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
try:
|
2017-02-17 12:54:26 +00:00
|
|
|
self.is_running = True
|
2016-10-18 09:22:49 +01:00
|
|
|
if workers == 1:
|
|
|
|
serve(**server_settings)
|
|
|
|
else:
|
2017-02-27 00:31:39 +00:00
|
|
|
serve_multiple(server_settings, workers)
|
2017-02-17 12:54:26 +00:00
|
|
|
except:
|
2016-10-16 14:01:59 +01:00
|
|
|
log.exception(
|
2016-11-19 07:16:20 +00:00
|
|
|
'Experienced exception while trying to serve')
|
2017-04-28 18:08:51 +01:00
|
|
|
raise
|
2017-02-17 12:54:26 +00:00
|
|
|
finally:
|
|
|
|
self.is_running = False
|
2016-10-18 09:22:49 +01:00
|
|
|
log.info("Server Stopped")
|
|
|
|
|
2016-10-15 20:59:00 +01:00
|
|
|
def stop(self):
|
2017-01-28 01:34:21 +00:00
|
|
|
"""This kills the Sanic"""
|
2016-10-18 09:22:49 +01:00
|
|
|
get_event_loop().stop()
|
|
|
|
|
2017-03-12 08:19:34 +00:00
|
|
|
def __call__(self):
|
|
|
|
"""gunicorn compatibility"""
|
|
|
|
return self
|
|
|
|
|
2017-05-21 11:14:58 +01:00
|
|
|
async def create_server(self, host=None, port=None, debug=False,
|
2017-05-03 09:52:19 +01:00
|
|
|
ssl=None, sock=None, protocol=None,
|
2017-04-11 10:59:07 +01:00
|
|
|
backlog=100, stop_event=None,
|
2017-04-13 05:49:45 +01:00
|
|
|
log_config=LOGGING):
|
2017-02-17 12:54:26 +00:00
|
|
|
"""Asynchronous version of `run`.
|
|
|
|
|
|
|
|
NOTE: This does not support multiprocessing and is not the preferred
|
|
|
|
way to run a Sanic application.
|
|
|
|
"""
|
2017-05-23 04:28:12 +01:00
|
|
|
if sock is None:
|
|
|
|
host, port = host or "127.0.0.1", port or 8000
|
|
|
|
|
2017-04-12 10:44:47 +01:00
|
|
|
if log_config:
|
|
|
|
logging.config.dictConfig(log_config)
|
2017-02-20 20:25:44 +00:00
|
|
|
if protocol is None:
|
2017-02-20 22:32:14 +00:00
|
|
|
protocol = (WebSocketProtocol if self.websocket_enabled
|
|
|
|
else HttpProtocol)
|
2017-02-27 00:31:39 +00:00
|
|
|
if stop_event is not None:
|
|
|
|
if debug:
|
|
|
|
warnings.simplefilter('default')
|
|
|
|
warnings.warn("stop_event will be removed from future versions.",
|
|
|
|
DeprecationWarning)
|
2017-08-09 05:58:10 +01:00
|
|
|
|
2017-01-29 22:01:00 +00:00
|
|
|
server_settings = self._helper(
|
2017-05-02 14:18:47 +01:00
|
|
|
host=host, port=port, debug=debug, ssl=ssl, sock=sock,
|
2017-05-03 09:52:19 +01:00
|
|
|
loop=get_event_loop(), protocol=protocol,
|
2017-04-12 09:39:17 +01:00
|
|
|
backlog=backlog, run_async=True,
|
2017-04-12 10:44:47 +01:00
|
|
|
has_log=log_config is not None)
|
2017-01-28 22:54:15 +00:00
|
|
|
|
2017-08-09 05:58:10 +01:00
|
|
|
# Trigger before_start events
|
|
|
|
await self.trigger_events(server_settings.get('before_start', []), server_settings.get('loop'))
|
|
|
|
|
2017-01-28 22:54:15 +00:00
|
|
|
return await serve(**server_settings)
|
|
|
|
|
2017-08-09 05:58:10 +01:00
|
|
|
async def trigger_events(self, events, loop):
|
|
|
|
"""Trigger events (functions or async)
|
|
|
|
:param events: one or more sync or async functions to execute
|
|
|
|
:param loop: event loop
|
|
|
|
"""
|
|
|
|
for event in events:
|
|
|
|
result = event(loop)
|
|
|
|
if isawaitable(result):
|
|
|
|
await result
|
|
|
|
|
2017-04-04 15:55:38 +01:00
|
|
|
async def _run_request_middleware(self, request):
|
|
|
|
# The if improves speed. I don't know why
|
|
|
|
if self.request_middleware:
|
|
|
|
for middleware in self.request_middleware:
|
|
|
|
response = middleware(request)
|
|
|
|
if isawaitable(response):
|
|
|
|
response = await response
|
|
|
|
if response:
|
|
|
|
return response
|
|
|
|
return None
|
|
|
|
|
|
|
|
async def _run_response_middleware(self, request, response):
|
|
|
|
if self.response_middleware:
|
|
|
|
for middleware in self.response_middleware:
|
|
|
|
_response = middleware(request, response)
|
|
|
|
if isawaitable(_response):
|
|
|
|
_response = await _response
|
|
|
|
if _response:
|
|
|
|
response = _response
|
|
|
|
break
|
|
|
|
return response
|
|
|
|
|
2017-05-21 11:14:58 +01:00
|
|
|
def _helper(self, host=None, port=None, debug=False,
|
2017-05-02 14:18:47 +01:00
|
|
|
ssl=None, sock=None, workers=1, loop=None,
|
2017-01-28 22:54:15 +00:00
|
|
|
protocol=HttpProtocol, backlog=100, stop_event=None,
|
2017-04-12 10:44:47 +01:00
|
|
|
register_sys_signals=True, run_async=False, has_log=True):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Helper function used by `run` and `create_server`."""
|
2017-03-08 03:54:02 +00:00
|
|
|
if isinstance(ssl, dict):
|
|
|
|
# try common aliaseses
|
|
|
|
cert = ssl.get('cert') or ssl.get('certificate')
|
|
|
|
key = ssl.get('key') or ssl.get('keyfile')
|
2017-04-08 21:31:11 +01:00
|
|
|
if cert is None or key is None:
|
2017-03-08 03:54:02 +00:00
|
|
|
raise ValueError("SSLContext or certificate and key required.")
|
2017-04-08 21:31:11 +01:00
|
|
|
context = create_default_context(purpose=Purpose.CLIENT_AUTH)
|
2017-03-08 03:54:02 +00:00
|
|
|
context.load_cert_chain(cert, keyfile=key)
|
|
|
|
ssl = context
|
2017-02-27 00:31:39 +00:00
|
|
|
if stop_event is not None:
|
|
|
|
if debug:
|
|
|
|
warnings.simplefilter('default')
|
|
|
|
warnings.warn("stop_event will be removed from future versions.",
|
|
|
|
DeprecationWarning)
|
2017-01-28 01:59:08 +00:00
|
|
|
|
2017-02-04 07:54:37 +00:00
|
|
|
self.error_handler.debug = debug
|
|
|
|
self.debug = debug
|
|
|
|
|
2017-01-24 03:58:37 +00:00
|
|
|
server_settings = {
|
|
|
|
'protocol': protocol,
|
2017-04-09 18:29:21 +01:00
|
|
|
'request_class': self.request_class,
|
2017-05-05 12:09:32 +01:00
|
|
|
'is_request_stream': self.is_request_stream,
|
|
|
|
'router': self.router,
|
2017-01-24 03:58:37 +00:00
|
|
|
'host': host,
|
|
|
|
'port': port,
|
|
|
|
'sock': sock,
|
|
|
|
'ssl': ssl,
|
2017-04-20 10:27:28 +01:00
|
|
|
'signal': Signal(),
|
2017-01-24 03:58:37 +00:00
|
|
|
'debug': debug,
|
|
|
|
'request_handler': self.handle_request,
|
|
|
|
'error_handler': self.error_handler,
|
|
|
|
'request_timeout': self.config.REQUEST_TIMEOUT,
|
|
|
|
'request_max_size': self.config.REQUEST_MAX_SIZE,
|
2017-04-17 06:43:49 +01:00
|
|
|
'keep_alive': self.config.KEEP_ALIVE,
|
2017-01-24 03:58:37 +00:00
|
|
|
'loop': loop,
|
2017-01-28 22:54:15 +00:00
|
|
|
'register_sys_signals': register_sys_signals,
|
2017-04-12 09:39:17 +01:00
|
|
|
'backlog': backlog,
|
2017-05-26 04:11:26 +01:00
|
|
|
'has_log': has_log,
|
|
|
|
'websocket_max_size': self.config.WEBSOCKET_MAX_SIZE,
|
2017-06-25 08:51:14 +01:00
|
|
|
'websocket_max_queue': self.config.WEBSOCKET_MAX_QUEUE,
|
|
|
|
'graceful_shutdown_timeout': self.config.GRACEFUL_SHUTDOWN_TIMEOUT
|
2017-01-24 03:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# -------------------------------------------- #
|
|
|
|
# Register start/stop events
|
|
|
|
# -------------------------------------------- #
|
|
|
|
|
2017-05-02 14:18:47 +01:00
|
|
|
for event_name, settings_name, reverse in (
|
|
|
|
("before_server_start", "before_start", False),
|
|
|
|
("after_server_start", "after_start", False),
|
|
|
|
("before_server_stop", "before_stop", True),
|
|
|
|
("after_server_stop", "after_stop", True),
|
2017-01-31 01:04:51 +00:00
|
|
|
):
|
2017-02-11 14:30:17 +00:00
|
|
|
listeners = self.listeners[event_name].copy()
|
2017-01-24 03:58:37 +00:00
|
|
|
if reverse:
|
|
|
|
listeners.reverse()
|
|
|
|
# Prepend sanic to the arguments when listeners are triggered
|
|
|
|
listeners = [partial(listener, self) for listener in listeners]
|
|
|
|
server_settings[settings_name] = listeners
|
|
|
|
|
2017-01-28 22:54:15 +00:00
|
|
|
if debug:
|
|
|
|
log.setLevel(logging.DEBUG)
|
2016-12-23 10:42:00 +00:00
|
|
|
if self.config.LOGO is not None:
|
|
|
|
log.debug(self.config.LOGO)
|
2017-01-24 03:58:37 +00:00
|
|
|
|
2017-01-29 22:01:00 +00:00
|
|
|
if run_async:
|
|
|
|
server_settings['run_async'] = True
|
2017-01-24 03:58:37 +00:00
|
|
|
|
|
|
|
# Serve
|
2017-03-12 08:19:34 +00:00
|
|
|
if host and port:
|
|
|
|
proto = "http"
|
|
|
|
if ssl is not None:
|
|
|
|
proto = "https"
|
|
|
|
log.info('Goin\' Fast @ {}://{}:{}'.format(proto, host, port))
|
2017-01-24 03:58:37 +00:00
|
|
|
|
2017-01-28 22:54:15 +00:00
|
|
|
return server_settings
|