From eeea1b2091958d33d0eac3109ddac7ec6bc6de8b Mon Sep 17 00:00:00 2001 From: Zvonimir Rudinski Date: Sun, 16 Nov 2025 14:38:23 +0100 Subject: [PATCH] add env file support --- .env.example | 2 ++ .gitignore | 3 ++- database.py | 5 ++--- requirements.txt | 1 + settings.py | 9 +++++++++ 5 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 .env.example create mode 100644 settings.py diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..5ec5bcb --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +JWT_SECRET=your_jwt_secret_key_here +JWT_ALGORITHM=HS256 diff --git a/.gitignore b/.gitignore index b1a2308..06b2e6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ .venv -database.db \ No newline at end of file +database.db +.env diff --git a/database.py b/database.py index aa196a9..d3837b5 100644 --- a/database.py +++ b/database.py @@ -1,11 +1,10 @@ from models import User from passlib.context import CryptContext +from settings import settings import sqlite3 import jwt import datetime -JWT_SECRET = "secret" - connection = sqlite3.connect('database.db') connection.row_factory = sqlite3.Row cursor = connection.cursor() @@ -46,4 +45,4 @@ def login(user: User) -> str: "exp": exp } - return jwt.encode(payload=payload, key=JWT_SECRET, algorithm="HS256") \ No newline at end of file + return jwt.encode(payload=payload, key=settings.jwt_secret, algorithm=settings.jwt_algorithm) diff --git a/requirements.txt b/requirements.txt index 2918174..e1a0537 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ fastapi[standard]==0.121.2 passlib==1.7.4 pyjwt==2.10.1 +pydantic-settings==2.12.0 diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..b5b957f --- /dev/null +++ b/settings.py @@ -0,0 +1,9 @@ +from pydantic_settings import BaseSettings, SettingsConfigDict + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8') + + jwt_secret: str + jwt_algorithm: str + +settings = Settings()