Initial commit
This commit is contained in:
375
app/app.py
Normal file
375
app/app.py
Normal file
@@ -0,0 +1,375 @@
|
||||
from flask import Flask, render_template_string, request, redirect, url_for
|
||||
import psycopg2
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Подключение к PostgreSQL
|
||||
def get_db_connection():
|
||||
return psycopg2.connect(
|
||||
host=os.environ.get('DB_HOST', 'postgres'),
|
||||
database=os.environ.get('DB_NAME', 'blog'),
|
||||
user=os.environ.get('DB_USER', 'blog_user'),
|
||||
password=os.environ.get('DB_PASSWORD', 'blog_password')
|
||||
)
|
||||
|
||||
# Современный HTML шаблон
|
||||
INDEX_HTML = '''
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Blog | Dedov Egor BSMO-32-25</title>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:root {
|
||||
--bg-primary: #0d1117;
|
||||
--bg-secondary: #161b22;
|
||||
--bg-tertiary: #21262d;
|
||||
--border-color: #30363d;
|
||||
--text-primary: #c9d1d9;
|
||||
--text-secondary: #8b949e;
|
||||
--accent: #58a6ff;
|
||||
--accent-hover: #1f6feb;
|
||||
--success: #3fb950;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif;
|
||||
background: var(--bg-primary);
|
||||
color: var(--text-primary);
|
||||
line-height: 1.6;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
padding: 40px 20px;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-bottom: 40px;
|
||||
padding-bottom: 20px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 32px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 8px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 4px 10px;
|
||||
background: var(--bg-tertiary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 20px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.info-bar {
|
||||
display: flex;
|
||||
gap: 20px;
|
||||
margin-top: 12px;
|
||||
font-size: 14px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.info-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
margin: 40px 0 20px 0;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.posts-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.post {
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 6px;
|
||||
padding: 20px;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
.post:hover {
|
||||
border-color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.post-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.post h3 {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.post-id {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
font-family: 'SF Mono', Monaco, Consolas, monospace;
|
||||
}
|
||||
|
||||
.post-content {
|
||||
color: var(--text-secondary);
|
||||
font-size: 14px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 60px 20px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.empty-state-icon {
|
||||
font-size: 48px;
|
||||
margin-bottom: 16px;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.form-container {
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 6px;
|
||||
padding: 24px;
|
||||
margin-top: 32px;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
margin-bottom: 8px;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
input, textarea {
|
||||
width: 100%;
|
||||
padding: 10px 12px;
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 6px;
|
||||
color: var(--text-primary);
|
||||
font-size: 14px;
|
||||
font-family: inherit;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
input:focus, textarea:focus {
|
||||
outline: none;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: vertical;
|
||||
min-height: 120px;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
button {
|
||||
background: var(--success);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #2ea043;
|
||||
}
|
||||
|
||||
button:active {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
.btn-delete {
|
||||
background: #da3633;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 6px 12px;
|
||||
border-radius: 6px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
width: auto;
|
||||
}
|
||||
|
||||
.btn-delete:hover {
|
||||
background: #b62324;
|
||||
}
|
||||
|
||||
.btn-delete:active {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
footer {
|
||||
margin-top: 60px;
|
||||
padding-top: 20px;
|
||||
border-top: 1px solid var(--border-color);
|
||||
text-align: center;
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.container {
|
||||
padding: 20px 16px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
.info-bar {
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<header>
|
||||
<h1>
|
||||
Blog System
|
||||
<span class="badge">v1.0</span>
|
||||
</h1>
|
||||
<div class="info-bar">
|
||||
<div class="info-item">
|
||||
<span>Author:</span>
|
||||
<span>Dedov Egor</span>
|
||||
</div>
|
||||
<div class="info-item">
|
||||
<span>Group:</span>
|
||||
<span>BSMO-32-25</span>
|
||||
</div>
|
||||
<div class="info-item">
|
||||
<span>Time:</span>
|
||||
<span>{{ current_time.strftime('%Y-%m-%d %H:%M:%S') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<h2 class="section-title">Blog Posts</h2>
|
||||
|
||||
<div class="posts-container">
|
||||
{% if posts %}
|
||||
{% for post in posts %}
|
||||
<div class="post">
|
||||
<div class="post-header">
|
||||
<h3>{{ post[1] }}</h3>
|
||||
<div style="display: flex; align-items: center; gap: 12px;">
|
||||
<span class="post-id">#{{ post[0] }}</span>
|
||||
<form method="POST" action="/delete/{{ post[0] }}" style="margin: 0;">
|
||||
<button type="submit" class="btn-delete" onclick="return confirm('Delete this post?')">Delete</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="post-content">
|
||||
{{ post[2] }}
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<div class="empty-state">
|
||||
<div class="empty-state-icon">-</div>
|
||||
<p>No posts yet. Create your first post below.</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="form-container">
|
||||
<h2 class="section-title" style="margin-top: 0;">New Post</h2>
|
||||
<form method="POST">
|
||||
<div class="form-group">
|
||||
<label for="title">Title</label>
|
||||
<input type="text" id="title" name="title" placeholder="Enter post title..." required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="content">Content</label>
|
||||
<textarea id="content" name="content" placeholder="Write post content..." required></textarea>
|
||||
</div>
|
||||
<button type="submit">Publish</button>
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<p>Virtualization and Containerization Project · Dedov Egor · BSMO-32-25</p>
|
||||
</footer>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
if request.method == 'POST':
|
||||
title = request.form['title']
|
||||
content = request.form['content']
|
||||
cur.execute('INSERT INTO posts (title, content) VALUES (%s, %s)', (title, content))
|
||||
conn.commit()
|
||||
|
||||
cur.execute('SELECT id, title, content FROM posts ORDER BY id DESC')
|
||||
posts = cur.fetchall()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
return render_template_string(INDEX_HTML, posts=posts, current_time=datetime.now())
|
||||
|
||||
@app.route('/delete/<int:post_id>', methods=['POST'])
|
||||
def delete_post(post_id):
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute('DELETE FROM posts WHERE id = %s', (post_id,))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
conn.close()
|
||||
return redirect(url_for('index'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000, debug=True)
|
||||
7
app/init.sql
Normal file
7
app/init.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
-- Создаём таблицу для постов блога
|
||||
CREATE TABLE IF NOT EXISTS posts (
|
||||
id SERIAL PRIMARY KEY,
|
||||
title VARCHAR(200) NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
2
app/requirements.txt
Normal file
2
app/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Flask==3.0.0
|
||||
psycopg2-binary==2.9.9
|
||||
Reference in New Issue
Block a user