Skip to content
Open
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
10 changes: 10 additions & 0 deletions ZaharenkoAA/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Веб-приложение, которое генерирует пароль по заданным параметрам.
Реализовано на основе микросервисной архитектуры с использованием Docker Compose.

nginx - Единая точка входа
frontend - Пользовательский интерфейс
backend - REST API для приёма заказов на пароли
worker - Асинхронный обработчик длительных задач
redis - Брокер сообщений и хранилище результатов

запуск командой: docker compose up --build
6 changes: 6 additions & 0 deletions ZaharenkoAA/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]
42 changes: 42 additions & 0 deletions ZaharenkoAA/backend/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import string
import random
import time
from flask import Flask, request, jsonify
from celery import Celery

app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://redis:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://redis:6379/0'

celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

@celery.task(bind=True)
def generate_password_task(self, length=12, use_digits=True, use_special=True):
chars = string.ascii_letters
if use_digits:
chars += string.digits
if use_special:
chars += string.punctuation
password = ''.join(random.choice(chars) for _ in range(length))
return password

@app.route('/api/generate', methods=['POST'])
def generate():
data = request.get_json() or {}
length = int(data.get('length', 12))
use_digits = data.get('use_digits', True)
use_special = data.get('use_special', True)
task = generate_password_task.delay(length, use_digits, use_special)
return jsonify({'task_id': task.id}), 202

@app.route('/api/status/<task_id>')
def task_status(task_id):
task = generate_password_task.AsyncResult(task_id)
if task.state == 'PENDING':
response = {'state': task.state, 'status': 'Задача ожидает выполнения...'}
elif task.state == 'SUCCESS':
response = {'state': task.state, 'result': task.result}
else:
response = {'state': task.state, 'status': str(task.info)}
return jsonify(response)
3 changes: 3 additions & 0 deletions ZaharenkoAA/backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flask
gunicorn
celery[redis]
49 changes: 49 additions & 0 deletions ZaharenkoAA/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: '3.8'

services:
redis:
image: redis:alpine
networks:
- app-net

backend:
build: ./backend
networks:
- app-net
depends_on:
- redis
environment:
- CELERY_BROKER_URL=redis://redis:6379/0
- CELERY_RESULT_BACKEND=redis://redis:6379/0

worker:
build: ./backend
command: celery -A app.celery worker --loglevel=info
networks:
- app-net
depends_on:
- redis
environment:
- CELERY_BROKER_URL=redis://redis:6379/0
- CELERY_RESULT_BACKEND=redis://redis:6379/0

frontend:
build: ./frontend
networks:
- app-net

nginx:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
networks:
- app-net
depends_on:
- frontend
- backend

networks:
app-net:
driver: bridge
4 changes: 4 additions & 0 deletions ZaharenkoAA/frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM python:3.10-alpine
WORKDIR /app
COPY static/ static/
CMD ["python", "-m", "http.server", "3000", "--directory", "static"]
33 changes: 33 additions & 0 deletions ZaharenkoAA/frontend/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Генератор паролей</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="container mt-5">
<h1>Генератор паролей</h1>
<div class="card p-4">
<div class="mb-3">
<label for="length" class="form-label">Длина пароля</label>
<input type="number" id="length" class="form-control" value="16" min="4" max="64">
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="useDigits" checked>
<label class="form-check-label" for="useDigits">Использовать цифры (0-9)</label>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" id="useSpecial" checked>
<label class="form-check-label" for="useSpecial">Спецсимволы (!@#$...)</label>
</div>
<button id="generateBtn" class="btn btn-primary">Сгенерировать</button>
<div id="status" class="mt-3 alert alert-info" style="display: none;"></div>
<div id="result" class="mt-3 alert alert-success" style="display: none;">
<strong>Ваш пароль:</strong> <span id="passwordValue"></span>
</div>
<div id="error" class="mt-3 alert alert-danger" style="display: none;"></div>
</div>
<script src="script.js"></script>
</body>
</html>
45 changes: 45 additions & 0 deletions ZaharenkoAA/frontend/static/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
document.getElementById('generateBtn').addEventListener('click', async () => {
const length = document.getElementById('length').value;
const useDigits = document.getElementById('useDigits').checked;
const useSpecial = document.getElementById('useSpecial').checked;

const statusDiv = document.getElementById('status');
const resultDiv = document.getElementById('result');
const errorDiv = document.getElementById('error');
statusDiv.style.display = 'block';
statusDiv.textContent = '';
resultDiv.style.display = 'none';
errorDiv.style.display = 'none';

try {
const response = await fetch('/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ length, use_digits: useDigits, use_special: useSpecial })
});
const { task_id } = await response.json();

const poll = setInterval(async () => {
const statusResp = await fetch(`/api/status/${task_id}`);
const data = await statusResp.json();

if (data.state === 'SUCCESS') {
clearInterval(poll);
statusDiv.style.display = 'none';
resultDiv.style.display = 'block';
document.getElementById('passwordValue').textContent = data.result;
} else if (data.state === 'FAILURE') {
clearInterval(poll);
statusDiv.style.display = 'none';
errorDiv.style.display = 'block';
errorDiv.textContent = `Ошибка: ${data.status}`;
} else {
statusDiv.textContent = `Статус: ${data.status || data.state}`;
}
}, 1000);
} catch (err) {
statusDiv.style.display = 'none';
errorDiv.style.display = 'block';
errorDiv.textContent = 'Ошибка соединения с сервером';
}
});
16 changes: 16 additions & 0 deletions ZaharenkoAA/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
server {
listen 80;
server_name localhost;

location / {
proxy_pass http://frontend:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

location /api/ {
proxy_pass http://backend:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
6 changes: 6 additions & 0 deletions ZaharenkoAA/worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]
42 changes: 42 additions & 0 deletions ZaharenkoAA/worker/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import string
import random
import time
from flask import Flask, request, jsonify
from celery import Celery

app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://redis:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://redis:6379/0'

celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

@celery.task(bind=True)
def generate_password_task(self, length=12, use_digits=True, use_special=True):
chars = string.ascii_letters
if use_digits:
chars += string.digits
if use_special:
chars += string.punctuation
password = ''.join(random.choice(chars) for _ in range(length))
return password

@app.route('/api/generate', methods=['POST'])
def generate():
data = request.get_json() or {}
length = int(data.get('length', 12))
use_digits = data.get('use_digits', True)
use_special = data.get('use_special', True)
task = generate_password_task.delay(length, use_digits, use_special)
return jsonify({'task_id': task.id}), 202

@app.route('/api/status/<task_id>')
def task_status(task_id):
task = generate_password_task.AsyncResult(task_id)
if task.state == 'PENDING':
response = {'state': task.state, 'status': 'Задача ожидает выполнения'}
elif task.state == 'SUCCESS':
response = {'state': task.state, 'result': task.result}
else:
response = {'state': task.state, 'status': str(task.info)}
return jsonify(response)