diff --git a/docs/sanic/deploying.md b/docs/sanic/deploying.md index 391c10da..d3652d0d 100644 --- a/docs/sanic/deploying.md +++ b/docs/sanic/deploying.md @@ -57,6 +57,12 @@ for Gunicorn `worker-class` argument: gunicorn myapp:app --bind 0.0.0.0:1337 --worker-class sanic.worker.GunicornWorker ``` +If your application suffers from memory leaks, you can configure Gunicorn to gracefully restart a worker +after it has processed a given number of requests. This can be a convenient way to help limit the effects +of the memory leak. + +See the [Gunicorn Docs](http://docs.gunicorn.org/en/latest/settings.html#max-requests) for more information. + ## Asynchronous support This is suitable if you *need* to share the sanic process with other applications, in particular the `loop`. However be advised that this method does not support using multiple processes, and is not the preferred way diff --git a/examples/aiohttp_example.py b/examples/aiohttp_example.py deleted file mode 100644 index 853ea959..00000000 --- a/examples/aiohttp_example.py +++ /dev/null @@ -1,26 +0,0 @@ -from sanic import Sanic -from sanic import response - -import aiohttp - -app = Sanic(__name__) - -async def fetch(session, url): - """ - Use session object to perform 'get' request on url - """ - async with session.get(url) as result: - return await result.json() - - -@app.route('/') -async def handle_request(request): - url = "https://api.github.com/repos/channelcat/sanic" - - async with aiohttp.ClientSession() as session: - result = await fetch(session, url) - return response.json(result) - - -if __name__ == '__main__': - app.run(host="0.0.0.0", port=8000, workers=2) diff --git a/examples/asyncorm/__init__.py b/examples/asyncorm/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/asyncorm/__main__.py b/examples/asyncorm/__main__.py deleted file mode 100644 index f7fca250..00000000 --- a/examples/asyncorm/__main__.py +++ /dev/null @@ -1,149 +0,0 @@ -from sanic import Sanic -from sanic.exceptions import NotFound, URLBuildError -from sanic.response import json -from sanic.views import HTTPMethodView - -from asyncorm import configure_orm -from asyncorm.exceptions import QuerysetError - -from library.models import Book -from library.serializer import BookSerializer - -app = Sanic(name=__name__) - - -@app.listener('before_server_start') -def orm_configure(sanic, loop): - db_config = {'database': 'sanic_example', - 'host': 'localhost', - 'user': 'ormdbuser', - 'password': 'ormDbPass', - } - - # configure_orm needs a dictionary with: - # * the database configuration - # * the application/s where the models are defined - orm_app = configure_orm({'loop': loop, # always use the sanic loop! - 'db_config': db_config, - 'modules': ['library', ], # list of apps - }) - - # orm_app is the object that orchestrates the whole ORM - # sync_db should be run only once, better do that as external command - # it creates the tables in the database!!!! - # orm_app.sync_db() - - -# for all the 404 lets handle the exceptions -@app.exception(NotFound) -def ignore_404s(request, exception): - return json({'method': request.method, - 'status': exception.status_code, - 'error': exception.args[0], - 'results': None, - }) - - -@app.exception(URLBuildError) -def ignore_urlbuilderrors(request, exception): - return json({'method': request.method, - 'status': exception.status_code, - 'error': exception.args[0], - 'results': None, - }) - - -# now the propper sanic workflow -class BooksView(HTTPMethodView): - - async def get(self, request): - filtered_by = request.raw_args - - if filtered_by: - try: - q_books = Book.objects.filter(**filtered_by) - except AttributeError as e: - raise URLBuildError(e.args[0]) - else: - q_books = Book.objects.all() - - books = [] - async for book in q_books: - books.append(BookSerializer.serialize(book)) - - return json({'method': request.method, - 'status': 200, - 'results': books or None, - 'count': len(books), - }) - - async def post(self, request): - # populate the book with the data in the request - book = Book(**request.json) - - # and await on save - await book.save() - - return json({'method': request.method, - 'status': 201, - 'results': BookSerializer.serialize(book), - }) - - -class BookView(HTTPMethodView): - async def get_object(self, request, book_id): - try: - # await on database consults - book = await Book.objects.get(**{'id': book_id}) - except QuerysetError as e: - raise NotFound(e.args[0]) - return book - - async def get(self, request, book_id): - # await on database consults - book = await self.get_object(request, book_id) - - return json({'method': request.method, - 'status': 200, - 'results': BookSerializer.serialize(book), - }) - - async def put(self, request, book_id): - # await on database consults - book = await self.get_object(request, book_id) - # await on save - await book.save(**request.json) - - return json({'method': request.method, - 'status': 200, - 'results': BookSerializer.serialize(book), - }) - - async def patch(self, request, book_id): - # await on database consults - book = await self.get_object(request, book_id) - # await on save - await book.save(**request.json) - - return json({'method': request.method, - 'status': 200, - 'results': BookSerializer.serialize(book), - }) - - async def delete(self, request, book_id): - # await on database consults - book = await self.get_object(request, book_id) - # await on its deletion - await book.delete() - - return json({'method': request.method, - 'status': 200, - 'results': None - }) - - -app.add_route(BooksView.as_view(), '/books/') -app.add_route(BookView.as_view(), '/books//') - -if __name__ == '__main__': - app.run(port=9000, debug=True) diff --git a/examples/asyncorm/library/__init__.py b/examples/asyncorm/library/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/asyncorm/library/models.py b/examples/asyncorm/library/models.py deleted file mode 100644 index b1a0749f..00000000 --- a/examples/asyncorm/library/models.py +++ /dev/null @@ -1,24 +0,0 @@ -from asyncorm import models - - -BOOK_CHOICES = ( - ('hard cover', 'hard cover book'), - ('paperback', 'paperback book') -) - - -# This is a simple model definition -class Book(models.Model): - name = models.CharField(max_length=50) - synopsis = models.CharField(max_length=255) - book_type = models.CharField( - max_length=15, - null=True, - choices=BOOK_CHOICES - ) - pages = models.IntegerField(null=True) - date_created = models.DateField(auto_now=True) - - class Meta(): - ordering = ['-name', ] - unique_together = ['name', 'synopsis'] diff --git a/examples/asyncorm/library/serializer.py b/examples/asyncorm/library/serializer.py deleted file mode 100644 index a31d5897..00000000 --- a/examples/asyncorm/library/serializer.py +++ /dev/null @@ -1,15 +0,0 @@ -from asyncorm.serializers import ModelSerializer, SerializerMethod -from library.models import Book - - -class BookSerializer(ModelSerializer): - book_type = SerializerMethod() - - def get_book_type(self, instance): - return instance.book_type_display() - - class Meta(): - model = Book - fields = [ - 'id', 'name', 'synopsis', 'book_type', 'pages', 'date_created' - ] diff --git a/examples/asyncorm/requirements.txt b/examples/asyncorm/requirements.txt deleted file mode 100644 index e9f21a88..00000000 --- a/examples/asyncorm/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -asyncorm>=0.0.9 -sanic==0.5.4 - diff --git a/examples/blueprints.py b/examples/blueprints.py index c8c9b43d..03154049 100644 --- a/examples/blueprints.py +++ b/examples/blueprints.py @@ -1,6 +1,6 @@ from sanic import Sanic from sanic import Blueprint -from sanic.response import json, text +from sanic.response import json app = Sanic(__name__) diff --git a/examples/cache_example.py b/examples/cache_example.py deleted file mode 100644 index 80b8edd8..00000000 --- a/examples/cache_example.py +++ /dev/null @@ -1,73 +0,0 @@ -""" -Example of caching using aiocache package. To run it you will need to install -aiocache with `pip install aiocache` plus a Redis instance running -in localhost:6379 - -Running this example you will see that the first call lasts 3 seconds and -the rest are instant because the value is retrieved from Redis. - -If you want more info about the package check -https://github.com/argaen/aiocache -""" - -import asyncio -import uuid - -from sanic import Sanic -from sanic.response import json -from sanic.log import log - -from aiocache import caches, cached - - -app = Sanic(__name__) - - -config = { - "default": { - "cache": "aiocache.RedisCache", - "endpoint": "127.0.0.1", - "timeout": 2, - "namespace": "sanic", - "serializer": { - "class": "aiocache.serializers.JsonSerializer" - } - } -} - - -@app.listener('before_server_start') -def init_cache(sanic, loop): - caches.set_config(config) - - -# You can use alias or pass explicit args instead -@cached(key="my_custom_key", ttl=30, alias="default") -async def expensive_call(): - log.info("Expensive has been called") - await asyncio.sleep(3) - # You are storing the whole dict under "my_custom_key" - return {"test": str(uuid.uuid4())} - - -async def get_cache_value(): - # This lazy loads a singleton so it will return the same instance every - # time. If you want to create a new instance, you can use - # `caches.create("default")` - cache = caches.get("default") - return await cache.get("my_custom_key") - - -@app.route("/") -async def test(request): - log.info("Received GET /") - return json(await expensive_call()) - - -@app.route("/retrieve") -async def test(request): - log.info("Received GET /retrieve") - return json(await get_cache_value()) - - -app.run(host="0.0.0.0", port=8000) diff --git a/examples/dask_distributed.py b/examples/dask_distributed.py deleted file mode 100644 index ef3fe423..00000000 --- a/examples/dask_distributed.py +++ /dev/null @@ -1,41 +0,0 @@ -from sanic import Sanic -from sanic import response - -from tornado.platform.asyncio import BaseAsyncIOLoop, to_asyncio_future -from distributed import LocalCluster, Client - - -app = Sanic(__name__) - - -def square(x): - return x**2 - - -@app.listener('after_server_start') -async def setup(app, loop): - # configure tornado use asyncio's loop - ioloop = BaseAsyncIOLoop(loop) - - # init distributed client - app.client = Client('tcp://localhost:8786', loop=ioloop, start=False) - await to_asyncio_future(app.client._start()) - - -@app.listener('before_server_stop') -async def stop(app, loop): - await to_asyncio_future(app.client._shutdown()) - - -@app.route('/') -async def test(request, value): - future = app.client.submit(square, value) - result = await to_asyncio_future(future._result()) - return response.text(f'The square of {value} is {result}') - - -if __name__ == '__main__': - # Distributed cluster should run somewhere else - with LocalCluster(scheduler_port=8786, nanny=False, n_workers=2, - threads_per_worker=1) as cluster: - app.run(host="0.0.0.0", port=8000) diff --git a/examples/detailed_example.py b/examples/detailed_example.py deleted file mode 100644 index 99e71cb1..00000000 --- a/examples/detailed_example.py +++ /dev/null @@ -1,136 +0,0 @@ -# This demo requires aioredis and environmental variables established in ENV_VARS -import json -import logging -import os - -from datetime import datetime - -import aioredis - -import sanic -from sanic import Sanic - - -ENV_VARS = ["REDIS_HOST", "REDIS_PORT", - "REDIS_MINPOOL", "REDIS_MAXPOOL", - "REDIS_PASS", "APP_LOGFILE"] - -app = Sanic(name=__name__) - -logger = None - - -@app.middleware("request") -async def log_uri(request): - # Simple middleware to log the URI endpoint that was called - logger.info("URI called: {0}".format(request.url)) - - -@app.listener('before_server_start') -async def before_server_start(app, loop): - logger.info("Starting redis pool") - app.redis_pool = await aioredis.create_pool( - (app.config.REDIS_HOST, int(app.config.REDIS_PORT)), - minsize=int(app.config.REDIS_MINPOOL), - maxsize=int(app.config.REDIS_MAXPOOL), - password=app.config.REDIS_PASS) - - -@app.listener('after_server_stop') -async def after_server_stop(app, loop): - logger.info("Closing redis pool") - app.redis_pool.close() - await app.redis_pool.wait_closed() - - -@app.middleware("request") -async def attach_db_connectors(request): - # Just put the db objects in the request for easier access - logger.info("Passing redis pool to request object") - request["redis"] = request.app.redis_pool - - -@app.route("/state/", methods=["GET"]) -async def access_state(request, user_id): - try: - # Check to see if the value is in cache, if so lets return that - with await request["redis"] as redis_conn: - state = await redis_conn.get(user_id, encoding="utf-8") - if state: - return sanic.response.json({"msg": "Success", - "status": 200, - "success": True, - "data": json.loads(state), - "finished_at": datetime.now().isoformat()}) - # Then state object is not in redis - logger.critical("Unable to find user_data in cache.") - return sanic.response.HTTPResponse({"msg": "User state not found", - "success": False, - "status": 404, - "finished_at": datetime.now().isoformat()}, status=404) - except aioredis.ProtocolError: - logger.critical("Unable to connect to state cache") - return sanic.response.HTTPResponse({"msg": "Internal Server Error", - "status": 500, - "success": False, - "finished_at": datetime.now().isoformat()}, status=500) - - -@app.route("/state//push", methods=["POST"]) -async def set_state(request, user_id): - try: - # Pull a connection from the pool - with await request["redis"] as redis_conn: - # Set the value in cache to your new value - await redis_conn.set(user_id, json.dumps(request.json), expire=1800) - logger.info("Successfully pushed state to cache") - return sanic.response.HTTPResponse({"msg": "Successfully pushed state to cache", - "success": True, - "status": 200, - "finished_at": datetime.now().isoformat()}) - except aioredis.ProtocolError: - logger.critical("Unable to connect to state cache") - return sanic.response.HTTPResponse({"msg": "Internal Server Error", - "status": 500, - "success": False, - "finished_at": datetime.now().isoformat()}, status=500) - - -def configure(): - # Setup environment variables - env_vars = [os.environ.get(v, None) for v in ENV_VARS] - if not all(env_vars): - # Send back environment variables that were not set - return False, ", ".join([ENV_VARS[i] for i, flag in env_vars if not flag]) - else: - # Add all the env vars to our app config - app.config.update({k: v for k, v in zip(ENV_VARS, env_vars)}) - setup_logging() - return True, None - - -def setup_logging(): - logging_format = "[%(asctime)s] %(process)d-%(levelname)s " - logging_format += "%(module)s::%(funcName)s():l%(lineno)d: " - logging_format += "%(message)s" - - logging.basicConfig( - filename=app.config.APP_LOGFILE, - format=logging_format, - level=logging.DEBUG) - - -def main(result, missing): - if result: - try: - app.run(host="0.0.0.0", port=8080, debug=True) - except: - logging.critical("User killed server. Closing") - else: - logging.critical("Unable to start. Missing environment variables [{0}]".format(missing)) - - -if __name__ == "__main__": - result, missing = configure() - logger = logging.getLogger() - main(result, missing) diff --git a/examples/exception_monitoring.py b/examples/exception_monitoring.py index 76d16d90..02a13e7d 100644 --- a/examples/exception_monitoring.py +++ b/examples/exception_monitoring.py @@ -37,7 +37,6 @@ server's error_handler to an instance of our CustomHandler """ from sanic import Sanic -from sanic import response app = Sanic(__name__) @@ -49,8 +48,7 @@ app.error_handler = handler async def test(request): # Here, something occurs which causes an unexpected exception # This exception will flow to our custom handler. - 1 / 0 - return response.json({"test": True}) + raise SanicException('You Broke It!') - -app.run(host="0.0.0.0", port=8000, debug=True) \ No newline at end of file +if __name__ == '__main__': + app.run(host="0.0.0.0", port=8000, debug=True) diff --git a/examples/jinja_example/jinja_example.py b/examples/jinja_example/jinja_example.py deleted file mode 100644 index a866489a..00000000 --- a/examples/jinja_example/jinja_example.py +++ /dev/null @@ -1,34 +0,0 @@ -# Render templates in a Flask like way from a "template" directory in -# the project - -from sanic import Sanic -from sanic import response -from jinja2 import Environment, PackageLoader, select_autoescape - -import sys -# Enabling async template execution which allows you to take advantage -# of newer Python features requires Python 3.6 or later. -enable_async = sys.version_info >= (3, 6) - - -app = Sanic(__name__) - -# Load the template environment with async support -template_env = Environment( - loader=PackageLoader('jinja_example', 'templates'), - autoescape=select_autoescape(['html', 'xml']), - enable_async=enable_async -) - -# Load the template from file -template = template_env.get_template("example_template.html") - - -@app.route('/') -async def test(request): - rendered_template = await template.render_async( - knights='that say nih; asynchronously') - return response.html(rendered_template) - - -app.run(host="0.0.0.0", port=8080, debug=True) diff --git a/examples/jinja_example/requirements.txt b/examples/jinja_example/requirements.txt deleted file mode 100644 index c7cd5408..00000000 --- a/examples/jinja_example/requirements.txt +++ /dev/null @@ -1,8 +0,0 @@ -aiofiles==0.3.1 -httptools==0.0.9 -Jinja2==2.9.6 -MarkupSafe==1.0 -sanic==0.5.2 -ujson==1.35 -uvloop==0.8.0 -websockets==3.3 diff --git a/examples/jinja_example/templates/example_template.html b/examples/jinja_example/templates/example_template.html deleted file mode 100644 index b914ad64..00000000 --- a/examples/jinja_example/templates/example_template.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - My Webpage - - -

Hello World

-

knights - {{ knights }}

- - diff --git a/examples/limit_concurrency.py b/examples/limit_concurrency.py index 8d38a813..f6b4b01a 100644 --- a/examples/limit_concurrency.py +++ b/examples/limit_concurrency.py @@ -8,11 +8,12 @@ app = Sanic(__name__) sem = None + @app.listener('before_server_start') def init(sanic, loop): global sem - CONCURRENCY_PER_WORKER = 4 - sem = asyncio.Semaphore(CONCURRENCY_PER_WORKER, loop=loop) + concurrency_per_worker = 4 + sem = asyncio.Semaphore(concurrency_per_worker, loop=loop) async def bounded_fetch(session, url): """ diff --git a/examples/modify_header_example.py b/examples/modify_header_example.py index bb5efe8e..f13e5f00 100644 --- a/examples/modify_header_example.py +++ b/examples/modify_header_example.py @@ -7,6 +7,7 @@ from sanic import response app = Sanic(__name__) + @app.route('/') def handle_request(request): return response.json( @@ -14,7 +15,8 @@ def handle_request(request): headers={'X-Served-By': 'sanic'}, status=200 ) - + + @app.route('/unauthorized') def handle_request(request): return response.json( diff --git a/examples/override_logging.py b/examples/override_logging.py index e4d529e8..99d26997 100644 --- a/examples/override_logging.py +++ b/examples/override_logging.py @@ -14,6 +14,8 @@ log = logging.getLogger() # Set logger to override default basicConfig sanic = Sanic() + + @sanic.route("/") def test(request): log.info("received request; responding with 'hey'") diff --git a/examples/plotly_example/plotlyjs_example.py b/examples/plotly_example/plotlyjs_example.py deleted file mode 100644 index d32491a6..00000000 --- a/examples/plotly_example/plotlyjs_example.py +++ /dev/null @@ -1,25 +0,0 @@ -from sanic import Sanic -from sanic.response import html -import plotly -import plotly.graph_objs as go - -app = Sanic(__name__) - - -@app.route('/') -async def index(request): - trace1 = go.Scatter( - x=[0, 1, 2, 3, 4, 5], - y=[1.5, 1, 1.3, 0.7, 0.8, 0.9] - ) - trace2 = go.Bar( - x=[0, 1, 2, 3, 4, 5], - y=[1, 0.5, 0.7, -1.2, 0.3, 0.4] - ) - - data = [trace1, trace2] - return html(plotly.offline.plot(data, auto_open=False, output_type='div')) - - -if __name__ == '__main__': - app.run(host='0.0.0.0', port=8000, debug=True) diff --git a/examples/plotly_example/requirements.txt b/examples/plotly_example/requirements.txt deleted file mode 100644 index 8e1e92c2..00000000 --- a/examples/plotly_example/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -plotly>=2.0.7 -sanic>=0.5.0 \ No newline at end of file diff --git a/examples/redirect_example.py b/examples/redirect_example.py index acc7d1ff..f73ad178 100644 --- a/examples/redirect_example.py +++ b/examples/redirect_example.py @@ -7,7 +7,8 @@ app = Sanic(__name__) @app.route('/') def handle_request(request): return response.redirect('/redirect') - + + @app.route('/redirect') async def test(request): return response.json({"Redirected": True}) diff --git a/examples/request_stream/client.py b/examples/request_stream/client.py index 30fcff06..a59c4c23 100644 --- a/examples/request_stream/client.py +++ b/examples/request_stream/client.py @@ -6,5 +6,5 @@ data = "" for i in range(1, 250000): data += str(i) -r = requests.post('http://127.0.0.1:8000/stream', data=data) +r = requests.post('http://0.0.0.0:8000/stream', data=data) print(r.text) diff --git a/examples/request_stream/server.py b/examples/request_stream/server.py index 37acfd5d..e53a224c 100644 --- a/examples/request_stream/server.py +++ b/examples/request_stream/server.py @@ -62,4 +62,4 @@ app.add_route(view, '/composition_view') if __name__ == '__main__': - app.run(host='127.0.0.1', port=8000) + app.run(host='0.0.0.0', port=8000) diff --git a/examples/request_timeout.py b/examples/request_timeout.py index fb2822ee..0c2c489c 100644 --- a/examples/request_timeout.py +++ b/examples/request_timeout.py @@ -18,4 +18,4 @@ async def test(request): def timeout(request, exception): return response.text('RequestTimeout from error_handler.', 408) -app.run(host='0.0.0.0', port=8000) \ No newline at end of file +app.run(host='0.0.0.0', port=8000) diff --git a/examples/run_async.py b/examples/run_async.py index b6f6d76d..3d8ab55a 100644 --- a/examples/run_async.py +++ b/examples/run_async.py @@ -1,12 +1,12 @@ from sanic import Sanic from sanic import response -from multiprocessing import Event from signal import signal, SIGINT import asyncio import uvloop app = Sanic(__name__) + @app.route("/") async def test(request): return response.json({"answer": "42"}) diff --git a/examples/sanic_aiomysql_with_global_pool.py b/examples/sanic_aiomysql_with_global_pool.py deleted file mode 100644 index aa6c680c..00000000 --- a/examples/sanic_aiomysql_with_global_pool.py +++ /dev/null @@ -1,60 +0,0 @@ -# encoding: utf-8 -""" -You need the aiomysql -""" -import os - -import aiomysql - -from sanic import Sanic -from sanic.response import json - -database_name = os.environ['DATABASE_NAME'] -database_host = os.environ['DATABASE_HOST'] -database_user = os.environ['DATABASE_USER'] -database_password = os.environ['DATABASE_PASSWORD'] -app = Sanic() - - -@app.listener("before_server_start") -async def get_pool(app, loop): - """ - the first param is the global instance , - so we can store our connection pool in it . - and it can be used by different request - :param args: - :param kwargs: - :return: - """ - app.pool = { - "aiomysql": await aiomysql.create_pool(host=database_host, user=database_user, password=database_password, - db=database_name, - maxsize=5)} - async with app.pool['aiomysql'].acquire() as conn: - async with conn.cursor() as cur: - await cur.execute('DROP TABLE IF EXISTS sanic_polls') - await cur.execute("""CREATE TABLE sanic_polls ( - id serial primary key, - question varchar(50), - pub_date timestamp - );""") - for i in range(0, 100): - await cur.execute("""INSERT INTO sanic_polls - (id, question, pub_date) VALUES ({}, {}, now()) - """.format(i, i)) - - -@app.route("/") -async def test(): - data = {} - async with app.pool['aiomysql'].acquire() as conn: - async with conn.cursor(aiomysql.DictCursor) as cur: - await cur.execute("SELECT question, pub_date FROM sanic_polls") - result = await cur.fetchall() - if result or len(result) > 0: - data['data'] = result - return json(data) - - -if __name__ == '__main__': - app.run(host="127.0.0.1", workers=4, port=12000) diff --git a/examples/sanic_aiopeewee.py b/examples/sanic_aiopeewee.py deleted file mode 100644 index 53146862..00000000 --- a/examples/sanic_aiopeewee.py +++ /dev/null @@ -1,61 +0,0 @@ -from sanic import Sanic -from sanic.response import json - -from aiopeewee import AioModel, AioMySQLDatabase, model_to_dict -from peewee import CharField, TextField, DateTimeField -from peewee import ForeignKeyField, PrimaryKeyField - - -db = AioMySQLDatabase('test', user='root', password='', - host='127.0.0.1', port=3306) - - -class User(AioModel): - username = CharField() - - class Meta: - database = db - - -class Blog(AioModel): - user = ForeignKeyField(User) - title = CharField(max_length=25) - content = TextField(default='') - pub_date = DateTimeField(null=True) - pk = PrimaryKeyField() - - class Meta: - database = db - - -app = Sanic(__name__) - - -@app.listener('before_server_start') -async def setup(app, loop): - # create connection pool - await db.connect(loop) - # create table if not exists - await db.create_tables([User, Blog], safe=True) - - -@app.listener('before_server_stop') -async def stop(app, loop): - # close connection pool - await db.close() - - -@app.post('/users') -async def add_user(request): - user = await User.create(**request.json) - return json(await model_to_dict(user)) - - -@app.get('/users/count') -async def user_count(request): - count = await User.select().count() - return json({'count': count}) - - -if __name__ == '__main__': - app.run(host="0.0.0.0", port=8000) diff --git a/examples/sanic_aiopg_example.py b/examples/sanic_aiopg_example.py deleted file mode 100644 index 16bde35f..00000000 --- a/examples/sanic_aiopg_example.py +++ /dev/null @@ -1,65 +0,0 @@ -""" To run this example you need additional aiopg package - -""" -import os -import asyncio - -import uvloop -import aiopg - -from sanic import Sanic -from sanic.response import json - -database_name = os.environ['DATABASE_NAME'] -database_host = os.environ['DATABASE_HOST'] -database_user = os.environ['DATABASE_USER'] -database_password = os.environ['DATABASE_PASSWORD'] - -connection = 'postgres://{0}:{1}@{2}/{3}'.format(database_user, - database_password, - database_host, - database_name) - - -async def get_pool(): - return await aiopg.create_pool(connection) - -app = Sanic(name=__name__) - -@app.listener('before_server_start') -async def prepare_db(app, loop): - """ - Let's create some table and add some data - """ - async with aiopg.create_pool(connection) as pool: - async with pool.acquire() as conn: - async with conn.cursor() as cur: - await cur.execute('DROP TABLE IF EXISTS sanic_polls') - await cur.execute("""CREATE TABLE sanic_polls ( - id serial primary key, - question varchar(50), - pub_date timestamp - );""") - for i in range(0, 100): - await cur.execute("""INSERT INTO sanic_polls - (id, question, pub_date) VALUES ({}, {}, now()) - """.format(i, i)) - - -@app.route("/") -async def handle(request): - result = [] - async def test_select(): - async with aiopg.create_pool(connection) as pool: - async with pool.acquire() as conn: - async with conn.cursor() as cur: - await cur.execute("SELECT question, pub_date FROM sanic_polls") - async for row in cur: - result.append({"question": row[0], "pub_date": row[1]}) - res = await test_select() - return json({'polls': result}) - -if __name__ == '__main__': - app.run(host='0.0.0.0', - port=8000, - debug=True) diff --git a/examples/sanic_aiopg_sqlalchemy_example.py b/examples/sanic_aiopg_sqlalchemy_example.py deleted file mode 100644 index 34bda28a..00000000 --- a/examples/sanic_aiopg_sqlalchemy_example.py +++ /dev/null @@ -1,67 +0,0 @@ -""" To run this example you need additional aiopg package - -""" -import os -import asyncio -import datetime - -import uvloop -from aiopg.sa import create_engine -import sqlalchemy as sa - -from sanic import Sanic -from sanic.response import json - -database_name = os.environ['DATABASE_NAME'] -database_host = os.environ['DATABASE_HOST'] -database_user = os.environ['DATABASE_USER'] -database_password = os.environ['DATABASE_PASSWORD'] - -connection = 'postgres://{0}:{1}@{2}/{3}'.format(database_user, - database_password, - database_host, - database_name) - -metadata = sa.MetaData() - -polls = sa.Table('sanic_polls', metadata, - sa.Column('id', sa.Integer, primary_key=True), - sa.Column('question', sa.String(50)), - sa.Column("pub_date", sa.DateTime)) - - -app = Sanic(name=__name__) - -@app.listener('before_server_start') -async def prepare_db(app, loop): - """ Let's add some data - - """ - async with create_engine(connection) as engine: - async with engine.acquire() as conn: - await conn.execute('DROP TABLE IF EXISTS sanic_polls') - await conn.execute("""CREATE TABLE sanic_polls ( - id serial primary key, - question varchar(50), - pub_date timestamp - );""") - for i in range(0, 100): - await conn.execute( - polls.insert().values(question=i, - pub_date=datetime.datetime.now()) - ) - - -@app.route("/") -async def handle(request): - async with create_engine(connection) as engine: - async with engine.acquire() as conn: - result = [] - async for row in conn.execute(polls.select()): - result.append({"question": row.question, - "pub_date": row.pub_date}) - return json({"polls": result}) - - -if __name__ == '__main__': - app.run(host='0.0.0.0', port=8000) diff --git a/examples/sanic_aioredis_example.py b/examples/sanic_aioredis_example.py deleted file mode 100644 index 8ba51617..00000000 --- a/examples/sanic_aioredis_example.py +++ /dev/null @@ -1,34 +0,0 @@ -""" To run this example you need additional aioredis package -""" -from sanic import Sanic, response -import aioredis - -app = Sanic(__name__) - - -@app.route("/") -async def handle(request): - async with request.app.redis_pool.get() as redis: - await redis.set('test-my-key', 'value') - val = await redis.get('test-my-key') - return response.text(val.decode('utf-8')) - - -@app.listener('before_server_start') -async def before_server_start(app, loop): - app.redis_pool = await aioredis.create_pool( - ('localhost', 6379), - minsize=5, - maxsize=10, - loop=loop - ) - - -@app.listener('after_server_stop') -async def after_server_stop(app, loop): - app.redis_pool.close() - await app.redis_pool.wait_closed() - - -if __name__ == '__main__': - app.run(host="0.0.0.0", port=8000) diff --git a/examples/sanic_asyncpg_example.py b/examples/sanic_asyncpg_example.py deleted file mode 100644 index 9e2b6c65..00000000 --- a/examples/sanic_asyncpg_example.py +++ /dev/null @@ -1,51 +0,0 @@ -import os -import asyncio - -import uvloop -from asyncpg import connect, create_pool - -from sanic import Sanic -from sanic.response import json - -DB_CONFIG = { - 'host': '', - 'user': '', - 'password': '', - 'port': '', - 'database': '' -} - - -def jsonify(records): - """ - Parse asyncpg record response into JSON format - """ - return [dict(r.items()) for r in records] - - -app = Sanic(__name__) - - -@app.listener('before_server_start') -async def register_db(app, loop): - app.pool = await create_pool(**DB_CONFIG, loop=loop, max_size=100) - async with app.pool.acquire() as connection: - await connection.execute('DROP TABLE IF EXISTS sanic_post') - await connection.execute("""CREATE TABLE sanic_post ( - id serial primary key, - content varchar(50), - post_date timestamp - );""") - for i in range(0, 1000): - await connection.execute(f"""INSERT INTO sanic_post - (id, content, post_date) VALUES ({i}, {i}, now())""") - - -@app.get('/') -async def root_get(request): - async with app.pool.acquire() as connection: - results = await connection.fetch('SELECT * FROM sanic_post') - return json({'posts': jsonify(results)}) - -if __name__ == '__main__': - app.run(host='127.0.0.1', port=8080) diff --git a/examples/sanic_motor.py b/examples/sanic_motor.py deleted file mode 100644 index c7d2b60f..00000000 --- a/examples/sanic_motor.py +++ /dev/null @@ -1,41 +0,0 @@ -""" sanic motor (async driver for mongodb) example -Required packages: -pymongo==3.4.0 -motor==1.1 -sanic==0.2.0 -""" -from sanic import Sanic -from sanic import response - - -app = Sanic('motor_mongodb') - - -def get_db(): - from motor.motor_asyncio import AsyncIOMotorClient - mongo_uri = "mongodb://127.0.0.1:27017/test" - client = AsyncIOMotorClient(mongo_uri) - return client['test'] - - -@app.route('/objects', methods=['GET']) -async def get(request): - db = get_db() - docs = await db.test_col.find().to_list(length=100) - for doc in docs: - doc['id'] = str(doc['_id']) - del doc['_id'] - return response.json(docs) - - -@app.route('/post', methods=['POST']) -async def new(request): - doc = request.json - print(doc) - db = get_db() - object_id = await db.test_col.save(doc) - return response.json({'object_id': str(object_id)}) - - -if __name__ == "__main__": - app.run(host='0.0.0.0', port=8000, debug=True) diff --git a/examples/sanic_peewee.py b/examples/sanic_peewee.py deleted file mode 100644 index aaa139f1..00000000 --- a/examples/sanic_peewee.py +++ /dev/null @@ -1,116 +0,0 @@ - -## You need the following additional packages for this example -# aiopg -# peewee_async -# peewee - - -## sanic imports -from sanic import Sanic -from sanic.response import json - -## peewee_async related imports -import peewee -from peewee import Model, BaseModel -from peewee_async import Manager, PostgresqlDatabase, execute -from functools import partial - # we instantiate a custom loop so we can pass it to our db manager - -## from peewee_async docs: -# Also there’s no need to connect and re-connect before executing async queries -# with manager! It’s all automatic. But you can run Manager.connect() or -# Manager.close() when you need it. - -class AsyncManager(Manager): - """Inherit the peewee_async manager with our own object - configuration - - database.allow_sync = False - """ - - def __init__(self, _model_class, *args, **kwargs): - super(AsyncManager, self).__init__(*args, **kwargs) - self._model_class = _model_class - self.database.allow_sync = False - - def _do_fill(self, method, *args, **kwargs): - _class_method = getattr(super(AsyncManager, self), method) - pf = partial(_class_method, self._model_class) - return pf(*args, **kwargs) - - def new(self, *args, **kwargs): - return self._do_fill('create', *args, **kwargs) - - def get(self, *args, **kwargs): - return self._do_fill('get', *args, **kwargs) - - def execute(self, query): - return execute(query) - - -def _get_meta_db_class(db): - """creating a declartive class model for db""" - class _BlockedMeta(BaseModel): - def __new__(cls, name, bases, attrs): - _instance = super(_BlockedMeta, cls).__new__(cls, name, bases, attrs) - _instance.objects = AsyncManager(_instance, db) - return _instance - - class _Base(Model, metaclass=_BlockedMeta): - - def to_dict(self): - return self._data - - class Meta: - database=db - return _Base - - -def declarative_base(*args, **kwargs): - """Returns a new Modeled Class after inheriting meta and Model classes""" - db = PostgresqlDatabase(*args, **kwargs) - return _get_meta_db_class(db) - - -AsyncBaseModel = declarative_base(database='test', - host='127.0.0.1', - user='postgres', - password='mysecretpassword') - -# let's create a simple key value store: -class KeyValue(AsyncBaseModel): - key = peewee.CharField(max_length=40, unique=True) - text = peewee.TextField(default='') - - -app = Sanic('peewee_example') - - -@app.route('/post//') -async def post(request, key, value): - """ - Save get parameters to database - """ - obj = await KeyValue.objects.new(key=key, text=value) - return json({'object_id': obj.id}) - - -@app.route('/get') -async def get(request): - """ - Load all objects from database - """ - all_objects = await KeyValue.objects.execute(KeyValue.select()) - serialized_obj = [] - for obj in all_objects: - serialized_obj.append({ - 'id': obj.id, - 'key': obj.key, - 'value': obj.text} - ) - - return json({'objects': serialized_obj}) - - -if __name__ == "__main__": - app.run(host='0.0.0.0', port=8000) diff --git a/examples/try_everything.py b/examples/try_everything.py index f380d925..2bc1c7b3 100644 --- a/examples/try_everything.py +++ b/examples/try_everything.py @@ -19,24 +19,27 @@ def test_sync(request): @app.route("/dynamic//") -def test_params(request, name, id): - return response.text("yeehaww {} {}".format(name, id)) +def test_params(request, name, i): + return response.text("yeehaww {} {}".format(name, i)) @app.route("/exception") def exception(request): raise ServerError("It's dead jim") + @app.route("/await") async def test_await(request): import asyncio await asyncio.sleep(5) return response.text("I'm feeling sleepy") + @app.route("/file") async def test_file(request): return await response.file(os.path.abspath("setup.py")) + @app.route("/file_stream") async def test_file_stream(request): return await response.file_stream(os.path.abspath("setup.py"), @@ -46,9 +49,11 @@ async def test_file_stream(request): # Exceptions # ----------------------------------------------- # + @app.exception(ServerError) async def test(request, exception): - return response.json({"exception": "{}".format(exception), "status": exception.status_code}, status=exception.status_code) + return response.json({"exception": "{}".format(exception), "status": exception.status_code}, + status=exception.status_code) # ----------------------------------------------- # @@ -67,7 +72,8 @@ def post_json(request): @app.route("/query_string") def query_string(request): - return response.json({"parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string}) + return response.json({"parsed": True, "args": request.args, "url": request.url, + "query_string": request.query_string}) # ----------------------------------------------- # diff --git a/examples/unix_socket.py b/examples/unix_socket.py index 070074fe..08e89445 100644 --- a/examples/unix_socket.py +++ b/examples/unix_socket.py @@ -1,11 +1,11 @@ from sanic import Sanic from sanic import response import socket -import sys import os app = Sanic(__name__) + @app.route("/test") async def test(request): return response.text("OK") diff --git a/examples/url_for_example.py b/examples/url_for_example.py index c26debf4..cb895b0c 100644 --- a/examples/url_for_example.py +++ b/examples/url_for_example.py @@ -3,6 +3,7 @@ from sanic import response app = Sanic(__name__) + @app.route('/') async def index(request): # generate a URL for the endpoint `post_handler` @@ -10,9 +11,10 @@ async def index(request): # the URL is `/posts/5`, redirect to it return response.redirect(url) + @app.route('/posts/') async def post_handler(request, post_id): return response.text('Post - {}'.format(post_id)) if __name__ == '__main__': - app.run(host="0.0.0.0", port=8000, debug=True) \ No newline at end of file + app.run(host="0.0.0.0", port=8000, debug=True) diff --git a/examples/vhosts.py b/examples/vhosts.py index 91b2b647..a6f946bc 100644 --- a/examples/vhosts.py +++ b/examples/vhosts.py @@ -11,20 +11,24 @@ from sanic.blueprints import Blueprint app = Sanic() bp = Blueprint("bp", host="bp.example.com") + @app.route('/', host=["example.com", "somethingelse.com", "therestofyourdomains.com"]) async def hello(request): return response.text("Some defaults") + @app.route('/', host="sub.example.com") async def hello(request): return response.text("42") + @bp.route("/question") async def hello(request): return response.text("What is the meaning of life?") + @bp.route("/answer") async def hello(request): return response.text("42") diff --git a/examples/websocket.py b/examples/websocket.py index 16cc4015..9cba083c 100644 --- a/examples/websocket.py +++ b/examples/websocket.py @@ -20,4 +20,5 @@ async def feed(request, ws): if __name__ == '__main__': - app.run() + app.run(host="0.0.0.0", port=8000, debug=True) + diff --git a/sanic/app.py b/sanic/app.py index 01af3526..ff680d9c 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -545,7 +545,7 @@ class Sanic: def run(self, host=None, port=None, debug=False, ssl=None, sock=None, workers=1, protocol=None, backlog=100, stop_event=None, register_sys_signals=True, - log_config=LOGGING): + log_config=None): """Run the HTTP Server and listen until keyboard interrupt or term signal. On termination, drain connections before closing. @@ -567,6 +567,7 @@ class Sanic: host, port = host or "127.0.0.1", port or 8000 if log_config: + self.log_config = log_config logging.config.dictConfig(log_config) if protocol is None: protocol = (WebSocketProtocol if self.websocket_enabled @@ -580,7 +581,7 @@ class Sanic: host=host, port=port, debug=debug, ssl=ssl, sock=sock, workers=workers, protocol=protocol, backlog=backlog, register_sys_signals=register_sys_signals, - has_log=log_config is not None) + has_log=self.log_config is not None) try: self.is_running = True diff --git a/sanic/exceptions.py b/sanic/exceptions.py index e1136dd1..95e41b4a 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -198,6 +198,34 @@ class InvalidRangeType(ContentRangeError): pass +@add_status_code(401) +class Unauthorized(SanicException): + """ + Unauthorized exception (401 HTTP status code). + + :param scheme: Name of the authentication scheme to be used. + :param realm: Description of the protected area. (optional) + :param challenge: A dict containing values to add to the WWW-Authenticate + header that is generated. This is especially useful when dealing with the + Digest scheme. (optional) + """ + pass + + def __init__(self, message, scheme, realm="", challenge=None): + super().__init__(message) + + adds = "" + + if challenge is not None: + values = ["{!s}={!r}".format(k, v) for k, v in challenge.items()] + adds = ', '.join(values) + adds = ', {}'.format(adds) + + self.headers = { + "WWW-Authenticate": "{} realm='{}'{}".format(scheme, realm, adds) + } + + def abort(status_code, message=None): """ Raise an exception based on SanicException. Returns the HTTP response diff --git a/sanic/request.py b/sanic/request.py index a5d204a8..29cb83f6 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -86,11 +86,15 @@ class Request(dict): :return: token related to request """ + prefixes = ('Token ', 'Bearer ') auth_header = self.headers.get('Authorization') - if auth_header is not None and 'Token ' in auth_header: - return auth_header.partition('Token ')[-1] - else: - return auth_header + + if auth_header is not None: + for prefix in prefixes: + if prefix in auth_header: + return auth_header.partition(prefix)[-1] + + return auth_header @property def form(self): @@ -178,6 +182,11 @@ class Request(dict): def content_type(self): return self.headers.get('Content-Type', DEFAULT_HTTP_CONTENT_TYPE) + @property + def match_info(self): + """return matched info after resolving route""" + return self.app.router.get(self)[2] + @property def path(self): return self._parsed_url.path.decode('utf-8') diff --git a/sanic/server.py b/sanic/server.py index f3106226..369e790e 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -75,7 +75,7 @@ class HttpProtocol(asyncio.Protocol): signal=Signal(), connections=set(), request_timeout=60, request_max_size=None, request_class=None, has_log=True, keep_alive=True, is_request_stream=False, router=None, - **kwargs): + state=None, **kwargs): self.loop = loop self.transport = None self.request = None @@ -99,6 +99,9 @@ class HttpProtocol(asyncio.Protocol): self._request_handler_task = None self._request_stream_task = None self._keep_alive = keep_alive + self.state = state if state else {} + if 'requests_count' not in self.state: + self.state['requests_count'] = 0 @property def keep_alive(self): @@ -154,6 +157,9 @@ class HttpProtocol(asyncio.Protocol): self.headers = [] self.parser = HttpRequestParser(self) + # requests count + self.state['requests_count'] = self.state['requests_count'] + 1 + # Parse request chunk or close connection try: self.parser.feed_data(data) @@ -389,7 +395,7 @@ def serve(host, port, request_handler, error_handler, before_start=None, register_sys_signals=True, run_async=False, connections=None, signal=Signal(), request_class=None, has_log=True, keep_alive=True, is_request_stream=False, router=None, websocket_max_size=None, - websocket_max_queue=None): + websocket_max_queue=None, state=None): """Start asynchronous HTTP Server on an individual process. :param host: Address to host on @@ -427,8 +433,6 @@ def serve(host, port, request_handler, error_handler, before_start=None, if debug: loop.set_debug(debug) - trigger_events(before_start, loop) - connections = connections if connections is not None else set() server = partial( protocol, @@ -445,7 +449,8 @@ def serve(host, port, request_handler, error_handler, before_start=None, is_request_stream=is_request_stream, router=router, websocket_max_size=websocket_max_size, - websocket_max_queue=websocket_max_queue + websocket_max_queue=websocket_max_queue, + state=state ) server_coroutine = loop.create_server( @@ -457,6 +462,7 @@ def serve(host, port, request_handler, error_handler, before_start=None, sock=sock, backlog=backlog ) + # Instead of pulling time at the end of every request, # pull it once per minute loop.call_soon(partial(update_current_time, loop)) @@ -464,6 +470,8 @@ def serve(host, port, request_handler, error_handler, before_start=None, if run_async: return server_coroutine + trigger_events(before_start, loop) + try: http_server = loop.run_until_complete(server_coroutine) except: diff --git a/sanic/testing.py b/sanic/testing.py index 09554e21..de26d025 100644 --- a/sanic/testing.py +++ b/sanic/testing.py @@ -32,7 +32,9 @@ class SanicTestClient: try: response.json = await response.json() - except (JSONDecodeError, UnicodeDecodeError): + except (JSONDecodeError, + UnicodeDecodeError, + aiohttp.ClientResponseError): response.json = None response.body = await response.read() diff --git a/sanic/worker.py b/sanic/worker.py index 1d3e384b..876354ce 100644 --- a/sanic/worker.py +++ b/sanic/worker.py @@ -29,7 +29,7 @@ class GunicornWorker(base.Worker): self.ssl_context = self._create_ssl_context(cfg) else: self.ssl_context = None - self.servers = [] + self.servers = {} self.connections = set() self.exit_code = 0 self.signal = Signal() @@ -96,11 +96,16 @@ class GunicornWorker(base.Worker): async def _run(self): for sock in self.sockets: - self.servers.append(await serve( + state = dict(requests_count=0) + self._server_settings["host"] = None + self._server_settings["port"] = None + server = await serve( sock=sock, connections=self.connections, + state=state, **self._server_settings - )) + ) + self.servers[server] = state async def _check_alive(self): # If our parent changed then we shut down. @@ -109,7 +114,15 @@ class GunicornWorker(base.Worker): while self.alive: self.notify() - if pid == os.getpid() and self.ppid != os.getppid(): + req_count = sum( + self.servers[srv]["requests_count"] for srv in self.servers + ) + if self.max_requests and req_count > self.max_requests: + self.alive = False + self.log.info( + "Max requests exceeded, shutting down: %s", self + ) + elif pid == os.getpid() and self.ppid != os.getppid(): self.alive = False self.log.info("Parent changed, shutting down: %s", self) else: @@ -166,3 +179,4 @@ class GunicornWorker(base.Worker): self.alive = False self.exit_code = 1 self.cfg.worker_abort(self) + sys.exit(1) diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index a2b8dc71..dcdecabd 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -3,7 +3,8 @@ from bs4 import BeautifulSoup from sanic import Sanic from sanic.response import text -from sanic.exceptions import InvalidUsage, ServerError, NotFound, abort +from sanic.exceptions import InvalidUsage, ServerError, NotFound, Unauthorized +from sanic.exceptions import abort class SanicExceptionTestException(Exception): @@ -26,6 +27,20 @@ def exception_app(): def handler_404(request): raise NotFound("OK") + @app.route('/401/basic') + def handler_401_basic(request): + raise Unauthorized("Unauthorized", "Basic", "Sanic") + + @app.route('/401/digest') + def handler_401_digest(request): + challenge = { + "qop": "auth, auth-int", + "algorithm": "MD5", + "nonce": "abcdef", + "opaque": "zyxwvu", + } + raise Unauthorized("Unauthorized", "Digest", "Sanic", challenge) + @app.route('/invalid') def handler_invalid(request): raise InvalidUsage("OK") @@ -49,8 +64,10 @@ def exception_app(): return app + def test_catch_exception_list(): app = Sanic('exception_list') + @app.exception([SanicExceptionTestException, NotFound]) def exception_list(request, exception): return text("ok") @@ -91,6 +108,25 @@ def test_not_found_exception(exception_app): assert response.status == 404 +def test_unauthorized_exception(exception_app): + """Test the built-in Unauthorized exception""" + request, response = exception_app.test_client.get('/401/basic') + assert response.status == 401 + assert response.headers.get('WWW-Authenticate') is not None + assert response.headers.get('WWW-Authenticate') == "Basic realm='Sanic'" + + request, response = exception_app.test_client.get('/401/digest') + assert response.status == 401 + + auth_header = response.headers.get('WWW-Authenticate') + assert auth_header is not None + assert auth_header.startswith('Digest') + assert "qop='auth, auth-int'" in auth_header + assert "algorithm='MD5'" in auth_header + assert "nonce='abcdef'" in auth_header + assert "opaque='zyxwvu'" in auth_header + + def test_handled_unhandled_exception(exception_app): """Test that an exception not built into sanic is handled""" request, response = exception_app.test_client.get('/divide_by_zero') diff --git a/tests/test_requests.py b/tests/test_requests.py index d61d4d55..671febeb 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -180,6 +180,16 @@ def test_token(): request, response = app.test_client.get('/', headers=headers) + assert request.token == token + + token = 'a1d895e0-553a-421a-8e22-5ff8ecb48cbf' + headers = { + 'content-type': 'application/json', + 'Authorization': 'Bearer {}'.format(token) + } + + request, response = app.test_client.get('/', headers=headers) + assert request.token == token # no Authorization headers @@ -211,6 +221,19 @@ def test_content_type(): assert response.text == 'application/json' +def test_match_info(): + app = Sanic('test_match_info') + + @app.route('/api/v1/user//') + async def handler(request, user_id): + return json(request.match_info) + + request, response = app.test_client.get('/api/v1/user/sanic_user/') + + assert request.match_info == {"user_id": "sanic_user"} + assert json_loads(response.text) == {"user_id": "sanic_user"} + + # ------------------------------------------------------------ # # POST # ------------------------------------------------------------ # diff --git a/tests/test_worker.py b/tests/test_worker.py index 2c1a0123..e1a13368 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -3,7 +3,11 @@ import json import shlex import subprocess import urllib.request - +from unittest import mock +from sanic.worker import GunicornWorker +from sanic.app import Sanic +import asyncio +import logging import pytest @@ -20,3 +24,79 @@ def test_gunicorn_worker(gunicorn_worker): with urllib.request.urlopen('http://localhost:1337/') as f: res = json.loads(f.read(100).decode()) assert res['test'] + + +class GunicornTestWorker(GunicornWorker): + + def __init__(self): + self.app = mock.Mock() + self.app.callable = Sanic("test_gunicorn_worker") + self.servers = {} + self.exit_code = 0 + self.cfg = mock.Mock() + self.notify = mock.Mock() + + +@pytest.fixture +def worker(): + return GunicornTestWorker() + + +def test_worker_init_process(worker): + with mock.patch('sanic.worker.asyncio') as mock_asyncio: + try: + worker.init_process() + except TypeError: + pass + + assert mock_asyncio.get_event_loop.return_value.close.called + assert mock_asyncio.new_event_loop.called + assert mock_asyncio.set_event_loop.called + + +def test_worker_init_signals(worker): + worker.loop = mock.Mock() + worker.init_signals() + assert worker.loop.add_signal_handler.called + + +def test_handle_abort(worker): + with mock.patch('sanic.worker.sys') as mock_sys: + worker.handle_abort(object(), object()) + assert not worker.alive + assert worker.exit_code == 1 + mock_sys.exit.assert_called_with(1) + + +def test_handle_quit(worker): + worker.handle_quit(object(), object()) + assert not worker.alive + assert worker.exit_code == 0 + + +def test_run_max_requests_exceeded(worker): + loop = asyncio.new_event_loop() + worker.ppid = 1 + worker.alive = True + sock = mock.Mock() + sock.cfg_addr = ('localhost', 8080) + worker.sockets = [sock] + worker.wsgi = mock.Mock() + worker.connections = set() + worker.log = mock.Mock() + worker.loop = loop + worker.servers = { + "server1": {"requests_count": 14}, + "server2": {"requests_count": 15}, + } + worker.max_requests = 10 + worker._run = mock.Mock(wraps=asyncio.coroutine(lambda *a, **kw: None)) + + # exceeding request count + _runner = asyncio.ensure_future(worker._check_alive(), loop=loop) + loop.run_until_complete(_runner) + + assert worker.alive == False + worker.notify.assert_called_with() + worker.log.info.assert_called_with("Max requests exceeded, shutting down: %s", + worker)