Skip to content

Commit

Permalink
Create a config file using dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Gomez-Gonzalez committed Jul 13, 2024
1 parent d6da357 commit 455895a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ env/
**/__pycache__
.vscode
Pipfile*
database.db
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,40 @@ screenshots from Chrome and add them to the backend.

1. Fork/Clone

1. Create and activate a virtual environment:
2. Create and activate a virtual environment:

```sh
$ python3 -m venv venv && source venv/bin/activate
```

1. Install the requirements:
3. Install the requirements:

```sh
(venv)$ pip install -r requirements.txt
```

1. Run the app:
4. Create a `.env` file in the root folder and add the following configuration:

```sh
JWT_SECRET="some random secret key"
JWT_ALGORITHM="HS256"
DB_URL="sqlite:///database.db"
```

5. Run the app:

```sh
(venv)$ python main.py
```

1. Test at [http://localhost:8081/docs](http://localhost:8081/docs)
6. Test at [http://localhost:8081/docs](http://localhost:8081/docs)

**Note**: the random secret `JWT_SECRET` can be generated for example
using a command like this one:

```sh
openssl rand -hex 32
```

Feel free to change any of the configuration variables in `.env` to
match your needs.
6 changes: 3 additions & 3 deletions app/auth/auth_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time
import os
from typing import Dict, Annotated
from datetime import datetime, timedelta, timezone
from typing import Union
Expand All @@ -12,8 +12,8 @@
import app.auth.crypto as crypto


JWT_SECRET = "96706f2fb5daf56405a9f2554399e53e93fe2b5f22c1b6bfc48c1d1f77a79151"
JWT_ALGORITHM = "HS256"
JWT_SECRET = os.getenv("JWT_SECRET")
JWT_ALGORITHM = os.getenv("JWT_ALGORITHM")

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

Expand Down
9 changes: 5 additions & 4 deletions app/model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
from pydantic import BaseModel, Field, EmailStr
from typing import Optional, Union

from sqlmodel import Field, Session, SQLModel, Relationship, create_engine

sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
engine = create_engine(
os.getenv("DB_URL"),
echo=True,
connect_args=connect_args)

def get_db_session():
with Session(engine) as session:
Expand Down
Binary file removed database.db
Binary file not shown.
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import uvicorn
from dotenv import load_dotenv

if __name__ == "__main__":
load_dotenv()
uvicorn.run("app.api:app_obj", host="0.0.0.0", port=8081, reload=True)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ python-decouple==3.8
uvicorn==0.29.0
sqlmodel==0.0.19
passlib==1.7.4
bcrypt==4.1.3
bcrypt==4.1.3
python-dotenv==1.0.1

0 comments on commit 455895a

Please sign in to comment.