Below you will find the prerequisites for running the project locally.
Begin with creating an environment variable file called .env
in the root of the project. This file will contain the credentials for e.g. MinIO object storage. The file should look like this:
cp .env-example .env
You will need to have conda installed on the system. Miniconda should suffice install miniconda
git clone git@github.com:upcycling-kandidat/kandidat-backend.git
cd kandidat-backend
conda env create --file environment.yml
This will look for the environment.yml file and create a new environment called chair-training. To activate this new environment, run the following command:
conda activate kandidat-backend
Further information about conda environments can be found here
You will need to have docker installed on the system. install docker Run the following command to start the MinIO object storage container
docker compose up -d
flask run --debug
The debug flask flag will reload the server when changes are made to the code.
When accessing environment variables alway use python-dotenv
package. This will make sure that the environment variables are loaded from the .env
file. Make sure to follow Create a .env file before running the code.
Example showing how to access the environment variable MINIO_ACCESS_KEY
:
from dotenv import dotenv_values
config = dotenv_values(".env")
config["MINIO_ACCESS_KEY"]
In storage.py
you will find the upload_file
function which takes in one argument input_file
which is a path of the desired file to upload.
To add a new endpoint, create a new function in app.py
and add the endpoint to the app
object. Example showing how to add a new endpoint which accepts HTTP POST
methods with the path /test
which returns a json object:
from flask import (
jsonify,
request,
)
@app.route("/test", methods=["POST"])
# The function name can be anything
def test():
if request.method == "POST":
return jsonify({"message": "test"})
return
The project uses Flask
as the web framework to serve the REST API and MinIO
as the object storage. The project is written in Python 3.8.16
.