Skip to content

Commit

Permalink
db_uri and code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
JibrilExe committed Feb 21, 2024
1 parent bf8d988 commit 3e95b1f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions backend/environment.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB_HOST=sqlite:///path/to/your/database.db
16 changes: 10 additions & 6 deletions backend/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@
This file is the base of the Flask API. It contains the basic structure of the API.
"""

from os import getenv
from dotenv import load_dotenv
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from .endpoints.index import index_bp

db = SQLAlchemy()

def create_app():
"""
Create a Flask application instance.
Returns:
Flask -- A Flask application instance
"""
db = SQLAlchemy()
load_dotenv()

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = getenv('DB_HOST')
app.register_blueprint(index_bp)
db.init_app(app)

return app

def init_db(db_uri):
"""Initialize the database with the given uri"""
app = create_app()
app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
db.init_app(app)
return app
7 changes: 5 additions & 2 deletions backend/project/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Main entry point for the application."""
from sys import path
from project import create_app
from os import getenv
from project import init_db
from dotenv import load_dotenv

path.append(".")

if __name__ == "__main__":
app = create_app()
load_dotenv(dotenv_path='environment.env')
app = init_db(getenv("DB_HOST"))
app.run(debug=True)

0 comments on commit 3e95b1f

Please sign in to comment.