Files
sanic/guide/content/en/guide/running/development.md
2023-09-06 15:44:00 +03:00

3.7 KiB

Development

The first thing that should be mentioned is that the webserver that is integrated into Sanic is not just a development server.

It is production ready out-of-the-box, unless you enable in debug mode.

Debug mode

By setting the debug mode, Sanic will be more verbose in its output and will disable several run-time optimizations.

# server.py
from sanic import Sanic
from sanic.response import json

app = Sanic(__name__)

@app.route("/")
async def hello_world(request):
    return json({"hello": "world"})
sanic server:app --host=0.0.0.0 --port=1234 --debug

.. danger::

Sanic's debug mode will slow down the server's performance, and is **NOT** intended for production environments.

**DO NOT** enable debug mode in production.

Automatic Reloader

.. column::

Sanic offers a way to enable or disable the Automatic Reloader. The easiest way to enable it is using the CLI's `--reload` argument to activate the Automatic Reloader. Every time a Python file is changed, the reloader will restart your application automatically. This is very convenient while developing.

.. note:: 

    The reloader is only available when using Sanic's [worker manager](./manager.md). If you have disabled it using `--single-process` then the reloader will not be available to you.

.. column::

```sh
sanic path.to:app --reload
```
You can also use the shorthand property
```sh
sanic path.to:app -r
```

.. column::

If you have additional directories that you would like to automatically reload on file save (for example, a directory of HTML templates), you can add that using `--reload-dir`.

.. column::

```sh
sanic path.to:app --reload --reload-dir=/path/to/templates
```
Or multiple directories, shown here using the shorthand properties
```sh
sanic path.to:app -r -R /path/to/one -R /path/to/two
```

Best of both worlds

.. column::

If you would like to be in debug mode **and** have the Automatic Reloader running, you can pass `dev=True`. This is equivalent to **debug + auto reload**.

*Added in v22.3*

.. column::

```sh
sanic path.to:app --dev
```
You can also use the shorthand property
```sh
sanic path.to:app -d
```

Automatic TLS certificate

When running in DEBUG mode, you can ask Sanic to handle setting up localhost temporary TLS certificates. This is helpful if you want to access your local development environment with https://.

This functionality is provided by either mkcert or trustme. Both are good choices, but there are some differences. trustme is a Python library and can be installed into your environment with pip. This makes for easy envrionment handling, but it is not compatible when running a HTTP/3 server. mkcert might be a more involved installation process, but can install a local CA and make it easier to use.

.. column::

You can choose which platform to use by setting `config.LOCAL_CERT_CREATOR`. When set to `"auto"`, it will select either option, preferring `mkcert` if possible.

.. column::

```python
app.config.LOCAL_CERT_CREATOR = "auto"
app.config.LOCAL_CERT_CREATOR = "mkcert"
app.config.LOCAL_CERT_CREATOR = "trustme"
```

.. column::

Automatic TLS can be enabled at Sanic server run time:

.. column::

```sh
sanic path.to.server:app --auto-tls --debug
```

.. warning::

Localhost TLS certificates (like those generated by both `mkcert` and `trustme`) are **NOT** suitable for production environments. If you are not familiar with how to obtain a *real* TLS certificate, checkout the [How to...](../how-to/tls.md) section.

Added in v22.6