Skip to content

Commit 0f9ab93

Browse files
authored
Merge pull request #31 from Tejas5405/fix/move-db-creds-to-env-vars
fix: move hardcoded DB credentials and Flask secret key to environment variables
2 parents 8dc4bae + aeedb24 commit 0f9ab93

4 files changed

Lines changed: 21 additions & 16 deletions

File tree

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copy this file to .env and fill in your values
2+
# Never commit .env to version control
3+
4+
MYSQL_HOST=localhost
5+
MYSQL_USER=root
6+
MYSQL_PASSWORD=your_password_here
7+
FLASK_SECRET_KEY=your-secret-key-here

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ __pycache__
22
.pytest_cache
33
.venv
44
src/config.py
5+
.env

src/app.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from werkzeug.utils import secure_filename
55

66
app = Flask(__name__)
7-
app.secret_key = "your_secret_key"
7+
app.secret_key = os.environ.get("FLASK_SECRET_KEY", "change-me-in-production")
8+
89
UPLOAD_FOLDER = "uploads"
910
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
1011

@@ -17,32 +18,25 @@ def index():
1718
database_name = request.form["database"]
1819
table_name = request.form["table"]
1920
column_names = request.form["columns"]
20-
2121
file = request.files["file"]
2222
if file.filename == "":
2323
flash("No file selected", "danger")
2424
return redirect(request.url)
25-
2625
filename = secure_filename(file.filename)
2726
file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
28-
file.save(file_path) # Save uploaded file
29-
27+
file.save(file_path)
3028
try:
3129
result = subprocess.run(
32-
["python", "csv_to_mysql.py", database_name, table_name, column_names, file_path],
30+
["python", "csv_to_mysql.py", database_name, table_name, column_names, file_path],
3331
text=True,
3432
capture_output=True
3533
)
36-
3734
flash(result.stdout, "success")
3835
if result.stderr:
3936
flash(result.stderr, "danger")
40-
4137
except Exception as e:
4238
flash(f"Error: {e}", "danger")
43-
4439
return redirect(url_for("index"))
45-
4640
return render_template("index.html")
4741

4842
if __name__ == "__main__":

src/config.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# Enter host for your mysql server here, the default is set to localhost replace it if required
2-
host = "localhost"
1+
import os
32

4-
# Enter username for your mysql server
5-
user = "USERNAME"
3+
from dotenv import load_dotenv
64

7-
# Enter password for your mysql server
8-
password = "PASSWORD"
5+
load_dotenv()
6+
7+
# MySQL connection settings loaded from environment variables
8+
# Copy .env.example to .env and fill in your values
9+
host = os.environ.get("MYSQL_HOST", "localhost")
10+
user = os.environ.get("MYSQL_USER", "root")
11+
password = os.environ.get("MYSQL_PASSWORD", "")

0 commit comments

Comments
 (0)