Skip to content

Commit

Permalink
Merge pull request #19 from codersforcauses/chore/template_sync_80332e7
Browse files Browse the repository at this point in the history
New devcontainer setup
  • Loading branch information
dct0 authored Jun 26, 2024
2 parents ad91626 + 494a124 commit 62ecc97
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 76 deletions.
6 changes: 3 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

// Use this environment variable if you need to bind mount your local source code into a new container.
"remoteEnv": {
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}"
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder:-.}"
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [3000, 8000, 8080]
// "forwardPorts": [3000, 8000, 8080],

// Use 'postCreateCommand' to run commands after the container is created.
// Note: Useful for when they open it and want everything to just start
// "postCreateCommand": "docker compose up -d"
"postCreateCommand": "./.devcontainer/setup.sh"
}
22 changes: 22 additions & 0 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# Runs once after devcontainer is created

figlet "1-time Setup"
echo "Running one-time setup for you :)"

# Create .env files if it doesn't exist
if [ ! -f ./client/.env ]; then
cp ./client/.env.example ./client/.env
fi
if [ ! -f ./server/.env ]; then
cp ./server/.env.example ./server/.env
fi

# Give editor intellisense
(cd server && POETRY_VIRTUALENVS_CREATE=false poetry install)
(cd client && npm install)

# Build containers so less waiting
docker compose pull
docker compose build --no-cache
37 changes: 18 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@ Django + Nextjs Template: Standardised CFC Tech Stack
## Get started

0. Activate the dev container in VSCode
1. Create a copy of `.env.example` found in the `client` folder and name it `.env`
2. Create a copy of `.env.example` found in the `server` folder and name it `.env`
3. Start the database in the background with `docker compose up -d`
4. `cd server` then run `poetry install`
5. Run `python manage.py migrate` to apply migrations to the database
6. Start the backend with `python manage.py runserver`, you'll also get a popup in VSCode telling you to open it in a browser
7. In a new terminal, `cd client`
8. Install deps, `npm install`
9. Run the frontend `npm run dev`
10. Click the popup that shows in the bottom right of VSCode to open it in a browser
1. Start the db, server and client with `docker compose up`
2. Server is at `localhost:8000`, client at `localhost:3000`

## Server

### Create superuser
### Create and run migrations

To log in to the Django admin dashboard, you'll need to create a superuser with the following command:
If the models are updated, be sure to create a migration:

```bash
python manage.py createsuperuser
docker container exec server python manage.py makemigrations # create a new migration OR
dxc server python manage.py makemigrations
```

### Create and run migrations

If the models are updated, be sure to create a migration:
## Other

```bash
python manage.py makemigrations # create a new migration
python manage.py migrate # apply migrations
```
### Get Intellisense

If you're in the dev container, this should be done already. You can run `poetry install` and `npm install` in the correct folders to get the latest dependencies.

### Editing Docker stuff

If you modify anything in the `docker` folder, you need to add the `--build` flag or Docker won't give you the latest changes.

### Custom env vars

Edit the `.env` file in the respective directory (client or server).
55 changes: 28 additions & 27 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
services:
db:
container_name: db
image: postgres
restart: unless-stopped
volumes:
- ${LOCAL_WORKSPACE_FOLDER}/data/db:/var/lib/postgresql/data
- ${LOCAL_WORKSPACE_FOLDER:-.}/data/db:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 3s
timeout: 3s
retries: 5
env_file:
- ./server/.env
env_file: ./server/.env
ports:
- 5432:5432
# server:
# build:
# context: .
# dockerfile: ./docker/server/Dockerfile
# restart: unless-stopped
# env_file: ./server/.env
# ports:
# - 8000:8000
# volumes:
# - ./server:/app

# client:
# build:
# context: .
# dockerfile: ./docker/client/Dockerfile
# restart: unless-stopped
# env_file: ./client/.env
# ports:
# - 3000:3000
# volumes:
# - ./client:/app
# - ignore:/app/node_modules
server:
container_name: server
build:
context: .
dockerfile: ./docker/server/Dockerfile
restart: unless-stopped
env_file: ./server/.env
ports:
- 8000:8000
volumes:
- ${LOCAL_WORKSPACE_FOLDER:-.}/server:/app
client:
container_name: client
build:
context: .
dockerfile: ./docker/client/Dockerfile
restart: unless-stopped
env_file: ./client/.env
ports:
- 3000:3000
volumes:
- ${LOCAL_WORKSPACE_FOLDER:-.}/client:/app
- ignore:/app/node_modules

# volumes:
# ignore:
volumes:
ignore:
39 changes: 14 additions & 25 deletions docker/client/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ echo "Image Build Date/Time: " "$(cat /app/build_timestamp.txt)" "UTC"
echo "-----------------------------------------------------------"
echo "APP_ENV: ${APP_ENV}"

# ====================================================================================
# Debug / Sanity check info
# ====================================================================================
echo " "
echo "======= Current Dir / Files (Debug) ============================================================================="
pwd
ls -al

echo " "
echo "======= Env Vars (Debug) ========================================================================================"
if [ "${APP_ENV^^}" != "PRODUCTION" ]; then
# Only print environment vars in non-prod environments to prevent sensitive variables being sent to logging system
printenv
fi
# # ====================================================================================
# # Debug / Sanity check info
# # ====================================================================================
# echo " "
# echo "======= Current Dir / Files (Debug) ============================================================================="
# pwd
# ls -al

# echo " "
# echo "======= Env Vars (Debug) ========================================================================================"
# if [ "${APP_ENV^^}" != "PRODUCTION" ]; then
# # Only print environment vars in non-prod environments to prevent sensitive variables being sent to logging system
# printenv
# fi

echo " "
echo "======= Linux version (Debug) ==================================================================================="
Expand All @@ -42,17 +42,6 @@ if [[ -z "${APP_ENV}" ]]; then
exit
fi

# ====================================================================================
# Install extra dependencies if ENV is LOCAL
# ====================================================================================
if [ "${APP_ENV^^}" = "DEVELOPMENT" ]; then

# Install some extras
echo " "
echo "======= Installing extra libraries just for DEVELOPMENT env ======================================================="
npm install --production=false
fi

# CI TEST DOWN THE TRACK

# ====================================================================================
Expand Down
2 changes: 1 addition & 1 deletion docker/server/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ python manage.py collectstatic --noinput

# Create Django Superuser
echo "Creating Django Superuser"
python manage.py create_superuser --username $DJANGO_SUPERUSER_USERNAME --password $DJANGO_SUPERUSER_PASSWORD
python manage.py createsuperuser --noinput

# Run inbuilt Django server if ENV is development
if [ "${APP_ENV^^}" = "DEVELOPMENT" ]; then
Expand Down
6 changes: 5 additions & 1 deletion server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ APP_ENV=DEVELOPMENT
API_SECRET_KEY="supersecretkey"
API_ALLOWED_HOSTS=".localhost 127.0.0.1 [::1]"

POSTGRES_HOST=172.17.0.1 # use localhost if not using dev container or try host.docker.internal
POSTGRES_HOST=db
POSTGRES_NAME=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_PORT=5432

DJANGO_SUPERUSER_PASSWORD=Password123
DJANGO_SUPERUSER_EMAIL=[email protected]
DJANGO_SUPERUSER_USERNAME=admin

FRONTEND_URL="http://localhost:3000"

0 comments on commit 62ecc97

Please sign in to comment.