From 1da0217ef1c05de984390ab1d3bcc74f0c6faad7 Mon Sep 17 00:00:00 2001 From: Simon Sandvik Lee Date: Thu, 4 Jan 2024 13:12:07 +0100 Subject: [PATCH 1/2] Feat: Add Docker setup --- backend/Dockerfile | 18 ++++++++++++++++++ backend/docker-compose.yaml | 33 +++++++++++++++++++++++++++++++++ backend/requirements.txt | 3 ++- backend/tutorai/settings.py | 11 ++++++++--- 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 backend/Dockerfile create mode 100644 backend/docker-compose.yaml diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..53d2fe3 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,18 @@ +# Use the official Python image from the Docker Hub +FROM python:3.11.2 + +# Make a new directory to put our code in. +RUN mkdir /code + +# Change the working directory. +WORKDIR /code + +# Copy only the requirements first +COPY requirements.txt /code/ +RUN pip install -r requirements.txt + +# Then copy the rest of the code +COPY . /code/ + +# Run the application: +CMD gunicorn --bind :8000 tutorai.wsgi --workers 1 --timeout 120 \ No newline at end of file diff --git a/backend/docker-compose.yaml b/backend/docker-compose.yaml new file mode 100644 index 0000000..1b04c74 --- /dev/null +++ b/backend/docker-compose.yaml @@ -0,0 +1,33 @@ +version: '3.8' + +services: + tutorai: + build: + context: . + dockerfile: Dockerfile + command: python manage.py runserver 0.0.0.0:8000 + volumes: + - .:/code + ports: + - "8000:8000" + depends_on: + - db + environment: + - DEBUG=True + - DATABASE_HOST=db + - DDATABSE_PORT=5432 + - DATABASE_NAME=tutoraidb + - DATABASE_USER=tutoraiuser + - DATABASE_PASSWORD=tutoraipassword + + db: + image: postgres:13 + volumes: + - postgres_data:/var/lib/postgresql/data + environment: + - POSTGRES_DB=tutoraidb + - POSTGRES_USER=tutoraiuser + - POSTGRES_PASSWORD=tutoraipassword + +volumes: + postgres_data: \ No newline at end of file diff --git a/backend/requirements.txt b/backend/requirements.txt index cfddaf8..2d369f6 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -6,9 +6,10 @@ djangorestframework==3.14.0 drf-yasg==1.21.7 inflection==0.5.1 packaging==23.2 +psycopg2-binary==2.9.9 pytz==2023.3.post1 PyYAML==6.0.1 sqlparse==0.4.4 typing_extensions==4.9.0 tzdata==2023.4 -uritemplate==4.1.1 +uritemplate==4.1.1 \ No newline at end of file diff --git a/backend/tutorai/settings.py b/backend/tutorai/settings.py index a72fe18..9105ee3 100644 --- a/backend/tutorai/settings.py +++ b/backend/tutorai/settings.py @@ -88,9 +88,14 @@ # https://docs.djangoproject.com/en/5.0/ref/settings/#databases DATABASES = { - "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": BASE_DIR / "db.sqlite3", + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'tutoraidb', + 'USER': 'tutoraiuser', + 'PASSWORD': 'tutoraipassword', + 'HOST': 'db', + 'PORT': '5432', + 'OPTIONS': {'sslmode': 'prefer'} } } From 1897385bb1bf64eacd4a52e5530354e9e17789c9 Mon Sep 17 00:00:00 2001 From: Simon Sandvik Lee Date: Thu, 4 Jan 2024 13:32:18 +0100 Subject: [PATCH 2/2] Docs: Add documentation for docker setup --- backend/README.md | 91 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 19 deletions(-) diff --git a/backend/README.md b/backend/README.md index 91778a1..f81ef49 100644 --- a/backend/README.md +++ b/backend/README.md @@ -1,5 +1,7 @@ # Backend + ## Django Basics + How to generate project: ```bash @@ -7,12 +9,62 @@ django-admin startproject tutorai cd tutorai ``` -## How to run the server +## Docker + +For ease of use and version management control, we use Docker to keep track of our containers and virtual environments. + +#### Our Project + +Our project uses docker to run the PostgreSQL server (database) and the Django Server. + +#### Download Docker + +To use docker, download docker engine [here](https://www.docker.com/get-started/) + +#### How to run the Backend + +To run the backend write the following commands in the terminal + +To build the project: + +```bash +docker-compose build +``` + +To run the project: ```bash -python manage.py runserver +docker-compose up ``` +#### Migrate the DB + +To migrate django to the database + +```bash +docker-compose run tutorai python manage.py migrate +``` + +#### Create superuser + +To log into the django database, create a superuser + +```bash +docker-compose run tutorai python manage.py createsuperuser +``` + +And follow the steps it gives you. + +#### Finish! + +Now you can access the django server through + +```bash +http://127.0.0.1:8000/admin/ +``` + +Enjoy! + ## How to create a new application in the backend To create a new application This command creates a new directory named "api" within your "backend" directory, along with the basic files needed for a Django app. @@ -32,30 +84,31 @@ INSTALLED_APPS = [ Develop Your App: Now that your app is set up, you can start building its models, views, templates, URLs, etc - ## Backend Structure The backend of our project is organized into several Django apps, each dedicated to handling a specific set of functionalities within our TutorAI platform. Below is an overview of the directory structure and the role of each component: ### Directory Overview -- `backend/` - - `api/` - This app serves as the gateway for our RESTful API, defining the endpoints that the frontend will consume. It is where serializers and viewsets are defined to expose our models over HTTP. - - `migrations/` - Contains database migrations for the `api` app, allowing changes to be tracked and propagated to the database schema. - - `admin.py` - Configuration for the Django admin interface specific to the `api` app. - - `apps.py` - Configuration file for the `api` application, including any app-specific settings. - - `models.py` - Defines the data models for the `api` app, which Django ORM will use to construct database tables. - - `tests.py` - Contains tests for the `api` app to ensure endpoints work as expected. - - `views.py` - Contains the views for the `api` app that handle requests and return responses. - - - `documents/` - Responsible for managing document-related functionalities, such as PDF uploads and storage. - - Similar structure to `api/` with `migrations/`, `models.py`, `views.py`, etc. +- `backend/` + + - `api/` - This app serves as the gateway for our RESTful API, defining the endpoints that the frontend will consume. It is where serializers and viewsets are defined to expose our models over HTTP. + - `migrations/` - Contains database migrations for the `api` app, allowing changes to be tracked and propagated to the database schema. + - `admin.py` - Configuration for the Django admin interface specific to the `api` app. + - `apps.py` - Configuration file for the `api` application, including any app-specific settings. + - `models.py` - Defines the data models for the `api` app, which Django ORM will use to construct database tables. + - `tests.py` - Contains tests for the `api` app to ensure endpoints work as expected. + - `views.py` - Contains the views for the `api` app that handle requests and return responses. + - `documents/` - Responsible for managing document-related functionalities, such as PDF uploads and storage. + + - Similar structure to `api/` with `migrations/`, `models.py`, `views.py`, etc. + + - `tutorai/` - The core app that includes settings and root configurations for the entire backend project. - - `tutorai/` - The core app that includes settings and root configurations for the entire backend project. - - Similar structure to `api/` but also includes global settings like `settings.py` and root URL configurations in `urls.py`. + - Similar structure to `api/` but also includes global settings like `settings.py` and root URL configurations in `urls.py`. - - `users/` - Manages user authentication, profiles, and permissions. It is crucial for handling user data securely and efficiently. - - Similar structure to `api/` with `migrations/`, `models.py`, `views.py`, etc. + - `users/` - Manages user authentication, profiles, and permissions. It is crucial for handling user data securely and efficiently. + - Similar structure to `api/` with `migrations/`, `models.py`, `views.py`, etc. - - `manage.py` - A command-line utility that lets you interact with this Django project in various ways, such as running the server or creating migrations. + - `manage.py` - A command-line utility that lets you interact with this Django project in various ways, such as running the server or creating migrations.