Creating an HTTP to HTTPS proxy: nesting Sanic inside Sanic
Running Sanic from within Sanic seems a bit like those Russian nesting dolls. While it may initially seem like an amazing thought experiment, it does have some real-world applicability. The most obvious example of running two instances of Sanic together like this would be to create your own HTTP to HTTPS proxy. That is what we are going to do now.
The caveat that I want to add to this is that this example will use a self-signed certificate. That means that it is not suitable for production use. You should look at the Securing your application with TLS section in Chapter 8, Running a Sanic Server, for details on how to properly secure your application using TLS.
To begin, we will create two servers. For the sake of simplicity, one will be server.py
(your main application running HTTPS over port 443
) and the other will be redirect.py
(the HTTP to HTTPS proxy running on port 80
).
- We will start by creating our self...