diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 5d969477..b08a60db 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -68,7 +68,7 @@ run all unittests, perform lint and other checks. Run unittests ------------- -``tox`` environment -> ``[testenv]` +``tox`` environment -> ``[testenv]`` To execute only unittests, run ``tox`` with environment like so: diff --git a/changelogs/1691.doc.rst b/changelogs/1691.doc.rst new file mode 100644 index 00000000..e4a9e3de --- /dev/null +++ b/changelogs/1691.doc.rst @@ -0,0 +1,4 @@ +Move docs from RST to MD + +Moved all docs from markdown to restructured text like the rest of the docs to unify the scheme and make it easier in +the future to update documentation. diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 00000000..54ca5ba3 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,468 @@ + + + +
+ + +Sanic is a Python 3.6+ web server and web framework that's written to go fast. It allows the usage of the async/await syntax added in Python 3.5, which makes your code non-blocking and speedy.
+The goal of the project is to provide a simple way to get up and running a highly performant HTTP server that is easy to build, to expand, and ultimately to scale.
+Sanic is developed on GitHub. Contributions are welcome!
++from sanic import Sanic +from sanic.response import json + +app = Sanic() + +@app.route("/") +async def test(request): + return json({"hello": "world"}) + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000) ++
Note
+Sanic does not support Python 3.5 from version 19.6 and forward. However, version 18.12LTS is supported thru +December 2020. Official Python support for version 3.5 is set to expire in September 2020.
+Hello world!
') -``` - -## JSON - - -```python -from sanic import response - - -@app.route('/json') -def handle_request(request): - return response.json({'message': 'Hello world!'}) -``` - -## File - -```python -from sanic import response - - -@app.route('/file') -async def handle_request(request): - return await response.file('/srv/www/whatever.png') -``` - -## Streaming - -```python -from sanic import response - -@app.route("/streaming") -async def index(request): - async def streaming_fn(response): - await response.write('foo') - await response.write('bar') - return response.stream(streaming_fn, content_type='text/plain') -``` - -See [Streaming](streaming.md) for more information. - -## File Streaming -For large files, a combination of File and Streaming above -```python -from sanic import response - -@app.route('/big_file.png') -async def handle_request(request): - return await response.file_stream('/srv/www/whatever.png') -``` - -## Redirect - -```python -from sanic import response - - -@app.route('/redirect') -def handle_request(request): - return response.redirect('/json') -``` - -## Raw - -Response without encoding the body - -```python -from sanic import response - - -@app.route('/raw') -def handle_request(request): - return response.raw(b'raw data') -``` - -## Modify headers or status - -To modify headers or status code, pass the `headers` or `status` argument to those functions: - -```python -from sanic import response - - -@app.route('/json') -def handle_request(request): - return response.json( - {'message': 'Hello world!'}, - headers={'X-Served-By': 'sanic'}, - status=200 - ) -``` diff --git a/docs/sanic/response.rst b/docs/sanic/response.rst new file mode 100644 index 00000000..75a44425 --- /dev/null +++ b/docs/sanic/response.rst @@ -0,0 +1,126 @@ +Response +======== + +Use functions in `sanic.response` module to create responses. + +Plain Text +---------- + +.. code-block:: python + + from sanic import response + + + @app.route('/text') + def handle_request(request): + return response.text('Hello world!') + +HTML +---- + +.. code-block:: python + + from sanic import response + + + @app.route('/html') + def handle_request(request): + return response.html('Hello world!
') + +JSON +---- + + +.. code-block:: python + + from sanic import response + + + @app.route('/json') + def handle_request(request): + return response.json({'message': 'Hello world!'}) + +File +---- + +.. code-block:: python + + from sanic import response + + + @app.route('/file') + async def handle_request(request): + return await response.file('/srv/www/whatever.png') + +Streaming +--------- + +.. code-block:: python + + from sanic import response + + @app.route("/streaming") + async def index(request): + async def streaming_fn(response): + await response.write('foo') + await response.write('bar') + return response.stream(streaming_fn, content_type='text/plain') + +See `StreamingHello world!
') -``` - -Then with curl: - -```bash -curl localhost/v1/html -``` diff --git a/docs/sanic/versioning.rst b/docs/sanic/versioning.rst new file mode 100644 index 00000000..0d748a78 --- /dev/null +++ b/docs/sanic/versioning.rst @@ -0,0 +1,54 @@ +Versioning +========== + +You can pass the `version` keyword to the route decorators, or to a blueprint initializer. It will result in the `v{version}` url prefix where `{version}` is the version number. + +Per route +--------- + +You can pass a version number to the routes directly. + +.. code-block:: python + + from sanic import response + + + @app.route('/text', version=1) + def handle_request(request): + return response.text('Hello world! Version 1') + + @app.route('/text', version=2) + def handle_request(request): + return response.text('Hello world! Version 2') + + app.run(port=80) + +Then with curl: + +.. code-block:: bash + + curl localhost/v1/text + curl localhost/v2/text + + +Global blueprint version +------------------------ + +You can also pass a version number to the blueprint, which will apply to all routes. + +.. code-block:: python + + from sanic import response + from sanic.blueprints import Blueprint + + bp = Blueprint('test', version=1) + + @bp.route('/html') + def handle_request(request): + return response.html('Hello world!
') + +Then with curl: + +.. code-block:: bash + + curl localhost/v1/html