feat: PostgreSQL подключение, переменные в .env.example

This commit is contained in:
2026-06-13 11:57:31 +04:00
parent 9b6f5b9c5e
commit 684b4cc9c0
4 changed files with 34 additions and 1 deletions
+8
View File
@@ -1,2 +1,10 @@
FLASK_ENV=production
PORT=5000
# ── База данных PostgreSQL ──
DB_HOST=postgresqlk8s-master.xxx.svc.cluster.local
DB_PORT=5432
DB_NAME=contracts
DB_USER=contracts
DB_PASS=
DB_SSLMODE=disable
+2
View File
@@ -2,3 +2,5 @@ flask
gunicorn
python-docx
requests
psycopg2-binary
python-dotenv
+23 -1
View File
@@ -1,16 +1,38 @@
import os
import psycopg2
from dotenv import load_dotenv
from flask import Flask, render_template
load_dotenv()
class ContractsApp:
def __init__(self):
self.app = Flask(__name__)
self.db = self._connect_db()
self.add_routes()
def _connect_db(self):
try:
conn = psycopg2.connect(
host=os.getenv("DB_HOST"),
port=os.getenv("DB_PORT", 5432),
dbname=os.getenv("DB_NAME"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASS"),
sslmode=os.getenv("DB_SSLMODE", "disable"),
)
return conn
except Exception as e:
print(f"DB connect error: {e}")
return None
def add_routes(self):
self.app.add_url_rule("/", "index", self.index)
self.app.add_url_rule("/health", "health", self.health)
def index(self):
return render_template("index.html")
db_status = "connected" if self.db else "no DB"
return render_template("index.html", db_status=db_status)
def health(self):
return "OK", 200, {"Content-Type": "text/plain"}
+1
View File
@@ -9,5 +9,6 @@
<body>
<h1>Contracts App 🚀</h1>
<p>Сервис работает.</p>
<p>База данных: <strong>{{ db_status }}</strong></p>
</body>
</html>