A RESTful API to manage users and their posts, built with Go using the Gin web framework and ent for ORM.
- Go >= 1.24
- PostgreSQL
- direnv (only if using Nix with flakes enabled) or manual
.envloading - Docker (optional)
This project uses ent for database modeling and code generation. The generated code lives under:
internal/database/repositories/postgresql/ent/
The schema definitions are located in:
internal/database/repositories/postgresql/ent/schema/
To generate code from your schema definitions:
go run -mod=mod entgo.io/ent/cmd/ent generate \
--target=internal/database/repositories/postgresql/ent \
./internal/database/repositories/postgresql/ent/schemaTo create a new schema:
go run -mod=mod entgo.io/ent/cmd/ent new --target internal/database/repositories/postgresql/ent/schema <SchemaName>git clone https://github.com/danilevy1212/UserPostApi-Challenge.git
cd UserPostApi-ChallengeCopy .env.example to .env:
cp .env.example .envIf you're using Nix with flakes enabled, you can alternatively use direnv to load them:
direnv allowOtherwise, use a tool like dotenv, or manually export the environment variables in your shell
Before running the API, you should run the database migration:
go run ./cmd/migrationgo run ./cmd/apidocker compose up apiYou can also bring up the PostgreSQL database separately with:
docker compose up databaseThe project includes both unit and integration tests.
To run all tests:
go test -v ./...To update failing snapshot tests:
UPDATE_SNAPS=true go test ./...Snapshot tests use go-snaps and standard assertions use stretchr/testify.
Full API documentation with expected inputs, outputs, and error formats can be found at:
./docs/API.md
Postman and Bruno collections are also provided under:
./devtools/
To run the API in a local Kubernetes cluster:
minikube start --driver=dockerApply the configuration files:
kubectl apply -f k8s.yaml
kubectl apply -f k8s-secret.yaml # Use k8s-secret.example.yaml as templateBuild the Docker image inside the minikube environment:
eval $(minikube docker-env)
docker build -f Dockerfile -t challenge-api:latest .Forward the database port:
kubectl port-forward svc/db-service 5432:5432Run DB migration:
go run ./cmd/migration- Full CRUD for Users and Posts
- Cleanly separated layers (models, handlers, repository)
- Database migration via Go
- Snapshot testing for handlers (no full end-to-end integration tests by design)
- Structured logging with zerolog
- Pretty output in development
- JSON logs in production
- Email is unique per user
- Once a post is created, its ownership (
user_id) cannot be changed - Minimal, non-field-specific error feedback