Table of contents
The REST API connects the databases and the web app.
Please follow the instruction of the deployment repository.
- Start local database and mail server
- Install Ubuntu / Debian packages
- Install FastAPI in a separate virtual environment
- Configure environment variables
- Start the database container
- Start the FastAPI Server
cd $EVIDENCE_DEPLOY
docker-compose up --build dbauth dbeval mail
sudo apt update
sudo apt install -y --no-install-recommends build-essential python3-dev python3-venv
sudo apt install -y --no-install-recommends libpq-dev
python3 -m venv .venv
source .venv/bin/activate
pip3 install --upgrade pip
pip3 install -r requirements-dev.txt
pip3 install -r requirements.txt
In particular, the SMTP settings must be adapted.
cp dev.env .env
source .venv/bin/activate
uvicorn app.main:app \
--host localhost --port 7070 \
--reload --log-level debug
Open http://localhost:7070/v1/docs in your browser.
In order to carry out the unit tests, a test account is created directly in the Postgres database.
The test user has the email nobody@example.com
and the password is supersecret
.
Never do this on a production server!
export EVIDENCE_DEPLOY=../
(cd $EVIDENCE_DEPLOY && cat restapi/test/addtestaccount.sql | docker exec -i evidence_dbauth psql -U evidence -d evidence)
cd restapi
source .venv/bin/activate
pytest
Authenticate yourself with the test account. Request an access token.
EMAIL=nobody@example.com
PASSWORD=supersecret
curl -X POST "http://localhost:7070/v1/auth/login" \
-H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" \
-d "username=${EMAIL}&password=${PASSWORD}" \
> mytokendata
cat mytokendata
TOKEN=$(cat mytokendata | python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])")
echo $TOKEN
curl -X POST "http://localhost:7070/v1/user/settings" \
-H "accept: application/json" -H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" -d '{"hello":"world3"}'
curl -X POST "http://localhost:7070/v1/user/settings" \
-H "accept: application/json" -H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" -d '{"test": 123}'
curl -X GET "http://localhost:7070/v1/user/settings" \
-H "accept: application/json" -H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}"
curl -X GET "http://localhost:7070/v1/bestworst/random/4" \
-H "accept: application/json" \
-H "Authorization: Bearer ${TOKEN}"
curl -X POST "http://localhost:7070/v1/bestworst/samples/4/3/100/0" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{"headword": "blau"}'
curl -X POST "http://localhost:7070/v1/interactivity/training-examples/5/10/0" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{"headword": "blau"}'
curl -X POST "http://localhost:7070/v1/serialized-features" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{"headword": "blau", "limit": 5}'
# model weights
curl -X POST "http://localhost:7070/v1/model/save" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{"weights": [0.2, -0.3, 1.3, -0.4]}'
curl -X POST "http://localhost:7070/v1/model/load" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}"
curl -X POST "http://localhost:7070/v1/model/load-all" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}"
import requests
data = {"username": "nobody@example.com", "password": "supersecret"}
resp = requests.post("http://localhost:7070/v1/auth/login", data)
print(resp.text)
TOKEN = resp.json()['access_token']
headers = {'Authorization': f"Bearer {TOKEN}"}
resp = requests.get("http://localhost:7070/v1/bestworst/random/5", headers=headers)
print(resp.json())
curl -X POST "http://localhost:7070/v1/variation/similarity-matrices" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{"headword": "Internet", "limit": 50}'
The authentication mechanism is implemented in
- the databases repository: `dbauth/
- the REST API repository:
app/routers/auth_email.py
It is very important to setup the SMTP credentials to send verification emails.
- In the UI, the user enters his email and desired password and sends it to the API
- API: Email / PW is forwarded to DB
- DB creates an inactive user account and returns verification token and API.
- API sends email with verification link to the specified email
- User clicks verification link
- API reads verification tokens and sends them to the DB
- DB checks verification token and sets user account to active.
Please replace you@example.com
with a valid email.
EMAIL=you@example.com
PASSWORD=secret2
curl -X POST "http://localhost:7070/v1/auth/register" \
-H "accept: application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=${EMAIL}&password=${PASSWORD}"
Please use the link in your email inbox.
VERIFYTOKEN=273950a0-a11a-461b-83b3-12ddd1b1d9b5
curl -X GET "http://localhost:7070/v1/auth/verify/${VERIFYTOKEN}"
curl -X POST "http://localhost:7070/v1/auth/login" \
-H "accept: application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=${EMAIL}&password=${PASSWORD}" > mytokendata
TOKEN=$(cat mytokendata | python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])")
echo $TOKEN
- Show the docs: http://localhost:7070/v1/docs
- Show Redoc: http://localhost:7070/v1/redoc
- Check pip8 syntax:
flake8 --ignore=F401 --exclude=$(grep -v '^#' .gitignore | xargs | sed -e 's/ /,/g')
- Run unit tests:
pytest
Clean Up code
find . -type f -name "*.pyc" | xargs rm
find . -type d -name "__pycache__" | xargs rm -r
rm -r .pytest_cache
rm -r .venv
Please open an issue for support.
Please contribute using Github Flow. Create a branch, add commits, and open a pull request. You are asked to sign the CLA on your first pull request.