Skip to content

Commit

Permalink
Merge pull request #24 from unb-mds/feat/docker
Browse files Browse the repository at this point in the history
docker(api): configurar o docker para ambiente dev da API
  • Loading branch information
mateusvrs authored Sep 27, 2023
2 parents 0a39fd3 + 361ec8d commit 37e3972
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 16 deletions.
25 changes: 22 additions & 3 deletions api/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
# Django Configuration
# Generated with:
# Configuração Django
# Gerar secret key:
# python3 -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
DJANGO_SECRET_KEY="your_secret_key"
DJANGO_SECRET_KEY="your_secret_key"
SETTINGS_FILE_PATH="core.settings.base"

# Banco de Dados
DB_ENGINE="django.db.backends.postgresql"
DB_NAME="postgres"
DB_USERNAME="suagradeunb"
DB_PASSWORD="suagradeunb"
DB_HOSTNAME=db
DB_PORT=5432

# PostgreSQL
POSTGRES_DB="postgres"
POSTGRES_USER="suagradeunb"
POSTGRES_PASSWORD="suagradeunb"


# Credenciais de acesso ao admin
ADMIN_NAME="admin"
ADMIN_PASS="admin"
3 changes: 3 additions & 0 deletions api/api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions api/api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'
Empty file added api/api/management/__init__.py
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions api/api/management/commands/initadmin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from decouple import config


class Command(BaseCommand):

def handle(self, *args, **options):
username = config("ADMIN_NAME")
if not len(User.objects.all().filter(username=username)):
password = config("ADMIN_PASS")
print(f'Conta do usuário {username} será criada!')
admin = User.objects.create_superuser(
username=username, password=password)
admin.is_active = True
admin.is_staff = True
admin.save()
else:
print('Conta de administrador já existe!')
Empty file added api/api/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions api/api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions api/api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions api/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Empty file added api/core/__init__.py
Empty file.
3 changes: 2 additions & 1 deletion api/api/asgi.py → api/core/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
"""

import os
from decouple import config

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', config('SETTINGS_FILE_PATH'))

application = get_asgi_application()
28 changes: 19 additions & 9 deletions api/api/settings.py → api/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pathlib import Path
from decouple import config
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -26,18 +27,20 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["*"]


# Application definition

INSTALLED_APPS = [
'api.apps.ApiConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]

MIDDLEWARE = [
Expand All @@ -50,7 +53,7 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'api.urls'
ROOT_URLCONF = 'core.urls'

TEMPLATES = [
{
Expand All @@ -68,20 +71,23 @@
},
]

WSGI_APPLICATION = 'api.wsgi.application'
WSGI_APPLICATION = 'core.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config("DB_NAME"),
"USER": config("DB_USERNAME"),
"PASSWORD": config("DB_PASSWORD"),
"HOST": config("DB_HOSTNAME"),
"PORT": config("DB_PORT", cast=int),
}
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

Expand All @@ -106,7 +112,7 @@

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
TIME_ZONE = 'America/Sao_Paulo'

USE_I18N = True

Expand All @@ -116,7 +122,11 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = 'static/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "../", "staticfiles")

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "../", "mediafiles")

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
Expand Down
11 changes: 11 additions & 0 deletions api/core/settings/dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .base import *

# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
6 changes: 6 additions & 0 deletions api/core/settings/prod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ["0.0.0.0:8000", "localhost:8000", "127.0.0.1:8000"]
File renamed without changes.
3 changes: 2 additions & 1 deletion api/api/wsgi.py → api/core/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
"""

import os
from decouple import config

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', config('SETTINGS_FILE_PATH'))

application = get_wsgi_application()
3 changes: 2 additions & 1 deletion api/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"""Django's command-line utility for administrative tasks."""
import os
import sys
from decouple import config


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', config('SETTINGS_FILE_PATH'))
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
db:
image: postgres:16
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./api/.env

api:
restart: unless-stopped
build:
context: .
dockerfile: ./docker/Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code/
env_file:
- ./api/.env
ports:
- 8000:8000
depends_on:
- db

volumes:
postgres_data:
28 changes: 28 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM python:3.11.5-alpine3.18

# definir o diretório de trabalho
WORKDIR /usr/src/app

# definir variáveis de ambiente
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# instalar dependências do poastgres
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev

# copiar os requisitos python
COPY ./requirements.txt /usr/src/app/requirements.txt

# instalação das dependências
RUN pip install --upgrade pip && \
pip install -r /usr/src/app/requirements.txt

# copiar entrypoint.sh e dar permissão de execução
COPY ./docker/entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh

# copiar o projeto
COPY ./api /usr/src/app/

# executar o arquivo de inicialização
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
20 changes: 20 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

echo 'Esperando o PostgreSQL iniciar...'

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

echo 'PostgreSQL iniciado'

echo 'Migrando banco de dados...'
python3 manage.py migrate

echo 'Criando usuário admin...'
python3 manage.py initadmin

echo 'Coletando arquivos estáticos...'
python3 manage.py collectstatic --no-input

exec "$@"
6 changes: 5 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ certifi==2023.7.22
charset-normalizer==3.2.0
click==8.1.7
colorama==0.4.6
decouple==0.0.7
Django==4.2.5
djangorestframework==3.14.0
ghp-import==2.1.0
idna==3.4
Jinja2==3.1.2
Expand All @@ -19,14 +19,18 @@ packaging==23.1
paginate==0.5.6
pathspec==0.11.2
platformdirs==3.10.0
psycopg2-binary==2.9.7
Pygments==2.16.1
pymdown-extensions==10.3
python-dateutil==2.8.2
python-decouple==3.8
pytz==2023.3.post1
PyYAML==6.0.1
pyyaml_env_tag==0.1
regex==2023.8.8
requests==2.31.0
six==1.16.0
sqlparse==0.4.4
typing_extensions==4.8.0
urllib3==2.0.5
watchdog==3.0.0

0 comments on commit 37e3972

Please sign in to comment.