mirror of
https://github.com/tiennm99/openai-status-bot.git
synced 2026-10-11 03:13:41 +00:00
Swap the Redis datastore for MongoDB via mongo-driver v2. New internal/mongostore mirrors the old redisstore type/constant names so consumers change only the import qualifier; document model collapses the multi-key subscriber/settings and per-event delivery sets into single-doc operations and a TTL-indexed delivery collection. Config now requires MONGODB_URI (Atlas) with MONGODB_DATABASE selecting dev vs prod on one cluster; REDIS_URL removed. Compose files run bot-only against Atlas. Default tests stay Docker-free (poller/bot use fakes, mongostore unit tests are pure-logic); a gated //go:build integration suite covers the real backend via testcontainers.
4.2 KiB
4.2 KiB
phase, title, status, effort
| phase | title | status | effort |
|---|---|---|---|
| 4 | Config Wiring & Deployment | complete | S |
Phase 4: Config Wiring & Deployment
Overview
Replace Redis configuration, connection bootstrap, and deployment artifacts with MongoDB equivalents. After this phase main.go builds the Mongo client; consumer type swap is Phase 5.
Requirements
- New env:
MONGODB_URI(Atlasmongodb+srv://..., required, no localhost default — there is no local DB),MONGODB_DATABASE(defaultopenai_status_bot; dev env sets a separate name e.g.openai_status_bot_dev). RemoveREDIS_URL. main.goconnects, pings, ensures indexes, constructsmongostore.New(client, dbName).- Deployment (validated): Atlas for both prod and dev — single cluster, two databases differentiated by
MONGODB_DATABASE. No localmongoservice in any compose file.
Architecture
config.Config: replace RedisOptions *redis.Options with MongoURI string + MongoDatabase string. Drop parseRedisURL, minRedisDB/maxRedisDB. Validation: MONGODB_URI required (error if empty, like TELEGRAM_BOT_TOKEN at config.go:35-38 — no localhost fallback since both envs use Atlas); MONGODB_DATABASE defaults to openai_status_bot. Keep validation minimal; the driver validates the URI on connect/ping.
Related Code Files
- Modify:
internal/config/config.go— swap Redis fields/parsing for Mongo fields; keepgetEnvdefault pattern. - Modify:
internal/config/config_test.go— replace Redis URL cases with Mongo URI/DB cases. - Modify:
cmd/openai-status-bot/main.go— replaceredis.NewClient+Ping(main.go:37-42) withmongo.Connect(options.Client().ApplyURI(cfg.MongoURI)),client.Ping,defer client.Disconnect(ctx); callstore.EnsureIndexes(ctx);store := mongostore.New(client, cfg.MongoDatabase). Update the connect-error log fields (drop redis-specificNetwork/Addr/DB/TLS). - Modify:
.env.example—MONGODB_URI=mongodb+srv://<user>:<pass>@<cluster>/(placeholder),MONGODB_DATABASE=openai_status_bot; removeREDIS_URL. - Modify:
docker-compose.yml— remove theredisservice andredis-datavolume entirely (no local DB). Becomes bot-only, connecting to Atlas via.env; this is the development path, so setMONGODB_DATABASEto the dev database (e.g.openai_status_bot_dev). Nodepends_on. - Modify:
docker-compose.bot.yml— bot-only, connects to Atlas via.envwith the productionMONGODB_DATABASE(openai_status_bot). With no local DB, this anddocker-compose.ymldiffer only by database name; keep both for the dev/prod split. Optionally collapse to one file — but keeping both matches the existing two-file convention (YAGNI: don't restructure beyond the DB-name change). - Modify:
README.md— Configuration table (MONGODB_URI,MONGODB_DATABASErows; dropREDIS_URLand the percent-encoding note, or rewrite the note for Atlas SRV URIs), feature bullet "Uses Redis for…" → MongoDB, Compose/Quick-Start description (Atlas, no local DB; dev vs prod database), intro line. Note that no local datastore runs in Compose — an Atlas URI is required to start.
Implementation Steps
- Edit
config.go+config_test.go;go test ./internal/config/. - Edit
main.gofor Mongo client lifecycle +EnsureIndexes. (Will not fully build until Phase 5 swapsredisstore→mongostorein poller/bot — acceptable; Phases 4-5 land together.) - Update
.env.example, both compose files, README.
Success Criteria
configpackage + tests pass with Mongo env vars; no Redis symbols remain in config.main.goopens/pings Mongo, ensures indexes, buildsmongostore.- Compose,
.env.example, README reference only MongoDB.
Risk Assessment
- Risk: managed URIs carry credentials/
+srv. Mitigation:ApplyURIhandlesmongodb+srv://; never log the URI (log db name only); keep.envout of git (already in.gitignore). - Risk: dropping
REDIS_URLbreaks existing deploys silently. Mitigation: README "Notes" + this plan flag the env rename; no compatibility shim (clean cutover, intended).