Set up database creation on start.
This commit is contained in:
34
src/main.py
34
src/main.py
@@ -1,34 +1,26 @@
|
||||
from flask import Flask, g, jsonify, request
|
||||
import sqlite3
|
||||
import container_managment
|
||||
from database import SmartCursor
|
||||
from database import SmartCursor, get_db, init_db
|
||||
import tables
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
|
||||
def convertTable(tupple):
|
||||
return {
|
||||
"id": tupple[0],
|
||||
"table_name": tupple[1],
|
||||
"table_link": tupple[2],
|
||||
"active": tupple[3] != 0,
|
||||
}
|
||||
@app.teardown_appcontext
|
||||
def close_connection(exception):
|
||||
db = getattr(g, "_database", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
def get_db():
|
||||
db = getattr(g, "_database", None)
|
||||
if db is None:
|
||||
db = sqlite3.connect("/data/tables.db")
|
||||
return db
|
||||
app.register_blueprint(tables.tables, url_prefix="/api")
|
||||
|
||||
with app.app_context():
|
||||
init_db(get_db())
|
||||
|
||||
@app.teardown_appcontext
|
||||
def close_connection(exception):
|
||||
db = getattr(g, "_database", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
return app
|
||||
|
||||
app.register_blueprint(tables.tables, url_prefix="/api")
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
create_app.run(debug=True)
|
||||
|
||||
Reference in New Issue
Block a user