FastAPI + HTTPS (Windows)
FastAPI over HTTPS for development on Windows
https://dev.to/rajshirolkar/fastapi-over-https-for-development-on-windows-2p7d
FastAPI over HTTPS for development on Windows
using mkcert + setup our Certificate Authority(CA) on localhost.
Note: since this is a self-signed certificate you might get a warning before accessing your API depending on which browser you're using.
We'll get started with the code right away so if your boss has told you "Cool get this running over https by EOD" you are in the right place.
- Open cmd and make a directory for our app.
> mkdir fastapi-https
> cd fastapi-https
- Create and activate a virtual environment for your project and install fastapi and uvicorn in our virtual environment.
> python -m venv ./venv
> .\venv\Scripts\activate
(venv) -> pip install fastapi uvicorn
It's always a good practice to create virtual environments
- Open the fastapi-https folder in VSCode and create a directory app which will contain our FastAPI application in app/main.py. Also create a file server.py to run our Uvicorn server and use it to serve our FastAPI app.
- Paste the following code in app/main.py which will create a FastAPI route for us.
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def read_main():
return { "message" : "Hello World of FastAPI HTTPS"}
- Since FastAPI doesn't come with a built-in web server like Flask and Django, we will be using Uvicorn which is an ASGI server. In the file server.py paste the following code –
import uvicorn
if __name__ == '__main__':
uvicorn.run("app.main:app",
host="0.0.0.0",
port=8432,
reload=True,
)
In this code in the main function we essentially tell the uvicorn server "Dude! Go to app.main and run whatever this 'app' is" and then we mention the host and port and yeah we do want to reload and all those things.
- Now its run the server.py file with python server.py and go to this link http://localhost:8432/
- It works! But over "http" ? this won't do in production. We need HTTPS in production. If you want to learn more about how HTTPS works, I will be writing another article here.
To get HTTPS we need to install mkcert. Mkcert is a free way to get a self signed certificate for your app so it can run over HTTPS.
Install mkcert using Chocolatey
> choco install mkcert
- You need to generate the certificate and add to your CA with the mkcert utility
> mkcert -install
> mkcert localhost 127.0.0.1 ::1
- The certificate is at "localhost+2.pem" and the key at "localhost+2-key.pem" in our project folder. I like to rename the files as "cert.pem" and "key.pem" so its a bit easier on the eyes.
- Now we just need to tell Uvicorn the location of these files and Uvicorn will do all the HTTPS heavy-lifting for us. In server.py add the ssl arguemnts
import uvicorn
if __name__ == '__main__':
uvicorn.run("app.main:app",
host="0.0.0.0",
port=8432,
reload=True,
ssl_keyfile="./key.pem",
ssl_certfile="./cert.pem"
)
- Voila! Go to https://localhost:8432/docs and we now have the sweet green text
'FastAPI' 카테고리의 다른 글
gunicorn 설정 (0) | 2022.01.12 |
---|---|
FastAPI 서버 API 버전 관리 (0) | 2022.01.10 |
Fast API, HTTPS (Fast API + Traefik + Webserver) (0) | 2021.12.30 |
채팅 서버를 위한 FastAPI와 WebSocket (0) | 2021.12.28 |
파이썬, FastAPI Oracle 연동 예제 2 (Pooling 모드) (0) | 2021.12.14 |