/engineer_thesis_api

Simple CRUD REST API resembling real world application's backend. Created for the purpose of my engineering thesis Comparison of frontend frameworks.

Primary LanguagePython

engineer_thesis_api

           

Simple CRUD REST API resembling real world application's backend. Created for the purpose of my engineering thesis Comparison of frontend frameworks.

Pre-requisites

Tools, libraries, frameworks

  • Django 4.0, Django REST Framework django djangorestframework
  • django-cors-headers
  • django-filter
  • djangorestframework-simplejwt
  • django-extensions
  • drf-spectacular
  • factory_boy
  • coverage
  • psycopg2
  • gunicorn
  • whitenoise

Setup

Create .venv directory in root directory if you want to have your virtual environment in the project directory ( otherwise it will be created inside pipenv's default .virtualenvs directory somewhere on your computer).

Launch virtual environment with pipenv (it will be created on first run):

pipenv shell

Install dependencies:

pipenv install

Environment variables

Define environmental variables in dotenv files in ./env directory.

env/backend.env

SECRET_KEY=
DJANGO_SETTINGS_MODULE=core.settings.dev
DB_NAME=postgres
DB_USER=postgres
DB_PASSWORD=postgres

env/db.env

POSTGRES_DB=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres

Running the application

Make sure Docker Engine is running.

Building containers (required if Dockerfile or installed packages changed):

docker compose build

Running containers

docker compose up

Executing commands in running containers (e.g. using django cli)

docker exec -it backend python manage.py migrate
docker exec -it backend python manage.py createsuperuser

Bringing down containers

docker compose down

Testing

Running tests with coverage

docker exec -it backend coverage run manage.py test

Coverage report after running unit tests

docker exec -it backend coverage report -m

Deployment

Useful links:

Launch

To configure and launch the app, run the fly launch command and follow the wizard. You need to provision a Postgres database before launching the app.

Environment variables

Set environment variables in Fly.io dashboard or via flyctl cli.

SECRET_KEY=...
PRODUCTION_HOST=engineer-thesis-api.fly.dev
CLIENT_APP_REACT=https://engineer-thesis-react.vercel.app
CLIENT_APP_SVELTE=https://engineer-thesis-svelte.vercel.app
fly secrets set VARIABLE=...

GitHub secrets

Obtain Fly.io API key and add it as a secret to your repository to enable continuous deployments.

fly auth token

Manual deployment

fly deploy

Connect to a running instance

fly ssh console