feat: PostgreSQL подключение, переменные в .env.example
This commit is contained in:
+23
-1
@@ -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"}
|
||||
|
||||
Reference in New Issue
Block a user