Skip to content

Commit

Permalink
feat: s3 default base storage
Browse files Browse the repository at this point in the history
  • Loading branch information
fivehanz committed Apr 3, 2024
1 parent 76a0797 commit 1944e01
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
MAKEFLAGS += -j3

MAKEFLAGS += -j4
GIT_TAG = ${shell git tag | tail -1}

build: build-tailwindcss build-statics
deps: bun-install python-install
build-docker: build-docker-image
dev: dev-tailwindcss dev-django dev-redis-start
dev: dev-tailwindcss dev-django dev-redis-start dev-minio-start
migrate: migrate-django
prod: prod-release

Expand Down Expand Up @@ -42,6 +41,13 @@ prod-nginx-link:
dev-redis-start:
docker run --rm -p 6379:6379 --name hanz_dev_redis redis:7-bookworm

dev-minio-start:
docker run --rm -p 9000:9000 -p 9001:9001 \
--env MINIO_ROOT_USER=root --env MINIO_ROOT_PASSWORD=password \
--volume ./dev_data/minio/data:/data \
--name hanz_dev_minio \
quay.io/minio/minio server /data --console-address ":9001"

build-docker-image:
docker build --tag hanz-web:${GIT_TAG} -f ./dev.Dockerfile .

Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ crispy-tailwind = "*"
wagtail-cache = "*"
redis = {extras = ["hiredis"], version = "*"}
django-dbbackup = "*"
django-storages = {extras = ["boto3"], version = "*"}
django-storages = {extras = ["boto3"], version = "==1.14"}
django-anymail = {extras = ["resend"], version = "*"}

[dev-packages]
Expand Down
8 changes: 4 additions & 4 deletions Pipfile.lock

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

36 changes: 25 additions & 11 deletions hanz/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import dj_database_url
from django.conf.global_settings import STORAGES

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
Expand Down Expand Up @@ -57,6 +58,7 @@
"crispy_forms",
"crispy_tailwind",
"dbbackup", # django-dbbackup
"storages", # django-storages
]

MIDDLEWARE = [
Expand Down Expand Up @@ -95,6 +97,8 @@

WSGI_APPLICATION = "hanz.wsgi.application"

SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
Expand Down Expand Up @@ -161,13 +165,9 @@
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True


Expand All @@ -193,19 +193,34 @@

# # in the case of CDN url
STATIC_HOST = os.environ.get("DJANGO_STATIC_HOST", "")

STATIC_URL = STATIC_HOST + "/static/"

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

SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
# Storage Backends
STORAGES["default"] = {
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
"access_key": os.environ.get("MEDIA_S3_ACCESS_KEY_ID"),
"secret_key": os.environ.get("MEDIA_S3_SECRET_KEY"),
"bucket_name": os.environ.get("MEDIA_S3_BUCKET_NAME"),
"region_name": os.environ.get("MEDIA_S3_REGION_NAME", "us-east-1"),
"endpoint_url": os.environ.get("MEDIA_S3_ENDPOINT_URL"),
"use_ssl": os.environ.get("MEDIA_S3_USE_SSL", True) == "True",
"addressing_style": "path",
"default_acl": "public-read",
},
}

COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage" # brotli compression for static files

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

# Static Files
# Boolean that decides if compression will happen.
COMPRESS_ENABLED = os.environ.get("COMPRESS_ENABLED", not DEBUG) == "True"
COMPRESS_STORAGE = (
"compressor.storage.GzipCompressorFileStorage" # gzip compression for static files
)

# Boolean that decides if compression should be done outside of the request/response loop.
# Must enable this to use with Whitenoise
Expand All @@ -215,7 +230,6 @@
COMPRESS_ROOT = os.path.join(BASE_DIR, "static")

# Wagtail settings

WAGTAIL_SITE_NAME = "hanz"

# Search
Expand Down

0 comments on commit 1944e01

Please sign in to comment.