Refactor application configuration; implement dynamic database URI setup based on environment variables for improved flexibility and maintainability
This commit is contained in:
parent
86a4e4d22f
commit
e67ae63eb8
10 changed files with 88 additions and 29 deletions
29
__init__.py
29
__init__.py
|
@ -1,37 +1,28 @@
|
|||
from flask import Flask
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from urllib.parse import quote_plus
|
||||
import logging
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
logger = logging.getLogger('sqlalchemy.engine')
|
||||
logger.setLevel(logging.INFO)
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s'))
|
||||
logger.addHandler(handler)
|
||||
if not logger.handlers:
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s'))
|
||||
logger.addHandler(handler)
|
||||
|
||||
def create_app():
|
||||
from config import Config
|
||||
app = Flask(__name__)
|
||||
|
||||
params = quote_plus(
|
||||
"DRIVER=ODBC Driver 17 for SQL Server;"
|
||||
"SERVER=NDVASQLCR01;"
|
||||
"DATABASE=conradTest;"
|
||||
"Trusted_Connection=yes;"
|
||||
)
|
||||
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = f"mssql+pyodbc:///?odbc_connect={params}"
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
app.config.from_object(Config)
|
||||
|
||||
db.init_app(app)
|
||||
|
||||
with app.app_context():
|
||||
from . import models
|
||||
db.create_all()
|
||||
|
||||
from .routes import main
|
||||
app.register_blueprint(main)
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return "Hello, you've reached the terrible but functional root of the site."
|
||||
|
||||
|
||||
return app
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue