Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

[feature request] Adicionar bootstrap inicial da aplicação #13

Merged
merged 7 commits into from
Sep 22, 2023
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
48 changes: 48 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Test application

on:
push:
pull_request:
types:
- reopened

jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: ["3.11"]
poetry-version: ["1.6.1"]

env:
DATABASE_URL: "sqlite:///db.sqlite3"

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install Poetry ${{ matrix.poetry-version }}
uses: snok/install-poetry@v1
with:
version: ${{ matrix.poetry-version }}
virtualenvs-create: false
installer-parallel: true

- name: Install dependencies
run: |
poetry run pip install --no-deps --upgrade pip
poetry install --with dev
- name: Run hooks
uses: pre-commit/[email protected]

- name: Run tests
run: |
poetry run python manage.py test \
--settings=server.settings.development
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ repos:
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies:
- 'django-stubs>=4.2.4,<5.0'
- 'django-environ>=0.11.2,<1.0'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# 2023-2-Squad06
# 2023-2-Squad06
Empty file added apps/templates/.keep
Empty file.
14 changes: 14 additions & 0 deletions bin/create-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# This script creates a .env file in the config directory and generates a
# random secret key for the Django app automatically.

read -r -d '' SECRET_KEY_CMD << EOM
from django.utils.crypto import get_random_string;
print(get_random_string(64))
EOM

SECRET_KEY=$(python3 -c "${SECRET_KEY_CMD}")
OUTPUT=$(sed "s/^\(DJANGO_SECRET_KEY=\).*/\1$SECRET_KEY/" config/.env.example)

echo "$OUTPUT" > config/.env
17 changes: 17 additions & 0 deletions config/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

############
# Django #
############

DJANGO_SECRET_KEY=

################
# PostgreSQL #
################

POSTGRES_HOST=postgres
POSTGRES_PORT=5432

POSTGRES_USER=virtualjudge
POSTGRES_PASSWORD=virtualjudge
POSTGRES_DB=virtualjudge
42 changes: 42 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
services:
django:
image: virtual-judge:dev
container_name: django
build:
context: .
dockerfile: docker/django/Dockerfile
target: development-build
args:
- DJANGO_ENV=development
- UID=${UID:-1000}
- GID=${GID:-1000}
cache_from:
- "virtual-judge:dev"
- "virtual-judge:latest"
- "*"
volumes:
- .:/app
ports:
- 8000:8000
networks:
- main
env_file:
- config/.env
command: python -Wd manage.py runserver 0.0.0.0:8000

postgres:
image: postgres:16.0-alpine
container_name: postgres
volumes:
- data:/var/lib/postgresql/data
networks:
- main
env_file:
- config/.env
restart: unless-stopped

volumes:
data:

networks:
main:
102 changes: 102 additions & 0 deletions docker/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# We use multi-stage builds to reduce the size of the image in production and
# to avoid installing unnecessary dependencies in the production image.
# See: https://docs.docker.com/develop/develop-images/multistage-build/

#######################
# Development stage #
#######################

FROM python:3.11.5-slim-bullseye AS development-build

ARG DJANGO_ENV \
# This is needed to fix permissions of files created in the container, so
# that they are owned by the host user.
UID=1000 \
GID=1000

ENV DJANGO_ENV=${DJANGO_ENV} \
# Python:
PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PYTHONDONTWRITEBYTECODE=1 \
# Pip:
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_DEFAULT_TIMEOUT=100 \
# Poetry:
POETRY_VERSION=1.6.1 \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
POETRY_HOME='/usr/local' \
# Tini
TINI_VERSION=v0.19.0 \
# Dockerize:
DOCKERIZE_VERSION=v0.7.0

SHELL ["/bin/bash", "-eo", "pipefail", "-c"]


# Install system dependencies:
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends \
bash \
brotli \
build-essential \
curl \
gettext \
git \
libpq-dev \
# Installing Poetry:
&& curl -sSL 'https://install.python-poetry.org' | python - \
&& poetry --version \
# Installing Dockerize:
&& curl -sSLO "https://github.com/jwilder/dockerize/releases/download/${DOCKERIZE_VERSION}/dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz" \
&& tar -C /usr/local/bin -xzvf "dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz" \
&& rm "dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz" \
&& dockerize --version \
# Installing Tini:
&& dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \
&& curl -o /usr/local/bin/tini -sSLO "https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-${dpkgArch}" \
&& chmod +x /usr/local/bin/tini \
&& tini --version \
# Cleaning cache:
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# We create a non-root user to run the application, so that we don't run the
# application as root.
RUN groupadd -g "${GID}" -r web \
&& useradd -d '/app' -g web -l -r -u "${UID}" web \
&& chown web:web -R '/app'

COPY --chown=web:web ./poetry.lock ./pyproject.toml /app/

RUN --mount=type=cache,target="$POETRY_CACHE_DIR" echo "${DJANGO_ENV}" \
&& poetry version \
&& poetry run pip install --no-deps --upgrade pip \
&& poetry install \
$(if [ "${DJANGO_ENV}" = 'production' ]; then echo '--only main'; fi) \
--no-interaction --no-ansi

COPY ./docker/django/entrypoint.sh /docker-entrypoint.sh

RUN chmod +x '/docker-entrypoint.sh'

USER web

# We customize how our app is loaded with the custom entrypoint:
ENTRYPOINT ["tini", "--", "/docker-entrypoint.sh"]


######################
# Production stage #
######################

FROM development AS production

COPY --chown=web:web . /app
21 changes: 21 additions & 0 deletions docker/django/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

readonly cmd="$*"

: "${POSTGRES_HOST:=postgres}"
: "${POSTGRES_PORT:=5432}"
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

# We need this line to make sure that this container is started after the one
# with PostgreSQL:
dockerize \
-wait "tcp://${POSTGRES_HOST}:${POSTGRES_PORT}" \
-timeout 90s

>&2 echo 'PostgreSQL is up -- continuing...'

exec $cmd
24 changes: 0 additions & 24 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
"""
MIT License

Copyright (c) 2023 MDS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from dataclasses import asdict
from typing import List

Expand Down
21 changes: 21 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys
from os import environ


def main() -> None:
environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings.development")

try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc

execute_from_command_line(sys.argv)


if __name__ == "__main__":
main()
Loading