Todo App + Auth + GORM + Testcontainers
Todo App with Auth using GORM and Testcontainers
This project demonstrates a Todo application with authentication using GORM and Testcontainers.
The database is a Postgres instance created using the GoFiber's Testcontainers Service module. The instance is reused across multiple runs of the application, allowing to develop locally without having to wait for the database to be ready.
When using the air command to run the application, the database is automatically started alongside the Fiber application, and it's automatically stopped when the air command is interrupted.
Prerequisites
Ensure you have the following installed and available in your GOPATH:
- Golang
- Docker, which Testcontainers uses to start the PostgreSQL container
- Air for hot reloading
- Godotenv CLI, which
.air.confuses to load.env
Installation
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/local-development-testcontainersInstall dependencies:
go getCreate the environment file and add a secret for signing the JWTs.
airloads.env, and the application refuses to start withoutTOKEN_KEY:cp .env.example .env echo "TOKEN_KEY=$(openssl rand -hex 32)" >> .env
Running the Application
- Start the application:
air
Environment Variables
| Variable | Description | Default |
|---|---|---|
PORT | Server listening port | 8000 |
DB | PostgreSQL connection string. Only builds without the dev tag use it. air builds with -tags dev, which starts a PostgreSQL container with Testcontainers and ignores DB | postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable |
TOKEN_KEY | Secret used to sign the JWTs. Generate a random one with openssl rand -hex 32 | required |
TOKEN_EXP | Token lifetime, a time.ParseDuration string | 10h |
TESTCONTAINERS_RYUK_DISABLED | Disables the Ryuk container so the database container is not removed when the application stops | true in .env.example |