The backend is split into three main parts. It consists of FastAPI, SqlAlchemy and Postgres. FastAPI is a framework for creating APIs in Python. SqlAlchemy is an ORM (Object Relational Mapper) that makes it easy to work with databases in Python. Postgres is the database used in production. In development, sqlite is used.
.env
is not commited to this repo because of security, so this file has to be created.
Both frontend and backend folders includes a .env.template
file. This file contains the variables that has to be set in the new .env
file.
In the backend
folder, create.env
file containing the following:
production = false
(This is used to determine if the database should be sqlite or postgres)SECRET_KEY = random_string
(This is used for hashing)
Feide instance (this can be created here):
client_id
is setclient_secret
is setREDIRECT_URI
is setBASE_URL
is set
OpenAI authentication (this can be created here):
OPENAI_KEY
is set
Note: There are some optional variables in the .env.template
file. These are not necessary for the application to run, but can be used for testing and development.
Note: For linting the backend, the VScode extention Black Formatter
is used. This is also visible in the .gitlab-ci.yml
-file where we have a job for checking the linting of the backend.
This is the recommended way to run the backend. This will start the backend in a Docker container. To do this, run the following command:
make dev
If you are using Windows, you can run the following commands (every command in the Makefile) manually in the terminal:
docker-compose up --no-deps backend
cd backend
# Creates virtual environment
python -m venv venv # only do once
# Activates virtual environment
source ./venv/bin/activate #Troubleshooting for windows below
# Installing python packages
pip install -r requirements.txt
# Apply migration scripts
alembic upgrade head
# Run the backend
uvicorn main:app --reload
# Running at 127.0.0.1:8000
Migration: alembic is used for databse migration (kind of git for databases). You can read more about alembic here, or take a look at this tutorial video.
When ANY changes is made to the database model file, then a migration has to happen. This is done by the following commands:
# Create migration
alembic revision --autogenerate -m "Revision message, something relevant here"
# Apply latest migration
alembic upgrade head
To run the tests, run the following command:
make test-backend
Or if you are using Windows, you can run the following described in the Makefile here
The command
source ./venv/bin/activate
Is intended for Linux and does not always work on windows.
Therefore, windows users using VS code, could install the extension Powershell
(Picture below), to run the activate script in the venv
-folder. Then manually run the script by clicking the script and clicking run
.
- First install the
Powershell
extension
- Then go into
backend/venv/Scripts/Activate.ps1
and run the script.
- Click this file and run it. If you see a green
(venv)
in the terminal, then the virtual environment is activates. You can now run the rest of the commands above.
- You can now run the rest of the commands to setup and run backend
# Installing python packages
pip install -r requirements.txt
# Apply migration scripts
alembic upgrade head
# Run the backend
uvicorn main:app --reload
# Running at 127.0.0.1:8000