Add folders
This commit is contained in:
parent
51f7582175
commit
9051fa1c2b
18
app/Dockerfile
Normal file
18
app/Dockerfile
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Basic image from Docker Hub
|
||||||
|
FROM python:3.9-slim
|
||||||
|
|
||||||
|
# Container working directory settings
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy the requirements file and install Python
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
|
# Copy application files to the image
|
||||||
|
COPY app.py .
|
||||||
|
|
||||||
|
# Expose port 5000 for the Flask application
|
||||||
|
EXPOSE 5000
|
||||||
|
|
||||||
|
# Program to run
|
||||||
|
CMD ["python", "app.py"]
|
100
app/app.py
Normal file
100
app/app.py
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
from flask import Flask, request, jsonify
|
||||||
|
import os
|
||||||
|
import psycopg2
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Database Configuration
|
||||||
|
db_host = os.environ.get("DB_HOST", "postgres_db")
|
||||||
|
db_name = os.environ.get("POSTGRES_DB", "sampledb")
|
||||||
|
db_user = os.environ.get("POSTGRES_USER", "user")
|
||||||
|
db_password = os.environ.get("POSTGRES_PASSWORD", "password")
|
||||||
|
|
||||||
|
def get_db_connection():
|
||||||
|
return psycopg2.connect(host=db_host, database=db_name, user=db_user, password=db_password)
|
||||||
|
|
||||||
|
def create_table():
|
||||||
|
conn = get_db_connection()
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute('''
|
||||||
|
CREATE TABLE IF NOT EXISTS articles (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
content TEXT NOT NULL
|
||||||
|
)
|
||||||
|
''')
|
||||||
|
conn.commit()
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
@app.route("/", methods=["GET", "POST"])
|
||||||
|
def index():
|
||||||
|
conn = get_db_connection()
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
if request.method == "POST":
|
||||||
|
title = request.form.get("title")
|
||||||
|
content = request.form.get("content")
|
||||||
|
if title and content:
|
||||||
|
cur.execute("INSERT INTO articles (title, content) VALUES (%s, %s)", (title, content))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
cur.execute("SELECT * FROM articles ORDER BY id DESC")
|
||||||
|
articles = cur.fetchall()
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return f"""
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>My Blog</title>
|
||||||
|
<style>
|
||||||
|
body {{ font-family: 'Poppins', sans-serif; background-color: #f5f5f5; color: #333; margin: 0; padding: 0; }}
|
||||||
|
.container {{ max-width: 1100px; margin: 50px auto; background: #6a0dad; padding: 30px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); display: flex; gap: 20px; }}
|
||||||
|
.form-container, .articles-container {{ flex: 1; background: #ffffff; padding: 20px; border-radius: 10px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); }}
|
||||||
|
h1{{ text-align: center; color: #6a0dad; font-size: 30px; }}
|
||||||
|
form {{ display: flex; flex-direction: column; gap: 10px; align-items: center; }}
|
||||||
|
input, textarea {{ width: 90%; padding: 14px; border: 1px solid #ccc; border-radius: 5px; background: #fff; color: #333; font-size: 18px; }}
|
||||||
|
button {{ background-color: #6a0dad; color: white; border: none; padding: 12px 25px; cursor: pointer; border-radius: 5px; transition: background 0.3s; font-size: 20px; }}
|
||||||
|
button:hover {{ background-color: #5a0dad; }}
|
||||||
|
.articles-container {{ display: flex; flex-direction: column; gap: 15px; }}
|
||||||
|
.article {{ background: #e0e0e0; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); position: relative; }}
|
||||||
|
.article h3 {{ margin: 0; color: #333; font-size: 22px; }}
|
||||||
|
.article p {{ margin: 10px 0; color: #555; font-size: 18px; }}
|
||||||
|
.delete-form {{ position: absolute; top: 10px; right: 10px; }}
|
||||||
|
.delete-button {{ background-color: #ff5555; padding: 8px 12px; font-size: 14px; border-radius: 5px; }}
|
||||||
|
.delete-button:hover {{ background-color: #cc4444; }}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="form-container">
|
||||||
|
<h1>My Blog</h1>
|
||||||
|
<form method="post">
|
||||||
|
<input type="text" name="title" placeholder="Title" required>
|
||||||
|
<textarea name="content" placeholder="Content" required></textarea>
|
||||||
|
<button type="submit">Add Article</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="articles-container">
|
||||||
|
<h1>Articles</h1>
|
||||||
|
{''.join(f'<div class="article"><h3>{a[1]}</h3><p>{a[2]}</p><form method="post" action="/delete/{a[0]}" class="delete-form"><button type="submit" class="delete-button">Delete</button></form></div>' for a in articles)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
@app.route("/delete/<int:article_id>", methods=["POST"])
|
||||||
|
def delete_article(article_id):
|
||||||
|
conn = get_db_connection()
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute("DELETE FROM articles WHERE id = %s", (article_id,))
|
||||||
|
conn.commit()
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
return "<script>window.location.href='/'</script>"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
create_table()
|
||||||
|
app.run(host="0.0.0.0", port=5000)
|
3
app/requirements.txt
Normal file
3
app/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Flask
|
||||||
|
psycopg2-binary
|
||||||
|
requests
|
Loading…
Reference in New Issue
Block a user