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

✨Feature: Vite HMR in development mode + Vite backend django integration #564

Merged
merged 17 commits into from
Sep 13, 2024
Merged
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
21 changes: 17 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
./node_modules
dev
stage
docs/

# Dependency directories
**/node_modules/
**/__pycache__/

# Docker
*/Dockerfile
*/*.Dockerfile
.dockerignore

# git
.git
.gitignore
**/__pycache__/
mkdocs/
/.env

# Builds
backend/vite_assets_dist

# vite
frontend/vite.config.*.timestamp-*
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ db.sqlite3
secret_key.txt
dev.env
stage.env
.env
__pycache__
.vscode/
*venv/
Expand All @@ -18,3 +19,5 @@ dev/linter.env
.DS_store
data
.idea

backend/vite_assets_dist
43 changes: 31 additions & 12 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

from pathlib import Path
from decouple import config

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -20,12 +21,12 @@
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-nh^$8a9bz71oax2_j(_x4^&6_s8au=+l(g$0c^d905(+vyf=4&'
SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = config('DEBUG', default=False, cast=bool)

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1"]


# Application definition
Expand All @@ -37,7 +38,8 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'ctj_api.apps.CtjApiConfig',
'rest_framework'
'rest_framework',
"django_vite",
]

MIDDLEWARE = [
Expand All @@ -55,8 +57,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates']
,
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand All @@ -77,12 +78,12 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'yourdatabase',
'USER': 'yourusername',
'PASSWORD': 'yourpassword',
'HOST': 'db',
'PORT': '5432'
'ENGINE': config('SQL_ENGINE'),
'NAME': config('SQL_DATABASE'),
'USER': config('SQL_USER'),
'PASSWORD': config('SQL_PASSWORD'),
'HOST': config('SQL_HOST'),
'PORT': config('SQL_PORT'),
}
}

Expand Down Expand Up @@ -127,3 +128,21 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# django-vite settings
# https://github.com/MrBin99/django-vite
DJANGO_VITE = {
"default": {
# enable vite HMR in dev mode
"dev_mode": config("DEBUG", default=False, cast=bool),
"dev_server_port": 5175,
# resolve static asset paths in production
"manifest_path": Path(BASE_DIR / "vite_assets_dist" / ".vite" / "manifest.json").resolve()
}
}
# Add the build.outDir from vite.config.js to STATICFILES_DIRS
# so that collectstatic can collect your compiled vite assets.
STATICFILES_DIRS = [
BASE_DIR / "vite_assets_dist"
]
6 changes: 6 additions & 0 deletions backend/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"""
# from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView

urlpatterns = [
path(
"",
TemplateView.as_view(template_name="index.html"),
name="index",
),
# path('admin/', admin.site.urls),
path('api/', include('ctj_api.urls')),
]
350 changes: 189 additions & 161 deletions backend/poetry.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ name = "backend"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
readme = "README.md"
readme = "../README.md"

[tool.poetry.dependencies]
python = "^3.11"
django = "^5.0.6"
psycopg2-binary = "^2.9.9"
djangorestframework = "^3.15.2"
django-vite = "^3.0.4"
python-decouple = "^3.8"


[build-system]
Expand Down
26 changes: 26 additions & 0 deletions backend/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% load django_vite %}

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Civic Tech Jobs</title>

<!-- TODO: fix favicon-->
<!-- Loads favicon via template -->
<link rel="icon" href="./src/assets/images/svgs/logos/logo-logomark.svg" />

{% vite_react_refresh %}

{% vite_hmr_client %}
</head>

<body>
<div id="app"></div>
{% vite_asset 'index.tsx' %}
</body>

</html>
39 changes: 27 additions & 12 deletions dev/django.dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
FROM python:3
FROM python:3.12-alpine

# Set the working directory inside the container
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Setup environment
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
ENV PYTHONUNBUFFERED 1s

# Install system dependencies
RUN apk update && apk upgrade && \
apk add --no-cache gcc g++ musl-dev curl libffi-dev postgresql-dev zlib-dev jpeg-dev freetype-dev

# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 -

# Add Poetry to PATH
ENV PATH="${PATH}:/root/.local/bin"

# Set up shell for pipe
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Copy only the pyproject.toml and poetry.lock to leverage Docker cache
COPY ./pyproject.toml .
COPY ./poetry.lock .

# Download Poetry into Path
RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python3 -
ENV PATH=/opt/poetry/bin:$PATH
# Install project dependencies
RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi

# Download dependencies
COPY pyproject.toml ./
COPY poetry.lock ./
# Copy the entire Django project to the working directory
COPY . .

RUN poetry config virtualenvs.create false && poetry install --no-interaction --sync
# Expose the port your application will run on
EXPOSE 8000

# Command to run the Django server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
20 changes: 20 additions & 0 deletions dev/vite.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use official Node.js 20 as base image
FROM node:20-alpine3.19

# Set working directory
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the frontend code
COPY . .

# Expose port 5175 for the client
EXPOSE 5175

# Start the Vite client in development mode
CMD [ "npm", "run", "dev" ]
52 changes: 52 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: CivicTechJobs

services:
pgdb:
image: postgres:12
container_name: pgdb
volumes:
- postgres_data:/lib/postgresql/data
env_file:
- dev/dev.env
ports:
- "5432:5432"

django:
build:
context: backend
dockerfile: ../dev/django.dockerfile
container_name: django
command: >
sh -c "python manage.py makemigrations &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000 &&
python manage.py generateschema --file openapi-schema.yml"
ports:
- "8000:8000"
env_file:
- dev/dev.env
depends_on:
- pgdb
develop:
watch:
- action: sync
path: ./backend
target: /usr/src/app

vite:
build:
context: frontend
dockerfile: ../dev/vite.dockerfile
container_name: vite
env_file:
- dev/dev.env
ports:
- "5175:5175"
develop:
watch:
- action: sync
path: ./frontend
target: /usr/src/app

volumes:
postgres_data: {}
4 changes: 3 additions & 1 deletion frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
frontend/static
frontend/templates
node_modules
dist/
dist/

vite.config.*.timestamp-*
Loading