Skip to content

Commit

Permalink
Improving the backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud-Emad committed Mar 5, 2024
1 parent 4cbd79c commit ff58a1c
Show file tree
Hide file tree
Showing 60 changed files with 362 additions and 238 deletions.
36 changes: 0 additions & 36 deletions Dockerfile

This file was deleted.

72 changes: 56 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,64 @@

CMD:=poetry run
client=cd client
up:
docker-compose up --build -d
client:=cd client
server:=cd server

help:
@echo "\n- Docker: To build and run a specific service, you can do that by executing 'make docker-up service=<service_name>'."
@echo "\n- To run a the backend project, you can do that by executing 'make runserver'."
@echo "\n- To run a the backend client, you can do that by executing 'make runclient'."

docker-up:
ifeq ($(service), frontend)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env up frontend --build -d
else ifeq ($(service), backend)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env up backend --build -d
else ifeq ($(service), postgres)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env up postgres --build -d
else
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env up --build -d
endif

docker-down:
ifeq ($(service), frontend)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env down frontend
else ifeq ($(service), backend)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env down backend
else ifeq ($(service), postgres)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env down postgres
else
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env down
endif

docker-logs:
ifeq ($(service), frontend)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env logs -f frontend
else ifeq ($(service), backend)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env logs -f backend
else ifeq ($(service), postgres)
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env logs -f postgres
else
docker compose -f ./docker/docker-compose.yml --env-file=./config/.env logs -f
endif

install:
curl -sSL https://install.python-poetry.org | python3 -
poetry install
poetry check
$(server) && poetry install
$(server) && poetry check
$(client) && pnpm i
runserver:
$(CMD) python3 manage.py runserver
$(server) && $(CMD) python3 manage.py runserver
runclient:
$(client) && npm install && npm run dev
$(client) && pnpm i && pnpm dev
test:
$(CMD) python3 manage.py test
$(server) && $(CMD) python3 manage.py test
lint:
$(CMD) black .
$(CMD) flake8 . --exclude=__init__.py
$(server) && $(CMD) black . --exclude=__init__.py
$(server) && $(CMD) flake8 . --exclude=__init__.py
$(client) && pnpm lint
migrate:
$(CMD) python3 manage.py migrate
migrations:
$(CMD) python3 manage.py makemigrations
$(server) && $(CMD) python3 manage.py makemigrations
$(server) && $(CMD) python3 manage.py migrate
user:
$(CMD) python3 manage.py createsuperuser
$(CMD) python3 manage.py sqlmigrate test_tracker 0006
$(server) && $(CMD) python3 manage.py createsuperuser
data:
$(server) && $(CMD) python3 manage.py create locations users
15 changes: 0 additions & 15 deletions docker-compose.yml

This file was deleted.

41 changes: 41 additions & 0 deletions docker/backend.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
FROM ubuntu:22.04

# Set the environment variable
ENV DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC
ENV REDIS_HOST=$REDIS_HOST
ENV DJANGO_DEBUG=$DJANGO_DEBUG
ENV EMAIL=$EMAIL
ENV EMAIL_PASSWORD=$EMAIL_PASSWORD
ENV EMAIL_HOST=$EMAIL_HOST
ENV DJANGO_SUPERUSER_EMAIL=$DJANGO_SUPERUSER_EMAIL
ENV DJANGO_SUPERUSER_PASSWORD=$DJANGO_SUPERUSER_PASSWORD
ENV SERVER_DOMAIN_NAME=$SERVER_DOMAIN_NAME
ENV CLIENT_DOMAIN_NAME=$CLIENT_DOMAIN_NAME
ENV ENV=$ENV
ENV DATABASE_NAME=$DATABASE_NAME
ENV DATABASE_USER=$DATABASE_USER
ENV DATABASE_PASSWORD=$DATABASE_PASSWORD
ENV DATABASE_HOST=$DATABASE_HOST
ENV DATABASE_PORT=$DATABASE_PORT

RUN echo deb http://be.archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse >> /etc/apt/sources.list
RUN apt-get -y update && \
apt-get -y install wget sudo netcat redis vim python3.8 python3-pip && \
rm -rf /var/lib/apt/lists/*

WORKDIR /server

RUN wget -O /sbin/zinit https://github.com/threefoldtech/zinit/releases/download/v0.2.5/zinit && \
chmod +x /sbin/zinit && pip install poetry && poetry --version

RUN mkdir -p /etc/zinit/

COPY ./server /server
COPY ./docker/scripts/backend/zinit /etc/zinit
COPY ./docker/scripts/backend/*.sh /server/scripts/
COPY ./config /config/

RUN chmod +x /server/scripts/*.sh

EXPOSE 8000
ENTRYPOINT ["zinit", "init"]
Empty file added docker/configuration.md
Empty file.
Empty file added docker/docker-compose.yml
Empty file.
27 changes: 27 additions & 0 deletions docker/frontend.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# build stage
FROM node:18.18.2 as build-stage
WORKDIR /client

ENV SERVER_DOMAIN_NAME_API=$SERVER_DOMAIN_NAME_API

# Copy package.json and package-lock.json separately to leverage Docker cache
COPY ./client/package*.json /client
COPY ./docker/scripts/frontend/build-env.sh /client/scripts/

RUN npm i -g pnpm && pnpm install
RUN chmod +x /client/scripts/build-env.sh
COPY ./client /client
RUN pnpm build

FROM nginx:stable-alpine as production-stage
COPY ./nginx/prod.conf /temp/prod.conf

RUN envsubst '$SERVER_DOMAIN_NAME_API' < /temp/prod.conf > /etc/nginx/conf.d/default.conf

# Copy the build artifacts and build-env.sh from the build-stage
COPY --from=build-stage /client/dist /usr/share/nginx/html
COPY --from=build-stage /client/scripts/build-env.sh /usr/share/nginx/html

EXPOSE 80

CMD ["/bin/sh", "-c", "/usr/share/nginx/html/build-env.sh && nginx -g \"daemon off;\""]
9 changes: 9 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Project Configuration

After executing

```sh
make install && make migrate
```

Please refer to [.env.template](.././config/.env.template) for all required values. Ensure all values are populated in the `.env` file within the [config](.././config/) directory.
32 changes: 32 additions & 0 deletions docs/docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Runing the project using Docker and Docker Compose

To run the full project with all instances, follow these steps:

1. Set Environment Variables

Create a `.env` file beside the [config](.././config/) directory and set the necessary environment variables. Refer to the previous section for sample environment variable configurations.

2. Execute Docker Compose

Run the following command to start the Docker containers:

```sh
# --> To run all services

└─(✹)──> docker compose -f ./docker/docker-compose.yml --env-file=./config/.env up --build -d

# --> To stop all services
└─(✹)──> docker compose -f ./docker/docker-compose.yml --env-file=./config/.env down

```

Also, you can excute the command using Make:

- make docker-up **To run all services**
- make docker-down **To stop all services**

After executing the command, you should see a confirmation similar to the following:

3. Verify Successful Deployment

This indicates that the Docker containers are being created and the services are starting up.
26 changes: 26 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Installation Process

To facilitate the installation process, this project utilizes the `poetry` package manager for `Python` and `yarn` for `Node.js`. Follow the steps outlined below for a seamless installation:

1. Install Poetry:
Execute the following command to install Poetry for Python:

```sh
curl -sSL https://install.python-poetry.org | python3 -
```

2. Install yarn:
To install `yarn` globally for Node.js, run the following command:

```sh
npm i -g yarn
```

After installing these packages you can now install the required packages by executing:
3. Install project Packages

```sh
make install
```

By following these steps, you ensure the proper setup of the required package managers for this project.
File renamed without changes.
2 changes: 1 addition & 1 deletion server/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

application = get_asgi_application()
2 changes: 1 addition & 1 deletion manage.py → server/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions server/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta
from server.components import config, BASE_DIR
from components import config, BASE_DIR


SECRET_KEY = config("DJANGO_SECRET_KEY")
Expand All @@ -19,7 +19,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"server.test_tracker",
"test_tracker",
# Third party
"rest_framework",
"corsheaders",
Expand All @@ -39,7 +39,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "server.urls"
ROOT_URLCONF = "urls"

TEMPLATES = [
{
Expand All @@ -57,7 +57,7 @@
},
]

WSGI_APPLICATION = "server.wsgi.application"
WSGI_APPLICATION = "wsgi.application"


# Database
Expand Down Expand Up @@ -98,7 +98,7 @@
"rest_framework.parsers.JSONParser",
"rest_framework.parsers.MultiPartParser",
],
"DEFAULT_PAGINATION_CLASS": "server.test_tracker.api.pagination.CustomPagination",
"DEFAULT_PAGINATION_CLASS": "test_tracker.api.pagination.CustomPagination",
"PAGE_SIZE": 10,
"DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"],
"TEST_REQUEST_DEFAULT_FORMAT": "json",
Expand Down
4 changes: 2 additions & 2 deletions server/test_tracker/api/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from rest_framework.request import Request
from rest_framework.views import APIView

from server.test_tracker.services.dashboard import get_project_by_id
from server.test_tracker.services.member import get_member_by_id
from test_tracker.services.dashboard import get_project_by_id
from test_tracker.services.member import get_member_by_id


class UserIsAuthenticated(permissions.BasePermission):
Expand Down
2 changes: 1 addition & 1 deletion server/test_tracker/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class TestTrackerConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "server.test_tracker"
name = "test_tracker"
4 changes: 2 additions & 2 deletions server/test_tracker/models/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models

from server.test_tracker.models.abstracts import TimeStampedModel
from server.test_tracker.models.users import User
from test_tracker.models.abstracts import TimeStampedModel
from test_tracker.models.users import User

import uuid

Expand Down
6 changes: 3 additions & 3 deletions server/test_tracker/models/project.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""This file contains all sub tables based on the project table on 'models/dashboard' """
from django.db import models
from server.test_tracker.models.abstracts import TimeStampedModel
from server.test_tracker.models.dashboard import Member, Project
from test_tracker.models.abstracts import TimeStampedModel
from test_tracker.models.dashboard import Member, Project

from server.test_tracker.models.users import User
from test_tracker.models.users import User


class PLAN_CHOICES(models.TextChoices):
Expand Down
2 changes: 1 addition & 1 deletion server/test_tracker/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
AnonymousUser,
)

from server.test_tracker.models.abstracts import (
from test_tracker.models.abstracts import (
TimeStampedModel,
)

Expand Down
2 changes: 1 addition & 1 deletion server/test_tracker/routs/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path

from server.test_tracker.views.auth import (
from test_tracker.views.auth import (
DecodeAndVerifySignatureAPIView,
GetUserAPIView,
LoginByTokenAPIView,
Expand Down
Loading

0 comments on commit ff58a1c

Please sign in to comment.