A modular, testable, and containerized backend for a simple bookstore, built with Go, Gin, GORM, and Docker. The project follows best practices for configuration, authentication, and integration testing.
- RESTful API for book and user management
- JWT-based authentication and authorization
- Integration tests with isolated Dockerized PostgreSQL
- Configurable via environment variables
- Modular package structure
backend/
├── api/ # API route definitions and Swagger docs
├── benchmark/ # (Reserved for benchmarks)
├── cmd/ # Application entrypoints (main.go, etc.)
├── config/ # Configuration loading and management
├── db/ # Database migrations
├── docs/ # Generated API documentation (Swagger)
├── infrastructure/ # Auth, DB, Docker, Logger, etc.
├── internal/ # Business logic (books(with unittest), users(with unittest), middleware, utils)
├── pkg/ # Shared packages (crypto, migration, validator)
├── test/ # Integration tests
├── .env # Environment variables (local)
├── .example.env # Example environment variables
├── go.mod, go.sum # Go dependencies
└── README.md # Project documentation
-
Clone the repository:
git clone https://github.com/chai-rs/go-gin-template.git cd go-gin-template
-
Copy and configure environment variables:
cp .example.env .env # Edit .env as needed
-
Install Go dependencies:
go mod download
-
(Optional) Install Docker
Required for running integration tests.
- All configuration is managed via the
config
package and.env
file. - Secrets (e.g.,
ACCESS_SECRET
,REFRESH_SECRET
) are set in the environment and can be overridden in test setup.
go run ./cmd/server
Or use Docker Compose:
docker-compose --env-file .docker.env up --build
- The application will be available at http://localhost:8000
- Only the server port is exposed; Redis and Postgres are accessible only within the Docker network.
- Environment variables for Compose are managed in
.docker.env
(see below).
.env
is for local development (e.g., running withgo run
on your host)..docker.env
is for Docker Compose. Service hostnames must match the Compose service names (backend-postgres
,backend-redis
).- See
.example.env
for all available variables and their expected values.
- backend-server: Your Go app, built from
/cmd/server
. - backend-postgres: PostgreSQL 17, data stored in
pgdata
volume. - backend-redis: Redis 7, data stored in
redisdata
volume.
All services are connected via the bookstore-network
for secure internal communication.
- Integration tests use
testify/suite
andory/dockertest
to spin up a PostgreSQL container, run migrations, and clean up automatically. - Example test suite:
test/base_test.go
,test/user_test.go
Run all tests:
go test ./...
- Swagger/OpenAPI docs are available in
docs/
and generated from code comments. - To view locally, run the server and visit
/swagger/index.html
(if route is enabled).
- Authentication: JWT-based, with token generation and verification in
infrastructure/auth
. - Authorization: Enforced via Casbin with Gorm adapter, configured in
auth_model.conf
. - Database: GORM ORM with migrations in
db/migrations
. - Testing: Uses Dockerized PostgreSQL for isolation, see
BaseSuite
intest/base_test.go
. - Handlers & Services: Business logic in
internal/
, separated by domain (books, users).
- Fork the repository
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes
- Push to the branch (
git push origin feature/fooBar
) - Open a pull request
MIT License