add additional examples to documentation
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
This commit is contained in:
parent
e3dfce88ff
commit
f6355bd075
|
@ -12,17 +12,18 @@ Basic Examples
|
||||||
This section of the examples are a collection of code that provide a simple use case example of the sanic application.
|
This section of the examples are a collection of code that provide a simple use case example of the sanic application.
|
||||||
|
|
||||||
Simple Apps
|
Simple Apps
|
||||||
^^^^^^^^^^^
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
A simple sanic application with a single ``async`` method with ``text`` and ``json`` type response.
|
A simple sanic application with a single ``async`` method with ``text`` and ``json`` type response.
|
||||||
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/teapot.py
|
.. literalinclude:: ../../examples/teapot.py
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/simple_server.py
|
.. literalinclude:: ../../examples/simple_server.py
|
||||||
|
|
||||||
|
|
||||||
Simple App with ``Sanic Views``
|
Simple App with ``Sanic Views``
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Showcasing the simple mechanism of using :class:`sanic.viewes.HTTPMethodView` as well as a way to extend the same
|
Showcasing the simple mechanism of using :class:`sanic.viewes.HTTPMethodView` as well as a way to extend the same
|
||||||
into providing a custom ``async`` behavior for ``view``.
|
into providing a custom ``async`` behavior for ``view``.
|
||||||
|
@ -31,13 +32,13 @@ into providing a custom ``async`` behavior for ``view``.
|
||||||
|
|
||||||
|
|
||||||
URL Redirect
|
URL Redirect
|
||||||
^^^^^^^^^^^^
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/redirect_example.py
|
.. literalinclude:: ../../examples/redirect_example.py
|
||||||
|
|
||||||
|
|
||||||
Named URL redirection
|
Named URL redirection
|
||||||
^^^^^^^^^^^^^^^^^^^^^
|
~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
``Sanic`` provides an easy to use way of redirecting the requests via a helper method called ``url_for`` that takes a
|
``Sanic`` provides an easy to use way of redirecting the requests via a helper method called ``url_for`` that takes a
|
||||||
unique url name as argument and returns you the actual route assigned for it. This will help in simplifying the
|
unique url name as argument and returns you the actual route assigned for it. This will help in simplifying the
|
||||||
|
@ -45,12 +46,74 @@ efforts required in redirecting the user between different section of the applic
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/url_for_example.py
|
.. literalinclude:: ../../examples/url_for_example.py
|
||||||
|
|
||||||
Custom Logging
|
Blueprints
|
||||||
^^^^^^^^^^^^^^
|
~~~~~~~~~~
|
||||||
|
``Sanic`` provides an amazing feature to group your APIs and routes under a logical collection that can easily be
|
||||||
|
imported and plugged into any of your sanic application and it's called ``blueprints``
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/blueprints.py
|
||||||
|
|
||||||
|
Logging Enhancements
|
||||||
|
~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Even though ``Sanic`` comes with a battery of Logging support it allows the end users to customize the way logging
|
Even though ``Sanic`` comes with a battery of Logging support it allows the end users to customize the way logging
|
||||||
is handled in the application runtime.
|
is handled in the application runtime.
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/override_logging.py
|
.. literalinclude:: ../../examples/override_logging.py
|
||||||
|
|
||||||
|
The following sample provides an example code that demonstrates the usage of :func:`sanic.app.Sanic.middleware` in order
|
||||||
|
to provide a mechanism to assign a unique request ID for each of the incoming requests and log them via
|
||||||
|
`aiotask-context <https://github.com/Skyscanner/aiotask-context>`_.
|
||||||
|
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/log_request_id.py
|
||||||
|
|
||||||
|
Sanic Streaming Support
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
``Sanic`` framework comes with in-built support for streaming large files and the following code explains the process
|
||||||
|
to setup a ``Sanic`` application with streaming support.
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/request_stream/server.py
|
||||||
|
|
||||||
|
Sample Client app to show the usage of streaming application by a client code.
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/request_stream/client.py
|
||||||
|
|
||||||
|
Sanic Concurrency Support
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
``Sanic`` supports the ability to start an app with multiple worker support. However, it's important to be able to limit
|
||||||
|
the concurrency per process/loop in order to ensure an efficient execution. The following section of the code provides a
|
||||||
|
brief example of how to limit the concurrency with the help of :class:`asyncio.Semaphore`
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/limit_concurrency.py
|
||||||
|
|
||||||
|
|
||||||
|
Sanic Deployment via Docker
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Deploying a ``sanic`` app via ``docker`` and ``docker-compose`` is an easy task to achieve and the following example
|
||||||
|
provides a deployment of the sample ``simple_server.py``
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/Dockerfile
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/docker-compose.yml
|
||||||
|
|
||||||
|
|
||||||
|
Monitoring and Error Handling
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
``Sanic`` provides an extendable bare minimum implementation of a global exception handler via
|
||||||
|
:class:`sanic.handlers.ErrorHandler`. This example shows how to extend it to enable some custom behaviors.
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/exception_monitoring.py
|
||||||
|
|
||||||
|
Security
|
||||||
|
~~~~~~~~
|
||||||
|
|
||||||
|
The following sample code shows a simple decorator based authentication and authorization mechanism that can be setup
|
||||||
|
to secure your ``sanic`` api endpoints.
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/authorized_sanic.py
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -38,13 +38,17 @@ A list of Sanic extensions created by the community.
|
||||||
|
|
||||||
- [pytest-sanic](https://github.com/yunstanford/pytest-sanic): A pytest plugin for Sanic. It helps you to test your code asynchronously.
|
- [pytest-sanic](https://github.com/yunstanford/pytest-sanic): A pytest plugin for Sanic. It helps you to test your code asynchronously.
|
||||||
|
|
||||||
|
## Project Creation Template
|
||||||
|
|
||||||
|
- [cookiecutter-sanic](https://github.com/harshanarayana/cookiecutter-sanic) Get your sanic application up and running in a matter of second in a well defined project structure.
|
||||||
|
Batteries included for deployment, unit testing, automated release management and changelog generation.
|
||||||
|
|
||||||
## Templating
|
## Templating
|
||||||
|
|
||||||
- [Sanic-WTF](https://github.com/pyx/sanic-wtf): Sanic-WTF makes using WTForms with Sanic and CSRF (Cross-Site Request Forgery) protection a little bit easier.
|
- [Sanic-WTF](https://github.com/pyx/sanic-wtf): Sanic-WTF makes using WTForms with Sanic and CSRF (Cross-Site Request Forgery) protection a little bit easier.
|
||||||
- [Jinja2](https://github.com/lixxu/sanic-jinja2): Support for Jinja2 template.
|
- [Jinja2](https://github.com/lixxu/sanic-jinja2): Support for Jinja2 template.
|
||||||
- [jinja2-sanic](https://github.com/yunstanford/jinja2-sanic): a jinja2 template renderer for Sanic.([Documentation](http://jinja2-sanic.readthedocs.io/en/latest/))
|
- [jinja2-sanic](https://github.com/yunstanford/jinja2-sanic): a jinja2 template renderer for Sanic.([Documentation](http://jinja2-sanic.readthedocs.io/en/latest/))
|
||||||
|
|
||||||
|
|
||||||
## API Helper Utilities
|
## API Helper Utilities
|
||||||
|
|
||||||
- [sanic-sse](https://github.com/inn0kenty/sanic_sse): [Server-Sent Events](https://en.wikipedia.org/wiki/Server-sent_events) implementation for Sanic.
|
- [sanic-sse](https://github.com/inn0kenty/sanic_sse): [Server-Sent Events](https://en.wikipedia.org/wiki/Server-sent_events) implementation for Sanic.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user