A production-ready, highly structured Go backend template using Fiber v3 and MongoDB. This template follows industry best practices for security, architecture, and maintainability, making it an ideal starting point for scalable applications.
- Framework: Fiber v3 (beta) — Fast, Express-inspired web framework for Go.
- Architecture: Clean Layered Architecture (Controller → Service → Repository).
- Database: MongoDB (using official mongo-go-driver v2).
- Authentication: JWT (JSON Web Token) with secure password hashing (bcrypt).
- File Uploads: AWS S3 integration with multi-file support.
- Image Optimization: Automatic image optimization and conversion to WebP using libvips.
- Validation: Robust input validation using go-playground/validator.
- Logging: Structured logging with zerolog.
- Error Handling: Centralized error handling with custom
AppErrortypes and HTTP status mapping. - Testing: Comprehensive test suite (45+ tests) using
go testand auto-generated mocks with gomock. - Containerization: Multi-stage
Dockerfileanddocker-compose.ymlfor production-ready deployment. - CI/CD: GitHub Actions workflow for linting, testing, and build verification.
.
├── main.go # Entrypoint: dependency injection, server start, graceful shutdown
├── Makefile # Automation for build, run, test, lint, and mocks
├── Dockerfile # Multi-stage production Docker build
├── compose.yml # Docker Compose for production/staging environments
├── deploy.sh # Deployment script with health checks
├── .air.toml # Configuration for live-reloading (Air)
├── .env.example # Template for environment variables
├── .golangci.yml # Linter configuration (golangci-lint)
│
├── config/ # Configuration management (env parsing, defaults)
├── controller/ # HTTP layer: parses requests, delegates to services
├── db/ # Database connection and index management
├── dto/ # Data Transfer Objects (Request/Response shapes)
├── helpers/ # App-specific utilities (AWS setup, image processing)
├── middleware/ # HTTP middlewares (auth, logging, error handling, etc.)
├── models/ # Database document models
├── pkg/ # Reusable, self-contained internal packages
│ ├── apperror/ # Structured application errors
│ ├── auth/ # JWT and password hashing services
│ ├── logger/ # Global structured logger setup
│ └── validate/ # Input validation wrapper
├── repository/ # Data Access Layer: MongoDB and S3 operations
│ └── mock/ # Auto-generated mocks for testing
├── routes/ # API route definitions and grouping
└── service/ # Business Logic Layer: core application rules
- Go 1.23+
- MongoDB (running locally or via Docker)
- libvips-dev (required for image optimization)
- Optional Tools:
- Air (for live reloading)
- mockgen (for generating test mocks)
- golangci-lint (for linting)
-
Clone the repository:
git clone https://github.com/chirag3003/go-backend-template.git cd go-backend-template -
Setup environment variables:
cp .env.example .env # Edit .env with your specific configuration (S3 keys, Mongo URI, etc.) -
Install dependencies:
go mod tidy
-
Run the application:
make run # OR for live-reloading: make devThe server will start at
http://localhost:5000.
| Variable | Description | Default |
|---|---|---|
PORT |
Port for the server to listen on | 5000 |
MONGO_URI |
MongoDB connection string | mongodb://localhost:27017 |
MONGO_DB |
MongoDB database name | go-template |
JWT_SECRET |
Secret key for signing JWTs | (Required) |
JWT_EXPIRATION |
Token validity duration (e.g., 24h, 1h) | 24h |
S3_ACCESS_KEY |
AWS IAM Access Key | (Required for S3) |
S3_SECRET_KEY |
AWS IAM Secret Key | (Required for S3) |
S3_REGION |
AWS S3 Region | ap-south-1 |
S3_BUCKET |
AWS S3 Bucket Name | (Required for S3) |
S3_ENDPOINT |
S3 API Endpoint | (Required for S3) |
LOG_LEVEL |
Logging level (debug, info, warn, error) | info |
| Target | Description |
|---|---|
make build |
Build the server binary to ./bin/server |
make run |
Build and run the application locally |
make dev |
Run the application with live-reloading (Air) |
make test |
Run all tests with race detection |
make test-coverage |
Run tests and generate HTML coverage report |
make lint |
Run the linter (golangci-lint) |
make mocks |
Regenerate test mocks from repository interfaces |
make docker-run |
Start the app and MongoDB via Docker Compose |
make tidy |
Tidy Go modules |
make clean |
Remove build artifacts and temporary files |
GET /health— Check server health statusGET /ready— Check database and AWS connectivityPOST /api/v1/auth/register— Create a new user accountPOST /api/v1/auth/login— Authenticate and receive a JWT token
GET /api/v1/user/me— Retrieve current authenticated user's profilePOST /api/v1/media/upload— Upload and optimize multiple images to S3
This project uses go test for unit and integration testing. Mocking is handled by go.uber.org/mock.
- Run all tests:
make test - Regenerate mocks:
make mocks - Check coverage:
make test-coverage
Build and run the entire stack (app + MongoDB) using Docker Compose:
make docker-runTo stop the services:
make docker-stopContributions are welcome! Please feel free to submit a Pull Request.
Chirag Bhalotia
- GitHub: @chirag3003
This project is licensed under the MIT License - see the LICENSE file for details.