Skip to content

Commit

Permalink
Feito funcionamento de estáticos via publicos via S3
Browse files Browse the repository at this point in the history
close #7
  • Loading branch information
renzo authored and renzon committed Aug 15, 2024
1 parent 58b8555 commit 1050b14
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 5 deletions.
23 changes: 21 additions & 2 deletions backend/devpro/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,27 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_ROOT = BASE_DIR.parent/'docker/staticfiles/static'
STATIC_URL = 'static/'

AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME', default='').strip()

if AWS_STORAGE_BUCKET_NAME == '':
# Configuração para coletar estáticos para o Nginx
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR.parent / 'docker/staticfiles/static'
else:
STATIC_URL = f'//{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/'

AWS_S3_ACCESS_KEY_ID = config('AWS_S3_ACCESS_KEY_ID')
AWS_S3_SECRET_ACCESS_KEY = config('AWS_S3_SECRET_ACCESS_KEY')
STORAGES = {
"staticfiles": {
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
'default_acl': 'public-read',
'location': 'static',
},
},
}

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
Expand Down
134 changes: 133 additions & 1 deletion backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ psycopg2 = "^2.9.9"
dj-database-url = "^2.2.0"
python-decouple = "^3.8"
django-min-custom-user = "^0.2.0"
django-storages = {extras = ["s3"], version = "^1.14.4"}


[tool.poetry.group.dev]
Expand Down
8 changes: 7 additions & 1 deletion config/env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ BACKEND_SECRET_KEY=escolha-um-segredo-como-valor-para-o-django
BACKEND_DEBUG=False
# Configure os dominios ou ips que sua aplicação vai rodar, separadas por vírgula
BACKEND_ALLOWED_HOSTS=localhost,127.0.0.1
DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres
DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres

# S3 configuration
## Se a variável não for definida, será usado Nginx para servir estatícos. Caso contrário, o S3.
AWS_STORAGE_BUCKET_NAME=
AWS_S3_ACCESS_KEY_ID=your-iam-s3-access-key-id
AWS_S3_SECRET_ACCESS_KEY=your-iam-s3-access-key-secret
6 changes: 5 additions & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ Para rodar, navegue até o diretório docker e rode o comando:

```bash
docker compose up -d
```
```

Por padrão ao rodar em ambiente local também está sendo usado o nginx rodando na porta 80.
Ele funciona como proxy reverso para a aplicação Django rodando na porta 8000.
E também serve os estático gerados pelo Django se não tiver um bucket definido.

0 comments on commit 1050b14

Please sign in to comment.