A small Go web service that writes a timestamp into MongoDB every time you hit /, and reads them all back on /logs. The point of this repository is not the app: it is packaging it properly with containers, on a private network, with persistent data and no secret in the git history.
Full brief in docs/CONSIGNES.md.
Two containers on one user defined bridge network. Only the app publishes a port to the host. MongoDB stays unreachable from outside the network, and its data lives in a named volume so it survives a rebuild or a reboot.
| Method | Path | What it does |
|---|---|---|
GET |
/ |
Inserts { created_at } in MongoDB, returns the new document id |
GET |
/logs |
Returns every document in the collection |
GET |
/healthz |
Returns {"status":"healthy"}, used as the container healthcheck |
Every setting comes from the environment. The app refuses to start if one is missing, which is intentional: a container that boots half configured is worse than one that fails loudly.
| Variable | Required | Notes |
|---|---|---|
MONGODB_HOST |
yes | Service name on the docker network, not localhost |
MONGODB_PORT |
yes | 27017 |
MONGODB_USER |
yes | |
MONGODB_PASSWORD |
one of the two | Plain value, handy for local go run |
MONGODB_PASSWORD_FILE |
one of the two | Path to a file holding the password, this is the container path |
PORT |
no | HTTP listen port, defaults to 8080 |
MONGODB_PASSWORD_FILE is the one that matters here. It lets the password arrive as a mounted file instead of an environment variable, so it never shows up in docker inspect, in the image layers, or in the repository.
Requirements: Docker with the Compose plugin, and Go 1.26+ only if you want to run the app outside a container.
git clone git@gitlab.com:WhiteMuush/simplon-containers-tp.git
cd simplon-containers-tp
cp .env.example .env # then edit the credentials in .envBoot the whole stack (app + MongoDB) with one command:
docker compose up -d # start in the background
docker compose logs -f api # follow the app logs
docker compose down # stop, data survives in the mongo-data volumeThen check it answers:
curl localhost:8080/healthz # {"status":"healthy"}
curl localhost:8080/ # inserts one document
curl localhost:8080/logs # reads them all backAfter changing the Go code, rebuild and redeploy the API only. Compose keeps MongoDB and its volume untouched, so no data is lost:
docker compose up -d --build apiBuild the image by hand (without starting anything), for example to tag a release:
docker build -t sct-api:dev .
# or pin a version, which is what compose reads from APP_VERSION
APP_VERSION=1.0.0 docker compose build apiOnly needs Go 1.26+. Point it at a throwaway MongoDB:
docker run -d --name mongo-dev -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=app \
-e MONGO_INITDB_ROOT_PASSWORD=devpassword \
mongo:7.0
MONGODB_HOST=localhost MONGODB_PORT=27017 \
MONGODB_USER=app MONGODB_PASSWORD=devpassword \
go run ..
├── main.go # wiring: env, mongo client, routes
├── handlers.go # the three HTTP handlers
├── docs/
│ ├── CONSIGNES.md # the brief
│ └── assets/ # diagrams
└── .gitignore # keeps .env and secrets out of git
main is a protected branch: pushing to it directly is disabled for everyone, so work happens on a branch and lands through a merge request.