diff --git a/src/lib/__init__.py b/src/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/lib/startup.py b/src/lib/startup.py new file mode 100644 index 0000000..5a4433c --- /dev/null +++ b/src/lib/startup.py @@ -0,0 +1,34 @@ +from pathlib import Path +import duckdb +import bcrypt + +from settings import settings + + +def generate_db() -> None: + settings.database_file.parent.mkdir(parents=True, exist_ok=True) + db_conn = duckdb.connect(str(settings.database_file)) + + # Read the SQL file + sql_file_path = Path(__file__).parent / "assets" / "database.sql" + with open(sql_file_path, "r") as sql_file: + sql_script = sql_file.read() + + # Execute the SQL script + db_conn.execute(sql_script) + + # Add admin user + hashed_password = bcrypt.hashpw(settings.admin_password, bcrypt.gensalt()) + db_conn.execute( + "INSERT INTO users (username, password, is_admin) VALUES (?, ?, 1)", + [settings.admin_username, hashed_password], + ) + db_conn.close() + + +def init_app() -> None: + if not settings.database_file.exists(): + generate_db() + + if not settings.logs_dir.exists(): + settings.logs_dir.mkdir(parents=True, exist_ok=True)