feat: PostgreSQL подключение, переменные в .env.example
This commit is contained in:
@@ -1,2 +1,10 @@
|
|||||||
FLASK_ENV=production
|
FLASK_ENV=production
|
||||||
PORT=5000
|
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,3 +2,5 @@ flask
|
|||||||
gunicorn
|
gunicorn
|
||||||
python-docx
|
python-docx
|
||||||
requests
|
requests
|
||||||
|
psycopg2-binary
|
||||||
|
python-dotenv
|
||||||
|
|||||||
+23
-1
@@ -1,16 +1,38 @@
|
|||||||
|
import os
|
||||||
|
import psycopg2
|
||||||
|
from dotenv import load_dotenv
|
||||||
from flask import Flask, render_template
|
from flask import Flask, render_template
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
class ContractsApp:
|
class ContractsApp:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.app = Flask(__name__)
|
self.app = Flask(__name__)
|
||||||
|
self.db = self._connect_db()
|
||||||
self.add_routes()
|
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):
|
def add_routes(self):
|
||||||
self.app.add_url_rule("/", "index", self.index)
|
self.app.add_url_rule("/", "index", self.index)
|
||||||
self.app.add_url_rule("/health", "health", self.health)
|
self.app.add_url_rule("/health", "health", self.health)
|
||||||
|
|
||||||
def index(self):
|
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):
|
def health(self):
|
||||||
return "OK", 200, {"Content-Type": "text/plain"}
|
return "OK", 200, {"Content-Type": "text/plain"}
|
||||||
|
|||||||
@@ -9,5 +9,6 @@
|
|||||||
<body>
|
<body>
|
||||||
<h1>Contracts App 🚀</h1>
|
<h1>Contracts App 🚀</h1>
|
||||||
<p>Сервис работает.</p>
|
<p>Сервис работает.</p>
|
||||||
|
<p>База данных: <strong>{{ db_status }}</strong></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user