Skip to content

Commit 1907de1

Browse files
committed
Added user seeding
1 parent 1321c40 commit 1907de1

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ COPY requirements.txt .
77
# Copy configuration and initial data files
88
COPY auth_config.yml .
99
COPY initial_todos.json .
10+
COPY initial_users.json .
1011

1112
# Install Python dependencies listed in requirements.txt
1213
RUN pip install --no-cache-dir -r requirements.txt

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ The API's authentication is configured through `auth_config.yml` in the root dir
6161

6262
## Initial Data
6363

64-
The project comes with `initial_todos.json`, which contains a set of predefined todo items that serve as sample data. This file includes example todos with various states (completed and pending) to help you get started with testing the API immediately.
64+
The project comes with initial data, seeded at startup:
65+
- Todos: `initial_todos.json`
66+
- Users: `initial_users.json`
6567

6668
## Running the API
6769

app/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from routes.notes import notes_bp
66
from routes.auth import auth_bp, init_auth_routes
77
from middleware.auth_middleware import AuthMiddleware
8-
from utils.config import load_config, load_initial_todos
8+
from utils.config import load_config, load_initial_todos, load_initial_users
99
from utils.auth import setup_auth_config
10-
from services.auth_service import init_auth_service
10+
from services.auth_service import init_auth_service, add_user
1111
import secrets
1212

1313
def create_app(auth_config):
@@ -55,13 +55,19 @@ def create_app(auth_config):
5555

5656
return app
5757

58+
def seed_users():
59+
for user_data in load_initial_users():
60+
if not add_user(user_data["username"], user_data["password"]):
61+
print(f"Error: Failed to add user on init")
62+
5863
if __name__ == "__main__":
5964
try:
6065
# Load authentication configuration from config file
6166
auth_method, secret = load_config()
6267

6368
# Set up authentication based on configuration
6469
auth_config = setup_auth_config(auth_method, secret)
70+
seed_users()
6571

6672
# Initialize auth service
6773
init_auth_service(auth_config)

app/utils/config.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import yaml
22
import os
33
import json
4+
from pathlib import Path
5+
from typing import List
6+
7+
INITIAL_USERS_FILE = "initial_users.json"
8+
INITIAL_TODOS_FILE = "initial_todos.json"
9+
410

511
def load_config():
612
"""Load configuration from auth_config.yml file."""
@@ -38,18 +44,27 @@ def load_config():
3844
print("Using default configuration (no auth)")
3945
return "none", None
4046

47+
4148
def load_initial_todos():
4249
"""Load initial todos from the configuration file."""
43-
todos_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'initial_todos.json')
44-
45-
if not os.path.exists(todos_path):
46-
print(f"Warning: initial_todos.json not found at {todos_path}, using empty todos list")
50+
path = Path(__file__).resolve().parents[2] / INITIAL_TODOS_FILE
51+
return load_initial_data(path, "todos")
52+
53+
def load_initial_users() -> List:
54+
path = Path(__file__).resolve().parents[2] / INITIAL_USERS_FILE
55+
return load_initial_data(path)
56+
57+
def load_initial_data(path: Path, data_key: str = "data") -> List:
58+
filename = path.name
59+
60+
if not path.exists():
61+
print(f"Warning: {filename} not found at {path}, using empty list")
4762
return []
48-
63+
4964
try:
50-
with open(todos_path, 'r') as f:
65+
with open(path, 'r') as f:
5166
data = json.load(f)
52-
return data.get('todos', [])
67+
return data.get(data_key, [])
5368
except Exception as e:
54-
print(f"Error loading initial_todos.json: {e}")
55-
return []
69+
print(f"Error loading {filename}: {e}")
70+
return []

initial_users.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"data": [
3+
{
4+
"username": "johnsmith",
5+
"password": "testpass123"
6+
}
7+
]
8+
}

0 commit comments

Comments
 (0)