configure nginx and front

This commit is contained in:
b1ek 2023-05-17 06:57:23 +10:00
parent a29095b68b
commit 0ffd513a2f
Signed by: blek
GPG Key ID: 14546221E3595D0C
14 changed files with 102 additions and 74 deletions

View File

@ -4,4 +4,5 @@
# Запуск локального сайта
1. Создайте `.env` файл, рекомендуем использовать для этого шаблон `.env.example`
2. Сгенерируйте ключи `genkeys.sh`
3. Постройте статичные файлы `build.sh`
3. Поднимите контейнеры `docker-compose up -d`

74
build.sh Executable file
View File

@ -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 -----

View File

@ -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;
}
}
}

View File

@ -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 -----

View File

@ -3,4 +3,4 @@
PYTHON='/usr/bin/env python3'
$PYTHON manage.py migrate
$PYTHON manage.py runserver 0.0.0.0:80
$PYTHON -m gunicorn -w 3 -b 0.0.0.0:80 pairent_backend.wsgi

View File

@ -0,0 +1 @@
templates/index.html

View File

@ -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):

View File

@ -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'"
}

View File

@ -1,20 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Pairent</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap"
rel="stylesheet"
/>
<script type="module" crossorigin src="/assets/index-dc786e36.js"></script>
<link rel="stylesheet" href="/assets/index-b130f2c5.css">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>

View File

@ -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),

View File

@ -6,3 +6,4 @@ Pillow
requests
python-ipware
python-dotenv
gunicorn

View File

@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"type": "module",
"homepage": "/static/dist",
"dependencies": {
"axios": "^1.4.0",
"bootstrap": "^5.2.3",

View File

@ -5,6 +5,6 @@ export default {
plugins: [
react()
],
publicDir: '/static/dist'
publicDir: 'static/dist',
}

2
static/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore