Skip to content

Commit

Permalink
Merge pull request #22 from SverreNystad/18-dockerize-the-backend
Browse files Browse the repository at this point in the history
Dockerize the backend
  • Loading branch information
SverreNystad committed Jan 4, 2024
2 parents cb898d7 + 1897385 commit cc4251e
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 23 deletions.
18 changes: 18 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
91 changes: 72 additions & 19 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
# Backend

## Django Basics

How to generate project:

```bash
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.
Expand All @@ -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.
33 changes: 33 additions & 0 deletions backend/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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:
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 8 additions & 3 deletions backend/tutorai/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
}
}

Expand Down

0 comments on commit cc4251e

Please sign in to comment.