mirror of
https://github.com/mattintech/simplefileupload-server.git
synced 2026-07-11 13:11:53 +00:00
refactoring how the .env is loaded
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
FROM python:3.8-slim
|
FROM python:3.13-slim
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY ./src /app
|
COPY ./src /app
|
||||||
COPY requirements.txt /app/requirements.txt
|
COPY requirements.txt /app/requirements.txt
|
||||||
|
|||||||
38
src/app.py
38
src/app.py
@@ -9,13 +9,34 @@ import utils.mLogger as log
|
|||||||
from controllers.file_controller import upload_file, download_file, delete_file
|
from controllers.file_controller import upload_file, download_file, delete_file
|
||||||
from views import views
|
from views import views
|
||||||
|
|
||||||
# Load environment variables
|
# Get the project root directory (one level up from src)
|
||||||
load_dotenv()
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
env_path = os.path.join(project_root, '.env')
|
||||||
|
|
||||||
|
# Load environment variables with explicit path
|
||||||
|
load_dotenv(dotenv_path=env_path)
|
||||||
|
|
||||||
|
# Debug log for environment loading
|
||||||
|
log.i(f"Loading .env from: {env_path}")
|
||||||
|
log.i(f"File exists: {os.path.exists(env_path)}")
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['UPLOAD_FOLDER'] = 'tmp/uploads'
|
app.config['UPLOAD_FOLDER'] = 'tmp/uploads'
|
||||||
app.config['CLIENT_KEY'] = os.getenv('CLIENT_KEY', 'default-secret-key')
|
app.config['CLIENT_KEY'] = os.getenv('CLIENT_KEY', 'default-secret-key')
|
||||||
|
|
||||||
|
# Log the loaded client key (masked)
|
||||||
|
if app.config['CLIENT_KEY']:
|
||||||
|
masked_key = app.config['CLIENT_KEY'][:4] + '*' * (len(app.config['CLIENT_KEY']) - 4)
|
||||||
|
log.i(f"Loaded client key: {masked_key}")
|
||||||
|
else:
|
||||||
|
log.w("No client key configured, using default")
|
||||||
|
|
||||||
|
# Log all environment variables (masked)
|
||||||
|
for key, value in os.environ.items():
|
||||||
|
if 'KEY' in key or 'SECRET' in key.upper():
|
||||||
|
masked = value[:4] + '*' * (len(value) - 4) if value else None
|
||||||
|
log.i(f"Env: {key}={masked}")
|
||||||
|
|
||||||
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
||||||
os.makedirs(app.config['UPLOAD_FOLDER'])
|
os.makedirs(app.config['UPLOAD_FOLDER'])
|
||||||
|
|
||||||
@@ -23,9 +44,16 @@ def require_client_key(f):
|
|||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated_function(*args, **kwargs):
|
def decorated_function(*args, **kwargs):
|
||||||
client_key = request.headers.get('X-Client-Key')
|
client_key = request.headers.get('X-Client-Key')
|
||||||
if not client_key or client_key != app.config['CLIENT_KEY']:
|
if not client_key:
|
||||||
log.e("Invalid or missing client key")
|
log.e("No client key provided in request headers")
|
||||||
return jsonify({'error': 'Invalid or missing client key'}), 401
|
return jsonify({'error': 'Missing client key'}), 401
|
||||||
|
|
||||||
|
expected_key = app.config['CLIENT_KEY']
|
||||||
|
if client_key != expected_key:
|
||||||
|
log.e(f"Invalid client key provided. Expected length: {len(expected_key)}, Got length: {len(client_key)}")
|
||||||
|
return jsonify({'error': 'Invalid client key'}), 401
|
||||||
|
|
||||||
|
log.i("Client key authentication successful")
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user