From 51891564ed8dc24847bda55a66fbce92d550752d Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Fri, 9 Aug 2024 17:04:33 +0200 Subject: [PATCH 1/2] Update Docker config. Rename static to www --- .env | 9 ++++++--- .gitignore | 4 ++-- Dockerfile | 13 ++++++++----- config/settings.py | 6 ++++-- docker-compose.yml | 11 +++++++++-- docker/dev.yml | 2 +- nginx.conf | 19 +++++++++++++++++-- poetry.lock | 23 ++++++++++++++++++++++- pyproject.toml | 1 + {static => www}/es/index.html | 0 {static => www}/fr/index.html | 0 {static => www}/index.html | 0 {static => www}/robots.txt | 0 13 files changed, 70 insertions(+), 18 deletions(-) rename {static => www}/es/index.html (100%) rename {static => www}/fr/index.html (100%) rename {static => www}/index.html (100%) rename {static => www}/robots.txt (100%) diff --git a/.env b/.env index 284f920e..7150c5ce 100644 --- a/.env +++ b/.env @@ -5,10 +5,13 @@ COMPOSE_PATH_SEPARATOR=; # dev is default target COMPOSE_FILE=docker-compose.yml;docker/dev.yml -API_PORT=127.0.0.1:8000 +SECRET_KEY=key + +DEBUG=True -# CORS -CORS_ALLOW_ORIGINS=["http://localhost","http://localhost:8000","http://localhost:5173"] +ALLOWED_HOSTS="localhost,localhost:8000,localhost:5173" + +API_PORT=127.0.0.1:8000 # authentication server OAUTH2_SERVER_URL=https://world.openfoodfacts.org/cgi/auth.pl diff --git a/.gitignore b/.gitignore index ce7aec6f..f76b1ded 100644 --- a/.gitignore +++ b/.gitignore @@ -139,6 +139,6 @@ dmypy.json # Project specific -static/app -static/img/*/* +www/app +www/img/*/* gh_pages diff --git a/Dockerfile b/Dockerfile index 6dd88707..ce4c3535 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ ENV PYTHONUNBUFFERED=1 \ PYSETUP_PATH="/opt/pysetup" \ VENV_PATH="/opt/pysetup/.venv" \ POETRY_HOME="/opt/poetry" \ - POETRY_VERSION=1.6.1 \ + POETRY_VERSION=1.8.3 \ POETRY_VIRTUALENVS_IN_PROJECT=true \ POETRY_NO_INTERACTION=1 ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" @@ -46,20 +46,23 @@ RUN groupadd -g $USER_GID off && \ mkdir -p /opt/open-prices && \ mkdir -p /opt/open-prices/data && \ mkdir -p /opt/open-prices/img && \ + mkdir -p /opt/open-prices/static && \ chown off:off -R /opt/open-prices /home/off -COPY --chown=off:off app /opt/open-prices/app -COPY --chown=off:off alembic /opt/open-prices/alembic +COPY --chown=off:off config /opt/open-prices/config +COPY --chown=off:off open_prices /opt/open-prices/open_prices COPY docker/docker-entrypoint.sh /docker-entrypoint.sh RUN chmod +x /docker-entrypoint.sh -COPY --chown=off:off poetry.lock pyproject.toml alembic.ini /opt/open-prices/ +COPY --chown=off:off poetry.lock pyproject.toml manage.py /opt/open-prices/ USER off:off WORKDIR /opt/open-prices ENTRYPOINT /docker-entrypoint.sh $0 $@ -CMD ["uvicorn", "app.api:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"] +RUN ["python", "manage.py", "collectstatic", "--noinput"] + +CMD ["gunicorn", "config.wsgi", "--bind", "0.0.0.0:8000", "--workers", "1"] # building dev packages diff --git a/config/settings.py b/config/settings.py index 96cc01ce..c46e402c 100644 --- a/config/settings.py +++ b/config/settings.py @@ -9,12 +9,12 @@ # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = os.getenv("SECRET_KEY") +SECRET_KEY = os.getenv("SECRET_KEY", "set-in-production") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.getenv("DEBUG") == "True" -ALLOWED_HOSTS = [x.strip() for x in os.getenv("ALLOWED_HOSTS").split(",")] +ALLOWED_HOSTS = [x.strip() for x in os.getenv("ALLOWED_HOSTS", "").split(",")] # Application definition @@ -133,8 +133,10 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.0/howto/static-files/ +STATIC_ROOT = BASE_DIR / "static" STATIC_URL = "static/" + # Default primary key field type # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field diff --git a/docker-compose.yml b/docker-compose.yml index ad694c0a..661d62bc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,9 @@ x-api-common: &api-common - POSTGRES_PASSWORD - POSTGRES_HOST - POSTGRES_PORT - - CORS_ALLOW_ORIGINS + - SECRET_KEY + - DEBUG + - ALLOWED_HOSTS - OAUTH2_SERVER_URL - SENTRY_DNS - LOG_LEVEL @@ -21,6 +23,7 @@ services: - images:/opt/open-prices/img - data-dump:/opt/open-prices/data - home_cache:/home/off/.cache + - ./static:/opt/open-prices/static depends_on: - postgres @@ -55,7 +58,9 @@ services: volumes: # Mount the nginx configuration file - ./nginx.conf:/etc/nginx/nginx.conf - # Mount the static files + # Mount the static files (for index & vue.js app) + - ./www:/var/www + # Mount the Django staticfiles - ./static:/var/static # Mount the images - images:/var/img @@ -76,3 +81,5 @@ volumes: # store ~/.cache in a volume home_cache: name: ${COMPOSE_PROJECT_NAME:-open_prices}_home_cache + static: + name: ${COMPOSE_PROJECT_NAME:-open_prices}_static diff --git a/docker/dev.yml b/docker/dev.yml index 7d6a0d50..9b81814b 100644 --- a/docker/dev.yml +++ b/docker/dev.yml @@ -25,7 +25,7 @@ services: api: <<: *api-base # uvicorn in reload mode - command: ["uvicorn", "app.api:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8000", "--reload"] + command: ["gunicorn", "config.wsgi", "--bind", "0.0.0.0:8000", "--reload"] scheduler: <<: *api-base diff --git a/nginx.conf b/nginx.conf index 741d0191..458067d6 100644 --- a/nginx.conf +++ b/nginx.conf @@ -40,7 +40,7 @@ http { # There is a single server block for all requests listen 80 default_server; listen [::]:80 default_server; - root /var/static; + root /var/www; index index.html; location /api/ { @@ -53,6 +53,16 @@ http { client_max_body_size 50M; } + location /admin/ { + proxy_pass http://api:8000$request_uri; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_read_timeout 90; + client_max_body_size 50M; + } + location ~* \.(woff|woff2|otf|jpg|jpeg|png|webp|css|js|ttf|svg|ico)$ { add_header Cache-Control public; add_header Pragma public; @@ -62,7 +72,7 @@ http { # Make vue.js app available under /app location /app/ { - alias /var/static/app/; + alias /var/www/app/; try_files $uri $uri/ /app/index.html; location ~* \.(woff|woff2|otf|jpg|jpeg|png|webp|css|js|ttf|svg|ico)$ { @@ -73,6 +83,11 @@ http { } } + location /static { + # alias /var/static; + root /var/static; + } + location /img { alias /var/img; diff --git a/poetry.lock b/poetry.lock index 53bf5b46..26c0142a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -872,6 +872,27 @@ python-dateutil = ">=2.8.1" [package.extras] dev = ["flake8", "markdown", "twine", "wheel"] +[[package]] +name = "gunicorn" +version = "22.0.0" +description = "WSGI HTTP Server for UNIX" +optional = false +python-versions = ">=3.7" +files = [ + {file = "gunicorn-22.0.0-py3-none-any.whl", hash = "sha256:350679f91b24062c86e386e198a15438d53a7a8207235a78ba1b53df4c4378d9"}, + {file = "gunicorn-22.0.0.tar.gz", hash = "sha256:4a0b436239ff76fb33f11c07a16482c521a7e09c1ce3cc293c2330afe01bec63"}, +] + +[package.dependencies] +packaging = "*" + +[package.extras] +eventlet = ["eventlet (>=0.24.1,!=0.36.0)"] +gevent = ["gevent (>=1.4.0)"] +setproctitle = ["setproctitle"] +testing = ["coverage", "eventlet", "gevent", "pytest", "pytest-cov"] +tornado = ["tornado (>=0.2)"] + [[package]] name = "h11" version = "0.14.0" @@ -3306,4 +3327,4 @@ viz = ["matplotlib", "nc-time-axis", "seaborn"] [metadata] lock-version = "2.0" python-versions = "~3.11" -content-hash = "3f07d33dcf96837262464bf3d0d9a7e2e64e6ade12f9dbe49f127b6bb6fb546f" +content-hash = "2da4238e0adc5c14a9864c2043774e9e6302ce610a2311ea9e87b9d860859b84" diff --git a/pyproject.toml b/pyproject.toml index 28ab1fa7..ca2057a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,7 @@ django-q2 = "^1.6.2" croniter = "^3.0.3" blessed = "^1.20.0" ipython = "^8.26.0" +gunicorn = "^22.0.0" [tool.poetry.group.dev.dependencies] black = "~23.12.1" diff --git a/static/es/index.html b/www/es/index.html similarity index 100% rename from static/es/index.html rename to www/es/index.html diff --git a/static/fr/index.html b/www/fr/index.html similarity index 100% rename from static/fr/index.html rename to www/fr/index.html diff --git a/static/index.html b/www/index.html similarity index 100% rename from static/index.html rename to www/index.html diff --git a/static/robots.txt b/www/robots.txt similarity index 100% rename from static/robots.txt rename to www/robots.txt From 52f0803d6b34af5f679a0038d98203eb70bd653d Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Mon, 12 Aug 2024 20:06:48 +0200 Subject: [PATCH 2/2] temp --- config/settings.py | 4 ++-- nginx.conf | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/config/settings.py b/config/settings.py index c46e402c..f05c4adf 100644 --- a/config/settings.py +++ b/config/settings.py @@ -133,8 +133,8 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.0/howto/static-files/ -STATIC_ROOT = BASE_DIR / "static" -STATIC_URL = "static/" +STATIC_ROOT = os.path.join(BASE_DIR, "static") +STATIC_URL = "/static/" # Default primary key field type diff --git a/nginx.conf b/nginx.conf index 458067d6..17a89659 100644 --- a/nginx.conf +++ b/nginx.conf @@ -83,9 +83,8 @@ http { } } - location /static { - # alias /var/static; - root /var/static; + location /static/ { + root /var; } location /img {