Skip to content
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Remove the variable and all commands in Makefile will use docker
USE_LOCAL=1
# If you want deploy app on heroku makefile have "h-deploy" goal, set app name here
HEROKU_APP_NAME=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ frontend/node_modules/*
frontend/dist/*
frontend/manifest.json
.coverage
staticroot
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# A single container to publish/run your app on a server

FROM node:15-alpine AS frontend
WORKDIR /app
ADD ./frontend /app
RUN yarn install && yarn build

FROM python:3-alpine
WORKDIR /app
ADD . /app
RUN apk update \
&& pip install gunicorn \
&& pip install -r requirements/base.txt \
&& rm -rf /var/cache/apk/*
COPY --from=frontend /app ./frontend
RUN python manage.py collectstatic --noinput

CMD docker/backend-cmd.sh
64 changes: 55 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
# Dev servers
CMD_PREFIX=./docker/compose.sh
MANAGE=$(CMD_PREFIX) run backend python manage.py
ifneq (,$(wildcard ./.env))
include .env
export
endif

DOCKER_CMD_PREFIX=./docker/compose.sh
ifeq (,$(USE_LOCAL))
MANAGE=$(DOCKER_CMD_PREFIX) run --rm backend python manage.py
else
MANAGE=python manage.py
endif
TEST_SETTINGS=fortytwo.test_settings
TEST_APP=apps/

# Dev servers
run:
@echo Starting http://127.0.0.1:8000
$(CMD_PREFIX) up

build:
$(CMD_PREFIX) build
ifeq (,$(USE_LOCAL))
$(DOCKER_CMD_PREFIX) up
else
(trap 'kill 0' SIGINT; $(MANAGE) runserver & yarn --cwd frontend serve)
endif

# Database
migrate:
Expand All @@ -29,8 +39,13 @@ shell:
@echo Starting shell...
$(MANAGE) shell

cmd=black apps && flake8 apps
lint:
$(CMD_PREFIX) run backend sh -c "black apps && flake8 apps"
ifeq (,$(USE_LOCAL))
$(DOCKER_CMD_PREFIX) run backend sh -c "$(cmd)"
else
$(cmd)
endif

djangotest:
$(MANAGE) test --settings=$(TEST_SETTINGS) $(TEST_APP) --noinput
Expand All @@ -45,4 +60,35 @@ collectstatic:
$(MANAGE) collectstatic --noinput

eslint:
$(CMD_PREFIX) run frontend sh -c "cd frontend && yarn lint src --fix"
ifeq (,$(USE_LOCAL))
$(DOCKER_CMD_PREFIX) run frontend sh -c "yarn --cwd /app/frontend lint src --fix"
else
yarn --cwd frontend lint src --fix
endif

# Deploy
DEPLOY_CONT_ID=fortytwotesttask
build:
ifeq (,$(USE_LOCAL))
docker build --tag=$(DEPLOY_CONT_ID):latest .
else
yarn --cwd frontend install
yarn --cwd frontend build
$(MAKE) collectstatic
endif

server:
ifeq (,$(USE_LOCAL))
docker run -p=8000:8000 --rm --name $(DEPLOY_CONT_ID) $(DEPLOY_CONT_ID):latest
else
./docker/backend-cmd.sh
endif

h-deploy:
ifeq (,$(HEROKU_APP_NAME))
@echo Missing HEROKU_APP_NAME env var
else
@echo Deploying on heroku
heroku container:push web --app $(HEROKU_APP_NAME)
heroku container:release web --app $(HEROKU_APP_NAME)
endif
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

A Django 3+ project template

### Requirements
* SPA
* global: `yarn`, `node` (tested on v15), `python>=3.7` (tested on 3.9)
### System requirements
* global: yarn, node (tested on v15), python>=3.7 (tested on 3.9) *OR* docker

### Code/deployment requirements
* management commands should be proxied to single word make commands, e.g make test
* app should be deployed on a server of your choice (heroku, pythonanywhere, aws, etc.)

### Initial setup
You can develop app in docker container or directly on host, check `.env.example`
Most usable commands are proxied in the `Makefile`
1 change: 0 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ services:
dockerfile: docker/backend.Dockerfile
container_name: backend
restart: always
# mount the volumes so we can change code and hot reload
volumes:
- .:/app
ports:
Expand Down
2 changes: 1 addition & 1 deletion docker/backend-cmd.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
python manage.py migrate --noinput
python manage.py createcachetable
supervisord -c /etc/supervisord.conf
gunicorn fortytwo.wsgi -b 0.0.0.0:"${PORT:-8000}" --reload
7 changes: 2 additions & 5 deletions docker/backend.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
FROM python:3-alpine

WORKDIR /app
ADD . /app
COPY . /app

# gcc, libc-dev are flake8 (typed-ast) deps
RUN apk update \
&& apk add make supervisor gcc libc-dev \
&& pip install gunicorn \
&& pip install -r requirements/base.txt \
&& apk add make gcc libc-dev \
&& pip install -r requirements/dev.txt \
&& echo "files = /app/docker/conf/supervisor.ini" >> /etc/supervisord.conf \
&& rm -rf /var/cache/apk/*

CMD docker/backend-cmd.sh
13 changes: 0 additions & 13 deletions docker/conf/supervisor.ini

This file was deleted.

4 changes: 1 addition & 3 deletions docker/frontend-cmd.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
cd /app/frontend
yarn
yarn serve
yarn --cwd /app/frontend serve
3 changes: 2 additions & 1 deletion docker/frontend.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM node:15-alpine

WORKDIR /app
ADD . /app
COPY . /app
RUN yarn --cwd /app/frontend install

CMD docker/frontend-cmd.sh
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Django==3.0.8
djangorestframework==3.12.2
django-webpack4-loader==0.0.5
gunicorn==20.1.0
2 changes: 2 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-r base.txt

42cc-pystyle
black==20.8b1
flake8==3.8.3
Expand Down