sk1/app/app.py
2026-05-19 22:47:34 +00:00

414 lines
8.1 KiB
Python

from flask import Flask, request, redirect, url_for, render_template_string
import os
import psycopg2
app = Flask(__name__)
DB_HOST = os.getenv("DB_HOST")
DB_NAME = os.getenv("DB_NAME")
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")
def get_db():
conn = psycopg2.connect(
host=DB_HOST,
database=DB_NAME,
user=DB_USER,
password=DB_PASSWORD
)
return conn
def init_db():
conn = get_db()
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS items (
id SERIAL PRIMARY KEY,
text TEXT NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
cur.close()
conn.close()
init_db()
TEMPLATE = """
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cloud Notes Platform</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Inter', sans-serif;
background: linear-gradient(135deg, #0f172a, #1e293b);
color: #e2e8f0;
min-height: 100vh;
padding: 40px 20px;
}
.container {
max-width: 1200px;
margin: auto;
}
.hero {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 20px;
margin-bottom: 35px;
}
.hero h1 {
font-size: 3rem;
font-weight: 700;
color: white;
margin-bottom: 12px;
}
.hero p {
color: #cbd5e1;
max-width: 700px;
line-height: 1.7;
font-size: 1rem;
}
.status-badge {
background: rgba(16, 185, 129, 0.15);
border: 1px solid rgba(16, 185, 129, 0.4);
color: #34d399;
padding: 12px 20px;
border-radius: 999px;
font-size: 0.95rem;
font-weight: 600;
height: fit-content;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 25px;
}
.card {
background: rgba(15, 23, 42, 0.75);
border: 1px solid rgba(255,255,255,0.08);
backdrop-filter: blur(10px);
border-radius: 22px;
padding: 30px;
box-shadow: 0 12px 30px rgba(0,0,0,0.3);
}
.card h2 {
color: white;
margin-bottom: 20px;
font-size: 1.4rem;
}
textarea {
width: 100%;
min-height: 150px;
border-radius: 16px;
border: 1px solid rgba(255,255,255,0.08);
background: #0f172a;
color: white;
padding: 16px;
font-size: 1rem;
resize: vertical;
outline: none;
transition: 0.2s ease;
}
textarea:focus {
border-color: #3b82f6;
box-shadow: 0 0 0 4px rgba(59,130,246,0.2);
}
button {
margin-top: 18px;
width: 100%;
padding: 15px;
border: none;
border-radius: 14px;
background: linear-gradient(135deg, #2563eb, #3b82f6);
color: white;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s ease, opacity 0.2s ease;
}
button:hover {
transform: translateY(-2px);
opacity: 0.95;
}
.notes-container {
max-height: 550px;
overflow-y: auto;
padding-right: 5px;
}
.note {
background: rgba(255,255,255,0.04);
border: 1px solid rgba(255,255,255,0.06);
border-radius: 16px;
padding: 18px;
margin-bottom: 18px;
transition: 0.2s ease;
}
.note:hover {
transform: translateY(-2px);
border-color: rgba(59,130,246,0.35);
}
.note-header {
display: flex;
justify-content: space-between;
margin-bottom: 12px;
color: #94a3b8;
font-size: 0.9rem;
}
.note-text {
color: #f8fafc;
line-height: 1.7;
word-wrap: break-word;
}
.stats {
display: flex;
gap: 18px;
margin-top: 25px;
flex-wrap: wrap;
}
.stat-box {
flex: 1;
min-width: 180px;
background: rgba(255,255,255,0.04);
border: 1px solid rgba(255,255,255,0.05);
border-radius: 16px;
padding: 18px;
}
.stat-box h3 {
color: #94a3b8;
font-size: 0.95rem;
margin-bottom: 10px;
}
.stat-box p {
font-size: 1.7rem;
font-weight: 700;
color: white;
}
.footer {
text-align: center;
margin-top: 40px;
color: #94a3b8;
font-size: 0.95rem;
}
@media (max-width: 900px) {
.grid {
grid-template-columns: 1fr;
}
.hero h1 {
font-size: 2.2rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="hero">
<div>
<h1>Cloud Notes Platform</h1>
<p>
A modern cloud-native Flask application deployed with Kubernetes,
Docker containers, persistent storage, and automated deployment workflows.
This platform demonstrates scalable cloud infrastructure concepts and
production-ready deployment architecture.
</p>
</div>
<div class="status-badge">
Running Mode: {{ mode }}
</div>
</div>
<div class="grid">
<div class="card">
<h2>Create New Note</h2>
<form method="post">
<textarea
id="text"
name="text"
placeholder="Write your note here..."
required
></textarea>
<button type="submit">
Save Note
</button>
</form>
<div class="stats">
<div class="stat-box">
<h3>Total Notes</h3>
<p>{{ items|length }}</p>
</div>
<div class="stat-box">
<h3>Infrastructure</h3>
<p>K8s</p>
</div>
</div>
</div>
<div class="card">
<h2>Stored Notes</h2>
<div class="notes-container">
{% for item in items %}
<div class="note">
<div class="note-header">
<span>Note #{{ item.id }}</span>
<span>{{ item.created }}</span>
</div>
<div class="note-text">
{{ item.text }}
</div>
</div>
{% else %}
<div class="note">
<div class="note-text">
No notes stored yet.
</div>
</div>
{% endfor %}
</div>
</div>
</div>
<div class="footer">
Cloud Technologies Project • Flask • Docker • Kubernetes • Persistent Storage
</div>
</div>
</body>
</html>
"""
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
text = request.form.get("text", "").strip()
if text:
conn = get_db()
cur = conn.cursor()
cur.execute(
"INSERT INTO items (text) VALUES (%s)",
(text,)
)
conn.commit()
cur.close()
conn.close()
return redirect(url_for("index"))
conn = get_db()
cur = conn.cursor()
cur.execute(
"SELECT id, text, created FROM items ORDER BY id DESC"
)
rows = cur.fetchall()
items = []
for row in rows:
items.append({
"id": row[0],
"text": row[1],
"created": row[2]
})
cur.close()
conn.close()
mode = "Docker Compose + PostgreSQL"
return render_template_string(
TEMPLATE,
items=items,
mode=mode
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)