From 0ffd513a2f5726ab8981607cab0ffa9b6f435f3f Mon Sep 17 00:00:00 2001 From: b1ek Date: Wed, 17 May 2023 06:57:23 +1000 Subject: [PATCH] configure nginx and front --- README.md | 1 + build.sh | 74 +++++++++++++++++++ conf/nginx/nginx.conf | 10 ++- genkeys.sh | 39 ---------- pairent_backend/docker-entry.sh | 2 +- pairent_backend/pairent_app/.gitignore | 1 + pairent_backend/pairent_app/views.py | 2 +- pairent_backend/pairent_backend/settings.py | 16 ++-- .../pairent_backend/templates/index.html | 20 ----- pairent_backend/pairent_backend/urls.py | 3 +- pairent_backend/requirements.txt | 3 +- pairent_frontend_react/package.json | 1 + pairent_frontend_react/vite.config.js | 2 +- static/.gitignore | 2 + 14 files changed, 102 insertions(+), 74 deletions(-) create mode 100755 build.sh create mode 100644 pairent_backend/pairent_app/.gitignore delete mode 100644 pairent_backend/pairent_backend/templates/index.html create mode 100644 static/.gitignore diff --git a/README.md b/README.md index c5c94ae..3bfce55 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,5 @@ # Запуск локального сайта 1. Создайте `.env` файл, рекомендуем использовать для этого шаблон `.env.example` 2. Сгенерируйте ключи `genkeys.sh` +3. Постройте статичные файлы `build.sh` 3. Поднимите контейнеры `docker-compose up -d` \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..fba5196 --- /dev/null +++ b/build.sh @@ -0,0 +1,74 @@ +#!/bin/sh + +# ----- Start commands ----- + +fatal_err() { + echo -e "\033[31mFatal error $*\033[0m" +} + +# ----- End commands ----- + + + +# ----- Start safeguards ----- + +if [[ "$1" != '-a' ]]; then + fatal_err + echo " This script will potentially break any existing instance of Pairent." + echo " To execute this script, re-run it with option -a as first argument." + exit -1 +fi + +if ! [ -f .env ]; then + fatal_err + echo -e " No .env file was found." + echo -e " Please use the \033[32m.env.example\033[0m to create a dotenv file:" + echo -e " 1. \033[34mcp .env.example .env\033[0m" + echo -e " 2. Edit your \033[32m.env\033[0m in your favourite editor" + exit -1 +fi + +# ----- End safeguards ----- + + + +# ----- Start bootstrap front ----- + +echo Building frontend static files... + +cd pairent_frontend_react + +NPMS=('npm' 'pnpm' 'yarn') +NPM='None' + +for n in ${NPMS[@]}; do + if [ -x "$(command -v "$n")" ]; then + NPM=$n + fi +done + +if [[ "$NPM" == 'None' ]]; then + fatal_err + echo ' No node package manager was found in your system.' + echo ' Please install one of the following:' + for n in ${NPMS[@]}; do + echo -e " $n" + done + exit -1 +fi + +rm -rf node_modules + +$NPM install +$NPM run build + +DIST_FOLDER=$(realpath dist) + +cd .. +cp -r $DIST_FOLDER/* static +cp $DIST_FOLDER/index.html pairent_backend/pairent_app/templates +cp $DIST_FOLDER/../public/* static -r + +echo Done building frontend static files + +# ----- End bootstrap front ----- \ No newline at end of file diff --git a/conf/nginx/nginx.conf b/conf/nginx/nginx.conf index 896e473..580c430 100644 --- a/conf/nginx/nginx.conf +++ b/conf/nginx/nginx.conf @@ -26,13 +26,17 @@ http { server { listen 80; - location / { + location ~ ^/(api|admin|index.html|/)$ { proxy_pass http://back; } - location /static { - root /var/www/static; + location / { + root /var/www/html; try_files $uri $uri/ index.html; } + + location ~ ^/\. { + deny all; + } } } \ No newline at end of file diff --git a/genkeys.sh b/genkeys.sh index 14845bc..6ea1bf6 100755 --- a/genkeys.sh +++ b/genkeys.sh @@ -56,42 +56,3 @@ ln -s .env pairent_backend/.env # ----- End bootstrap back ----- exit - -# ----- Start bootstrap front ----- - -echo Building frontend static files... - -cd pairent_frontend_react - -NPMS=('npm' 'pnpm' 'yarn') -NPM='None' - -for n in ${NPMS[@]}; do - if [ -x "$(command -v "$n")" ]; then - NPM=$n - fi -done - -if [[ "$NPM" == 'None' ]]; then - fatal_err - echo ' No node package manager was found in your system.' - echo ' Please install one of the following:' - for n in ${NPMS[@]}; do - echo -e " $n" - done - exit -1 -fi - -rm -rf node_modules - -$NPM install -$NPM run build - -DIST_FOLDER=$(realpath dist) - -cd .. -cp -r $DIST_FOLDER/* static - -echo Done building frontend static files - -# ----- End bootstrap front ----- \ No newline at end of file diff --git a/pairent_backend/docker-entry.sh b/pairent_backend/docker-entry.sh index 22efc3c..1b98e55 100644 --- a/pairent_backend/docker-entry.sh +++ b/pairent_backend/docker-entry.sh @@ -3,4 +3,4 @@ PYTHON='/usr/bin/env python3' $PYTHON manage.py migrate -$PYTHON manage.py runserver 0.0.0.0:80 \ No newline at end of file +$PYTHON -m gunicorn -w 3 -b 0.0.0.0:80 pairent_backend.wsgi \ No newline at end of file diff --git a/pairent_backend/pairent_app/.gitignore b/pairent_backend/pairent_app/.gitignore new file mode 100644 index 0000000..559b34a --- /dev/null +++ b/pairent_backend/pairent_app/.gitignore @@ -0,0 +1 @@ +templates/index.html diff --git a/pairent_backend/pairent_app/views.py b/pairent_backend/pairent_app/views.py index 840307c..05ed745 100644 --- a/pairent_backend/pairent_app/views.py +++ b/pairent_backend/pairent_app/views.py @@ -21,7 +21,7 @@ from .serializer import (ApartamentListSerializer, from .authlib import * -import json, math, random, re, requests, oidc_client, base64, uuid, time, ipware as iplib +import json, math, random, re, requests, base64, uuid, time, ipware as iplib ipware = iplib.IpWare(); class ApartamentViewSet(viewsets.ReadOnlyModelViewSet): diff --git a/pairent_backend/pairent_backend/settings.py b/pairent_backend/pairent_backend/settings.py index 9caabb4..37fcd3a 100644 --- a/pairent_backend/pairent_backend/settings.py +++ b/pairent_backend/pairent_backend/settings.py @@ -20,12 +20,12 @@ BASE_DIR = Path(__file__).resolve().parent.parent # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ -DOTENV = dotenv_values(BASE_DIR / '..' / '.env'); +DOTENV = dotenv_values(BASE_DIR / '.env'); # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = DOTENV['DJANGO_KEY'] # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = DOTENV['APP_DEBUG'] == 'true' +DEBUG = True#DOTENV['APP_DEBUG'] == 'true' ALLOWED_HOSTS = ['*'] @@ -74,7 +74,9 @@ ROOT_URLCONF = 'pairent_backend.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [ + 'templates/' + ], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ @@ -97,10 +99,10 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'pairent_db', - 'USER': 'django', - 'PASSWORD': 'Budnya1924$', - 'HOST': '178.20.44.123', - 'PORT': '3306', + 'USER': DOTENV['DB_USER'], + 'PASSWORD': DOTENV['DB_PASSWORD'], + 'HOST': DOTENV['DB_HOST'], + 'PORT': DOTENV['DB_PORT'], 'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" } diff --git a/pairent_backend/pairent_backend/templates/index.html b/pairent_backend/pairent_backend/templates/index.html deleted file mode 100644 index 7b2ba46..0000000 --- a/pairent_backend/pairent_backend/templates/index.html +++ /dev/null @@ -1,20 +0,0 @@ - - - - - Pairent - - - - - - - - -
- - - diff --git a/pairent_backend/pairent_backend/urls.py b/pairent_backend/pairent_backend/urls.py index 96340ac..1601e09 100644 --- a/pairent_backend/pairent_backend/urls.py +++ b/pairent_backend/pairent_backend/urls.py @@ -16,9 +16,10 @@ Including another URLconf from django.template import loader from django.contrib import admin from django.urls import path, include +from django.http import HttpResponse def root_url(req): - return loader.get_template('index.html').render() + return HttpResponse(loader.get_template('index.html').render()) urlpatterns = [ path('admin/', admin.site.urls), diff --git a/pairent_backend/requirements.txt b/pairent_backend/requirements.txt index bb650f4..5b3d1ab 100644 --- a/pairent_backend/requirements.txt +++ b/pairent_backend/requirements.txt @@ -5,4 +5,5 @@ django-cors-headers Pillow requests python-ipware -python-dotenv \ No newline at end of file +python-dotenv +gunicorn \ No newline at end of file diff --git a/pairent_frontend_react/package.json b/pairent_frontend_react/package.json index 96546ba..68ebc0e 100644 --- a/pairent_frontend_react/package.json +++ b/pairent_frontend_react/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "private": true, "type": "module", + "homepage": "/static/dist", "dependencies": { "axios": "^1.4.0", "bootstrap": "^5.2.3", diff --git a/pairent_frontend_react/vite.config.js b/pairent_frontend_react/vite.config.js index dc49f83..82c7627 100644 --- a/pairent_frontend_react/vite.config.js +++ b/pairent_frontend_react/vite.config.js @@ -5,6 +5,6 @@ export default { plugins: [ react() ], - publicDir: '/static/dist' + publicDir: 'static/dist', } \ No newline at end of file diff --git a/static/.gitignore b/static/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/static/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file