дубль два тащим все в мастер, решать надо или нет потом будем
а если серьезно, то внесенные изменения: почти полность изменил структуру проекта, чувствительные данные теперь находятся в файле .env, немного поправил docker-compose.yml в сторону best practice
This commit is contained in:
parent
51e0019fd6
commit
ffc73c7657
10
.env
Normal file
10
.env
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#Пример .env файла. Используем его чтобы не указывать чувствительные данные (пароли, ключи, API и т.д.) напрямую в коде
|
||||||
|
# PostgreSQL
|
||||||
|
POSTGRES_USER=user
|
||||||
|
POSTGRES_PASSWORD=password
|
||||||
|
POSTGRES_DB=website
|
||||||
|
DB_HOST=database
|
||||||
|
|
||||||
|
# Nginx bublick & private key
|
||||||
|
SSL_CERT_FILE=./nginx/ssl/domain.crt
|
||||||
|
SSL_KEY_FILE=./nginx/ssl/domain.key
|
@ -1 +1 @@
|
|||||||
Первый пуш, не гарантирую работоспособность этого кода
|
Все в дом все в дом
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
Flask
|
Flask
|
||||||
psycopg2-binary
|
psycopg2-binary
|
||||||
|
python-dotenv
|
@ -1,20 +1,26 @@
|
|||||||
from flask import Flask, request, render_template
|
from flask import Flask, request, render_template
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
app = Flask(__name__, template_folder='/media/frontend')
|
app = Flask(__name__, template_folder='/media/frontend')
|
||||||
|
|
||||||
|
|
||||||
DATABASE = {
|
DATABASE = {
|
||||||
'dbname': 'WebSite',
|
'dbname': os.getenv('POSTGRES_DB'),
|
||||||
'user': 'User',
|
'user': os.getenv('POSTGRES_USER'),
|
||||||
'password': 'Password',
|
'password': os.getenv('POSTGRES_PASSWORD'),
|
||||||
'host': 'DataBase',
|
'host': os.getenv('DB_HOST')
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_db_connection():
|
def get_db_connection():
|
||||||
conn = psycopg2.connect(**DATABASE)
|
try:
|
||||||
return conn
|
conn = psycopg2.connect(**DATABASE)
|
||||||
|
return conn
|
||||||
|
except psycopg2.Error as e:
|
||||||
|
print(f"Ошибка подключения к базе данных: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
def index():
|
def index():
|
||||||
@ -23,45 +29,50 @@ def index():
|
|||||||
@app.route('/submit', methods=['POST'])
|
@app.route('/submit', methods=['POST'])
|
||||||
def submit():
|
def submit():
|
||||||
action = request.form.get('action')
|
action = request.form.get('action')
|
||||||
|
message = ""
|
||||||
|
|
||||||
if action == 'Login':
|
try:
|
||||||
username = request.form.get('username')
|
|
||||||
password = request.form.get('password')
|
|
||||||
|
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
cur.execute('SELECT * FROM users WHERE username = %s AND password = %s', (username, password))
|
if action == 'Login':
|
||||||
user = cur.fetchone()
|
username = request.form.get('username')
|
||||||
|
password = request.form.get('password')
|
||||||
|
|
||||||
cur.close()
|
cur.execute(
|
||||||
conn.close()
|
'SELECT * FROM users WHERE username = %s AND password = %s',
|
||||||
|
(username, password)
|
||||||
|
)
|
||||||
|
user = cur.fetchone()
|
||||||
|
|
||||||
|
message = "Успешный вход!" if user else "Неправильные имя пользователя или пароль!"
|
||||||
|
|
||||||
|
elif action == 'Register':
|
||||||
|
new_username = request.form.get('new_username')
|
||||||
|
new_password = request.form.get('new_password')
|
||||||
|
|
||||||
|
cur.execute('SELECT * FROM users WHERE username = %s', (new_username,))
|
||||||
|
if cur.fetchone():
|
||||||
|
message = "Пользователь уже существует!"
|
||||||
|
else:
|
||||||
|
cur.execute(
|
||||||
|
'INSERT INTO users (username, password) VALUES (%s, %s)',
|
||||||
|
(new_username, new_password)
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
message = "Успешная регистрация!"
|
||||||
|
|
||||||
if user:
|
|
||||||
message = "Login successful!"
|
|
||||||
else:
|
else:
|
||||||
message = "Invalid username or password."
|
message = "Неизвестное действие"
|
||||||
|
|
||||||
elif action == 'Register':
|
except psycopg2.Error as e:
|
||||||
new_username = request.form.get('new_username')
|
conn.rollback()
|
||||||
new_password = request.form.get('new_password')
|
message = f"Ошибка базы данных: {e}"
|
||||||
|
finally:
|
||||||
conn = get_db_connection()
|
if 'cur' in locals():
|
||||||
cur = conn.cursor()
|
cur.close()
|
||||||
|
if 'conn' in locals():
|
||||||
cur.execute('SELECT * FROM users WHERE username = %s', (new_username,))
|
conn.close()
|
||||||
if cur.fetchone():
|
|
||||||
message = "User already exists!"
|
|
||||||
else:
|
|
||||||
cur.execute('INSERT INTO users (username, password) VALUES (%s, %s)', (new_username, new_password))
|
|
||||||
conn.commit()
|
|
||||||
message = "Registration successful!"
|
|
||||||
|
|
||||||
cur.close()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
else:
|
|
||||||
message = "Invalid action."
|
|
||||||
|
|
||||||
return render_template('index.html', message=message)
|
return render_template('index.html', message=message)
|
||||||
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
Наш сертификат
|
|
@ -1 +0,0 @@
|
|||||||
Приватный ключ
|
|
@ -1,5 +1,5 @@
|
|||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
username VARCHAR(50) UNIQUE NOT NULL,
|
username VARCHAR(50) UNIQUE NOT NULL,
|
||||||
password VARCHAR(50) NOT NULL
|
password VARCHAR(50) NOT NULL
|
||||||
);
|
);
|
51
docker-compose.yaml
Normal file
51
docker-compose.yaml
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
services:
|
||||||
|
database:
|
||||||
|
image: postgres:17.4-alpine3.21
|
||||||
|
container_name: database
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: ${POSTGRES_DB}
|
||||||
|
POSTGRES_USER: ${POSTGRES_USER}
|
||||||
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||||
|
|
||||||
|
#Монтируем директорию на хосте, чтобы при повторных "docker-compose up" таблицы с нашими данными сохранялись
|
||||||
|
volumes:
|
||||||
|
- /database:/var/lib/postgresql/data
|
||||||
|
cpus: '0.15'
|
||||||
|
mem_limit: 256M
|
||||||
|
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U admin -WebSite"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
webserver:
|
||||||
|
image: nginx:1.27.4-alpine
|
||||||
|
container_name: webserver
|
||||||
|
ports:
|
||||||
|
- "80:80"
|
||||||
|
- "443:443"
|
||||||
|
volumes:
|
||||||
|
- /WebApp/frontend:/usr/share/nginx/html
|
||||||
|
- /WebApp/nginx/ssl:/etc/nginx/sites-available
|
||||||
|
- /WebApp/nginx/nginx.conf:/etc/nginx/nginx.conf
|
||||||
|
|
||||||
|
depends_on:
|
||||||
|
- database
|
||||||
|
- backend
|
||||||
|
cpus: '0.15'
|
||||||
|
mem_limit: 256M
|
||||||
|
backend:
|
||||||
|
image: python:3.9
|
||||||
|
container_name: backend_part
|
||||||
|
ports:
|
||||||
|
- "5000:5000"
|
||||||
|
volumes:
|
||||||
|
- /WebApp/backend:/media/backend
|
||||||
|
working_dir: /media/backend
|
||||||
|
command: >
|
||||||
|
sh -c "pip install -r requirements.txt && python server.py"
|
||||||
|
cpus: '0.35'
|
||||||
|
mem_limit: 256M
|
@ -1,51 +0,0 @@
|
|||||||
services:
|
|
||||||
|
|
||||||
DataBase:
|
|
||||||
image: postgres:latest
|
|
||||||
container_name: DataBase
|
|
||||||
ports:
|
|
||||||
- "5432:5432"
|
|
||||||
#Так никто не делает в реальных кейсах, я просто даун, не умею работать с секретами (опция secrets)
|
|
||||||
environment:
|
|
||||||
POSTGRES_USER: user
|
|
||||||
POSTGRES_PASSWORD: password
|
|
||||||
POSTGRES_DB: WebSite
|
|
||||||
volumes:
|
|
||||||
- /home/git/myprojects/database:/var/lib/postgresql/data
|
|
||||||
cpus: '0.15'
|
|
||||||
mem_limit: 256M
|
|
||||||
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "pg_isready -U admin -WebSite"]
|
|
||||||
interval: 5s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
|
|
||||||
webserver:
|
|
||||||
image: nginx:latest
|
|
||||||
container_name: WebServer
|
|
||||||
ports:
|
|
||||||
- "80:80"
|
|
||||||
- "443:443"
|
|
||||||
volumes:
|
|
||||||
- /home/git/myprojects/WorkServer/frontend:/usr/share/nginx/html
|
|
||||||
- /home/git/myprojects/WorkServer/conf:/etc/nginx/sites-available
|
|
||||||
|
|
||||||
depends_on:
|
|
||||||
- DataBase
|
|
||||||
- backend
|
|
||||||
cpus: '0.15'
|
|
||||||
mem_limit: 256M
|
|
||||||
backend:
|
|
||||||
image: python:3.9
|
|
||||||
container_name: backend_part
|
|
||||||
ports:
|
|
||||||
- "5000:5000"
|
|
||||||
volumes:
|
|
||||||
- /home/git/myprojects/WorkServer/backend:/media/backend
|
|
||||||
- /home/git/myprojects/WorkServer/frontend:/media/frontend
|
|
||||||
working_dir: /media/backend
|
|
||||||
command: >
|
|
||||||
sh -c "pip install -r requirements.txt && python server.py"
|
|
||||||
cpus: '0.35'
|
|
||||||
mem_limit: 256M
|
|
@ -1,3 +0,0 @@
|
|||||||
FROM nginx:v1
|
|
||||||
|
|
||||||
COPY nginx.conf /etc/nginx/nginx.conf
|
|
36
frontend/index.html
Normal file
36
frontend/index.html
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Login and Register Form</title>
|
||||||
|
<link rel="stylesheet" href="./static/css/styles.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="form-container">
|
||||||
|
<h1>Login Form</h1>
|
||||||
|
<form action="/submit" method="post">
|
||||||
|
<label for="username">Username:</label><br>
|
||||||
|
<input type="text" id="username" name="username" required><br><br>
|
||||||
|
|
||||||
|
<label for="password">Password:</label><br>
|
||||||
|
<input type="password" id="password" name="password" required><br><br>
|
||||||
|
|
||||||
|
<button type="submit" name="action" value="Login">Login</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-container">
|
||||||
|
<h1>Register Form</h1>
|
||||||
|
<form action="/submit" method="post">
|
||||||
|
<label for="new_username">Username:</label><br>
|
||||||
|
<input type="text" id="new_username" name="new_username" required><br><br>
|
||||||
|
|
||||||
|
<label for="new_password">Password:</label><br>
|
||||||
|
<input type="password" id="new_password" name="new_password" required><br><br>
|
||||||
|
|
||||||
|
<button type="submit" name="action" value="Register">Register</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
68
frontend/static/css/styles.css
Normal file
68
frontend/static/css/styles.css
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/* Общие стили */
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
background-color: #f4f4f4;
|
||||||
|
margin: 0;
|
||||||
|
padding: 20px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column; /* Располагаем контейнеры вертикально */
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 100vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Контейнер для форм */
|
||||||
|
.form-container { /* Изменили селектор на класс .form-container */
|
||||||
|
width: 100%;
|
||||||
|
max-width: 400px;
|
||||||
|
padding: 20px;
|
||||||
|
background-color: #fff;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Заголовки */
|
||||||
|
h1 {
|
||||||
|
text-align: center;
|
||||||
|
color: #333;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Метки (labels) */
|
||||||
|
label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #555;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Поля ввода (input) */
|
||||||
|
input[type="text"],
|
||||||
|
input[type="password"] {
|
||||||
|
width: 100%;
|
||||||
|
padding: 10px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 4px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Кнопки */
|
||||||
|
button {
|
||||||
|
background-color: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
padding: 12px 20px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
width: 100%;
|
||||||
|
font-size: 16px;
|
||||||
|
transition: background-color 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
background-color: #45a049;
|
||||||
|
}
|
@ -1,32 +1,32 @@
|
|||||||
|
|
||||||
user nginx;
|
user nginx;
|
||||||
worker_processes auto;
|
worker_processes auto;
|
||||||
|
|
||||||
error_log /var/log/nginx/error.log notice;
|
error_log /var/log/nginx/error.log notice;
|
||||||
pid /var/run/nginx.pid;
|
pid /var/run/nginx.pid;
|
||||||
|
|
||||||
|
|
||||||
events {
|
events {
|
||||||
worker_connections 1024;
|
worker_connections 1024;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
http {
|
http {
|
||||||
include /etc/nginx/mime.types;
|
include /etc/nginx/mime.types;
|
||||||
default_type application/octet-stream;
|
default_type application/octet-stream;
|
||||||
|
|
||||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||||
'$status $body_bytes_sent "$http_referer" '
|
'$status $body_bytes_sent "$http_referer" '
|
||||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||||
|
|
||||||
access_log /var/log/nginx/access.log main;
|
access_log /var/log/nginx/access.log main;
|
||||||
|
|
||||||
sendfile on;
|
sendfile on;
|
||||||
#tcp_nopush on;
|
#tcp_nopush on;
|
||||||
|
|
||||||
keepalive_timeout 65;
|
keepalive_timeout 65;
|
||||||
|
|
||||||
#gzip on;
|
#gzip on;
|
||||||
|
|
||||||
include /etc/nginx/sites-available/domain.conf;
|
include /etc/nginx/sites-available/domain.conf;
|
||||||
}
|
}
|
@ -1,44 +1,40 @@
|
|||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
server_name your_domain.com;
|
server_name your_doman.com www.your_doman.com;
|
||||||
return 301 https://your_domain.com$request_uri;
|
return 301 https://your_doman.com$request_uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen 443 ssl;
|
listen 443 ssl;
|
||||||
server_name your_domain.com;
|
server_name your_doman.com www.your_doman.com;
|
||||||
|
|
||||||
|
|
||||||
ssl_certificate /etc/nginx/sites-available/domain.crt;
|
ssl_certificate /etc/nginx/sites-available/domain.crt;
|
||||||
ssl_certificate_key /etc/nginx/sites-available/domain.key;
|
ssl_certificate_key /etc/nginx/sites-available/domain.key;
|
||||||
|
|
||||||
|
|
||||||
ssl_protocols TLSv1.2 TLSv1.3;
|
ssl_protocols TLSv1.2 TLSv1.3;
|
||||||
ssl_ciphers HIGH:!aNULL:!MD5;
|
ssl_ciphers HIGH:!aNULL:!MD5;
|
||||||
ssl_prefer_server_ciphers on;
|
ssl_prefer_server_ciphers on;
|
||||||
|
|
||||||
|
|
||||||
root /usr/share/nginx/html;
|
root /usr/share/nginx/html;
|
||||||
|
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
location /submit {
|
location /submit {
|
||||||
proxy_pass http://backend_part:5000;
|
proxy_pass http://backend_part:5000;
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
}
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
location /api {
|
}
|
||||||
proxy_pass http://backend_part:5000/;
|
|
||||||
proxy_set_header Host $host;
|
}
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
1
nginx/ssl/domain.crt
Normal file
1
nginx/ssl/domain.crt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Наш публичный ключ
|
1
nginx/ssl/domain.key
Normal file
1
nginx/ssl/domain.key
Normal file
@ -0,0 +1 @@
|
|||||||
|
Наш приватный ключ
|
@ -1,21 +1,21 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
TARGET="/home/git/myprojects/WorkServer"
|
TARGET="path to work repo"
|
||||||
GIT_DIR="/home/git/myprojects/web"
|
GIT_DIR="Path to .git repo"
|
||||||
BRANCH="master"
|
BRANCH="master"
|
||||||
|
|
||||||
git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f "$BRANCH"
|
git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f "$BRANCH"
|
||||||
|
|
||||||
cd /home/git/myprojects/WorkServer/docker
|
cd $TARGET
|
||||||
|
|
||||||
#docker-compose block:
|
#docker-compose block:
|
||||||
|
|
||||||
# 1
|
# 1
|
||||||
docker-compose build
|
docker-compose build
|
||||||
|
|
||||||
# 2
|
# 2
|
||||||
docker-compose down
|
docker-compose down
|
||||||
|
|
||||||
# 3
|
# 3
|
||||||
docker-compose up -d
|
docker-compose up -d
|
||||||
|
|
11
scripts/pythonprojects.code-workspace
Normal file
11
scripts/pythonprojects.code-workspace
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"path": "../../../../../PythonProjects/pythonprojects"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": ".."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"settings": {}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user