Skip to content

Commit cea1f49

Browse files
Merge pull request #4 from Titus-System/feat/password-management
Configure Mongo auth end-to-end (Compose, env docs, and app connection string)
2 parents b26297f + 3ef9542 commit cea1f49

6 files changed

Lines changed: 94 additions & 18 deletions

File tree

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ docker-compose.yaml
2727
.vscode
2828
.idea
2929
*.DS_Store
30+
31+
.digital-ocean

.env.example

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PROJECT_VERSION=1.0.0
44

55
ENVIRONMENT=development
66

7-
FRONTEND_URL=http://localhost:3000
7+
FRONTEND_URL=http://syncdesk.pro
88

99
# CORS settings
1010
CORS_ALLOW_ORIGINS=["https://app.example.com","http://localhost:3000"]
@@ -20,8 +20,10 @@ POSTGRES_HOST=localhost
2020
POSTGRES_PORT=5432
2121

2222
# Configurações do MongoDB
23-
# MONGO_USER=mongouser
24-
# MONGO_PASSWORD=mongopassword
23+
MONGO_INITDB_ROOT_USERNAME=mongouser # option to be used with docker compose
24+
MONGO_INITDB_ROOT_PASSWORD=mongopassword # to be used with docker compose
25+
MONGO_USER=mongouser
26+
MONGO_PASSWORD=mongopassword
2527
MONGO_HOST=localhost
2628
MONGO_PORT=27017
2729
MONGO_DB=syncdesk_db

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ logs/*
4646

4747
.github/*
4848
!/.github/workflows/
49+
50+
51+
.digital-ocean

README.md

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ Each sub-module has its own README with detailed documentation:
5050
## Prerequisites
5151

5252
- **Python 3.12+**
53+
- **Poetry** (package manager) — [install guide](https://python-poetry.org/docs/#installation)
5354
- **PostgreSQL** (running locally or in a container)
5455
- **MongoDB** (running locally or in a container)
55-
- **Poetry** (package manager) — [install guide](https://python-poetry.org/docs/#installation)
56+
- **Docker + Docker Compose plugin** (recommended for quickest setup)
5657

5758
---
5859

@@ -61,15 +62,21 @@ Each sub-module has its own README with detailed documentation:
6162
### 1. Clone and install dependencies
6263

6364
```bash
64-
git clone <repository-url>
65-
cd backend
65+
git clone https://github.com/Titus-System/syncdesk-api.git
66+
cd syncdesk-api
6667
make install
6768
# or: poetry install
6869
```
6970

7071
### 2. Configure environment variables
7172

72-
Create a `.env` file in the project root. All variables have sensible defaults for local development, so a minimal `.env` can be empty — but you should at least set a proper JWT secret:
73+
Create a `.env` file in the project root:
74+
75+
```bash
76+
cp .env.example .env
77+
```
78+
79+
Use these values as a baseline for local development:
7380

7481
```dotenv
7582
# .env
@@ -85,13 +92,18 @@ POSTGRES_PORT=5432
8592
POSTGRES_DB=syncdesk_db
8693
8794
# MongoDB
88-
MONGO_USER=
89-
MONGO_PASSWORD=
95+
MONGO_USER=mongouser
96+
MONGO_PASSWORD=mongopassword
9097
MONGO_HOST=localhost
9198
MONGO_PORT=27017
9299
MONGO_DB=syncdesk_db
93100
101+
# Mongo root user (required when Mongo runs via docker compose)
102+
MONGO_INITDB_ROOT_USERNAME=mongouser
103+
MONGO_INITDB_ROOT_PASSWORD=mongopassword
104+
94105
# JWT (change the secrets in any non-local environment)
106+
JWT_SECRET_KEY=change-me-in-production
95107
ACCESS_TOKEN_SIGNING_KEY=change-me-in-production
96108
REFRESH_TOKEN_SIGNING_KEY=change-me-in-production
97109
JWT_ALGORITHM=HS256
@@ -109,22 +121,32 @@ PROJECT_VERSION=0.1.0
109121

110122
Full variable reference is in the [core/ docs](app/core/README.md#configuration-configpy).
111123

124+
Important:
125+
126+
- For Docker Compose, keep `MONGO_USER` / `MONGO_PASSWORD` equal to `MONGO_INITDB_ROOT_USERNAME` / `MONGO_INITDB_ROOT_PASSWORD`.
127+
- The app authenticates MongoDB using `authSource=admin` when credentials are present.
128+
112129
### 3. Set up the databases
113130

114-
#### Option A: Development mode (auto-setup)
131+
#### Option A: Local API run (with local DB services)
115132

116133
When `ENVIRONMENT=development`, the app:
117134

118135
- connects to MongoDB on startup,
119-
- and **automatically creates the PostgreSQL database and all tables** (drops and recreates).
136+
- creates the PostgreSQL database if it does not exist,
137+
- and runs Alembic migrations if the schema is behind `head`.
120138

121-
Make sure both PostgreSQL and MongoDB are running:
139+
Start databases first (one simple option is using Compose only for DB services):
122140

123141
```bash
124-
make dev
142+
docker compose up -d db mongo
125143
```
126144

127-
> **Warning:** Postgres auto-setup drops all tables every startup in development. Use only for local development.
145+
Then run the API locally:
146+
147+
```bash
148+
make dev
149+
```
128150

129151
#### Option B: Using Alembic migrations (recommended for staging/production)
130152

@@ -207,19 +229,37 @@ POSTGRES_DB=syncdesk_db
207229
POSTGRES_HOST=localhost
208230
POSTGRES_PORT=5432
209231
232+
MONGO_INITDB_ROOT_USERNAME=mongouser
233+
MONGO_INITDB_ROOT_PASSWORD=mongopassword
234+
MONGO_USER=mongouser
235+
MONGO_PASSWORD=mongopassword
210236
MONGO_HOST=localhost
211237
MONGO_PORT=27017
212238
MONGO_DB=syncdesk_db
213239
```
214240

215241
`POSTGRES_HOST` is automatically overridden to `db`, and `MONGO_HOST` to `mongo`, inside the API container.
216242

243+
`MONGO_INITDB_ROOT_*` is used to create the MongoDB root user on first startup. The API connects with `MONGO_USER`/`MONGO_PASSWORD` and authenticates against `admin`.
244+
217245
### 2. Start all services
218246

219247
```bash
220248
docker compose up --build
221249
```
222250

251+
To run in detached mode:
252+
253+
```bash
254+
docker compose up -d --build
255+
```
256+
257+
To follow API logs:
258+
259+
```bash
260+
docker compose logs -f api
261+
```
262+
223263
What happens automatically:
224264

225265
- PostgreSQL container starts and becomes healthy
@@ -229,6 +269,15 @@ What happens automatically:
229269
- Alembic runs: `alembic upgrade head`
230270
- FastAPI starts on `http://localhost:8000`
231271

272+
If you previously changed Mongo credentials and still get `Authentication failed`, recreate containers and volumes once:
273+
274+
```bash
275+
docker compose down -v
276+
docker compose up --build
277+
```
278+
279+
If ports are already in use locally, adjust host ports in `docker-compose.yaml`.
280+
232281
### 3. Stop services
233282

234283
```bash
@@ -240,6 +289,21 @@ To also remove Postgres and Mongo persisted data:
240289
```bash
241290
docker compose down -v
242291
```
292+
293+
Data persistence behavior:
294+
295+
- `docker compose down` keeps DB data (named volumes are preserved)
296+
- `docker compose down -v` removes DB data
297+
298+
Quick reset commands:
299+
300+
```bash
301+
# Stop and keep data
302+
docker compose down
303+
304+
# Stop and delete database data
305+
docker compose down -v
306+
```
243307
---
244308

245309
## Code Quality

app/core/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ def mongo_database_url(self) -> str:
7878
if self.MONGO_USER and self.MONGO_PASSWORD:
7979
return (
8080
f"mongodb://{self.MONGO_USER}:{self.MONGO_PASSWORD}@"
81-
f"{self.MONGO_HOST}:{self.MONGO_PORT}/{self.mongo_db_test}"
81+
f"{self.MONGO_HOST}:{self.MONGO_PORT}/{self.MONGO_DB}?authSource=admin"
8282
)
83-
return f"mongodb://{self.MONGO_HOST}:{self.MONGO_PORT}/{self.mongo_db_test}"
83+
return f"mongodb://{self.MONGO_HOST}:{self.MONGO_PORT}/{self.MONGO_DB}"
8484

8585
@property
8686
def test_mongo_bd_url(self) -> str:
8787
if self.MONGO_USER and self.MONGO_PASSWORD:
8888
return (
8989
f"mongodb://{self.MONGO_USER}:{self.MONGO_PASSWORD}@"
90-
f"{self.MONGO_HOST}:{self.MONGO_PORT}/{self.MONGO_DB}"
90+
f"{self.MONGO_HOST}:{self.MONGO_PORT}/{self.mongo_db_test}?authSource=admin"
9191
)
9292
return f"mongodb://{self.MONGO_HOST}:{self.MONGO_PORT}/{self.mongo_db_test}"
9393

docker-compose.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ services:
2424
image: mongo:7
2525
container_name: syncdesk_mongo_db
2626
restart: unless-stopped
27+
env_file:
28+
- .env
29+
environment:
30+
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
31+
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
2732
ports:
2833
- "27017:27017"
2934
volumes:
3035
- mongo_data:/data/db
3136
healthcheck:
32-
test: [ "CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping')" ]
37+
test: [ "CMD", "mongosh", "--quiet", "-u", "${MONGO_INITDB_ROOT_USERNAME}", "-p", "${MONGO_INITDB_ROOT_PASSWORD}", "--authenticationDatabase", "admin", "--eval", "db.adminCommand('ping')" ]
3338
interval: 30s
3439
timeout: 5s
3540
retries: 5

0 commit comments

Comments
 (0)