langbrain/Python FastAPI.md

48 lines
1.3 KiB
Markdown

## Set up virtual environnement
At the root of your repository, first set up a python virtual environnement.
`$ python -m venv .venv`
This will be useful to keep track of the dependencies
[source](https://docs.python.org/3/library/venv.html)
Activate it
`$ source .venv/bin/activate`
now you should see `(.venv) ~/Code/fastapi-corbia-telegram$ `
`pip install --upgrade pip`
`(.venv) ~/Code/fastapi-corbia-telegram$ pip install fastapi[all] uvicorn`
To save the dependency list, you can run the pip freeze command and send the output to a requirement.txt
`(.venv) ~/Code/fastapi-corbia-telegram$ pip freeze > requirements.txt`
Later on, when you deploy your application on a server, you will be able to run
`(.venv) pip install -r requirements.txt`
## Hello world
```python
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
```
## Using Environnement Variables
For API authentication and database connection, basically anything that requires a "secret". We're gonna use environnement variables.
For development, we can write everything in a .env file that will be added to the .gitignore. In production, the content of the .env file will be placed in a "secret" file, usually in /etc/app_name/secret, that will be loaded as an Environnement file by the systemd service file.