Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docker-compose to project #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.db.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_USER=ewallet
POSTGRES_PASSWORD=ewallet
POSTGRES_DB=ewallet
TZ=Asia/Tehran
POSTGRES_MAP_PORT=5432
12 changes: 12 additions & 0 deletions .env.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DEBUG=1
SECRET_KEY=@!vs#(qlryt1ypcfh79_9ym&wo8r=#1)6$ygm%cfvj0b!6@bsf429xacw450*3x!c#wyeje^71aysgq!h=z6_f_0!00)p*1i0qb+
DJANGO_ALLOWED_HOSTS=*
DB_ENGINE=django.db.backends.postgresql
DB_NAME=ewallet
DB_USER=ewallet
DB_PASSWORD=ewallet
DB_HOST=db
DB_PORT=5432
TZ=Asia/Tehran
DOCKER_MODE=1
DJANGO_MAP_PORT=8000
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ migrations/
*.sqlite
*.sqlite3
.idea/
.env.db
27 changes: 27 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use an official Python runtime as a parent image
FROM python:3.9

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set the working directory in the container
WORKDIR /app

# Copy the requirement
COPY ./requirements.txt ./requirements.base.txt ./requirements.postgres.txt /app/

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy source code
COPY ewallet/* /app
RUN ls /app
# Make port 8000 available to the world outside this container
EXPOSE 8000

# Add permission to entrypoint
RUN chmod +x /app/entrypoint.sh

# Define the command to run your application
ENTRYPOINT ["/app/entrypoint.sh"]
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dev:
docker compose -f ./docker-compose.dev.yml --env-file .env --env-file .env.db up -d

dev-build:
docker compose -f ./docker-compose.dev.yml --env-file .env --env-file .env.db up -d --build

dev-build-no-cache:
docker compose -f ./docker-compose.dev.yml --env-file .env --env-file .env.db build --no-cache

dev-down:
docker compose -f ./docker-compose.dev.yml --env-file .env --env-file .env.db down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
. venv/bin/activate
```

4. Установка зависимостей из файла ``requirements.txt``.
4. Установка зависимостей из файла ``requirements.base.txt``.

```bash
pip install -r requirements.txt
Expand Down Expand Up @@ -309,3 +309,12 @@ http://127.0.0.1:8000/api/money/transactions/red
]
```

## Run with docker-compose
```bash
make dev-up
```

## Shoutdown with docker-compose
```bash
make dev-down
```
40 changes: 40 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '3.3'

services:
web:
build:
context: .
dockerfile: Dockerfile.dev
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- "./ewallet/:/app:rw"
ports:
- ${DJANGO_MAP_PORT}:8000
env_file:
- ./.env
depends_on:
- db
networks:
- ewaller-network
restart: unless-stopped

db:
image: postgres:13.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- ${POSTGRES_MAP_PORT}:5432
env_file:
- ./.env.db
networks:
- ewaller-network
restart: unless-stopped


volumes:
postgres_data:
redis_data:

networks:
ewaller-network:
external: false
15 changes: 15 additions & 0 deletions ewallet/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."

while ! nc -z $DB_HOST $DB_PORT; do
sleep 0.1
done

echo "PostgreSQL started"
fi
python3 manage.py makemigrations money
python3 manage.py migrate
exec "$@"
42 changes: 27 additions & 15 deletions ewallet/ewallet/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@

from dotenv import load_dotenv


load_dotenv()
if not os.environ.get("DOCKER_MODE", False):
load_dotenv()

BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_DIR = BASE_DIR.parent

SECRET_KEY = os.getenv('SECRET_KEY')
SECRET_KEY = os.environ.get('SECRET_KEY', "")

DEBUG = False
DEBUG = True if os.environ.get('DEBUG') in (1, "1") else False

ALLOWED_HOSTS = [
'127.0.0.1',
'localhost',
]
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", "*").split(" ")

INSTALLED_APPS = [
'django.contrib.contenttypes',
Expand All @@ -37,16 +34,31 @@


WSGI_APPLICATION = 'ewallet.wsgi.application'

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
if os.environ.get('DB_ENGINE', False):
DATABASES = {
"default": {
"ENGINE": os.environ.get("DB_ENGINE", "django.db.backends.postgresql"),
"NAME": os.environ.get("DB_NAME", "ewallet"),
"USER": os.environ.get("DB_USER", "ewallet"),
"PASSWORD": os.environ.get("DB_PASSWORD", "ewallet"),
"HOST": os.environ.get("DB_HOST", ""),
"PORT": os.environ.get("DB_PORT", ""),
"TEST": {
"NAME": "ewallet_test",
"MIGRATE": False,
},
},
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
}

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
TIME_ZONE = os.environ.get('TZ', 'UTC')
USE_I18N = True
USE_L10N = True
USE_TZ = True
Expand Down
2 changes: 1 addition & 1 deletion ewallet/money/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Meta:
class WalletCreateUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = Wallet
fields = ['name']
fields = ['name', "slug", "balance"]


class TransactionGetSerializer(serializers.ModelSerializer):
Expand Down
1 change: 1 addition & 0 deletions ewallet/money/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
path('wallets', WalletView.as_view(), name='create-wallet'),
path('wallets/<str:slug>', WalletView.as_view(), name='update-wallet'),
path('wallets/<str:slug>', WalletView.as_view(), name='delete-wallet'),
path('wallets/get-by-slug/<str:slug>', WalletView.get_by_slug, name='get-by-slug-wallet'),
path('transactions', TransactionView.as_view(), name='get-transactions'),
path('transactions/<int:id>', TransactionView.as_view(),
name='delete-transactions'),
Expand Down
13 changes: 10 additions & 3 deletions ewallet/money/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def post(self, request):
wallet = Wallet()
serializer = WalletCreateUpdateSerializer(wallet, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_201_CREATED)
saved_instance = serializer.save()
return Response(WalletCreateUpdateSerializer(saved_instance).data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def put(self, request, slug: str):
Expand All @@ -56,6 +56,13 @@ def delete(self, request, slug: str):
if operation:
return Response(status=status.HTTP_200_OK)
return Response(status=status.HTTP_400_BAD_REQUEST)

@staticmethod
@api_view(['GET', ])
def get_by_slug(request, slug:str):
wallet = get_object_or_404(Wallet, slug=slug)
serializer = WalletGetSerializer(wallet)
return Response(serializer.data)


class TransactionView(APIView):
Expand Down Expand Up @@ -87,7 +94,7 @@ def get_by_wallet(request, wallet_slug: str):

@transaction_decorators.atomic
def post(self, request):
wallet = get_object_or_404(Wallet, name=request.data['wallet'])
wallet = get_object_or_404(Wallet, slug=request.data['slug'])
transaction = Transaction(wallet=wallet)
serializer = TransactionCreateUpdateSerializer(transaction,
data=request.data)
Expand Down
9 changes: 0 additions & 9 deletions ewallet/money/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ class Wallet(models.Model):
slug = models.SlugField(max_length=150, unique=True, blank=True)
balance = models.PositiveIntegerField(default=0, editable=False)

def save(self, *args, **kwargs):
if self.name == '':
self.name = 'Wallet {}'.format(uuid.uuid4())

slug = slugify(self.name, allow_unicode=True)
if not self.slug or self.slug != slug:
self.slug = slug
return super().save(*args, **kwargs)


class Transaction(models.Model):
TYPE_INCOME = 'income'
Expand Down
6 changes: 6 additions & 0 deletions requirements.base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
asgiref==3.3.1
Django==3.1.7
djangorestframework==3.12.4
python-dotenv==0.16.0
pytz==2021.1
sqlparse==0.4.1
1 change: 1 addition & 0 deletions requirements.postgres.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
psycopg2-binary==2.9.3
8 changes: 2 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
asgiref==3.3.1
Django==3.1.7
djangorestframework==3.12.4
python-dotenv==0.16.0
pytz==2021.1
sqlparse==0.4.1
-r ./requirements.base.txt
-r ./requirements.postgres.txt