A clean and well-structured Go API base project that provides a solid foundation for building RESTful APIs. This project uses Fiber as the web framework and GORM for database operations with PostgreSQL.
- 🏗️ Clean architecture with separation of concerns
- 🔐 Environment variable configuration
- 🗄️ PostgreSQL database integration with GORM
- 🛣️ Organized routing system
- 🛡️ Middleware support
- 📦 Dependency management with Go modules
- ✅ End-to-end testing with SQLite
- Go 1.23.5 or higher
- PostgreSQL database
- Basic understanding of Go programming
.
├── app.go # Main application entry point
├── controllers/ # Request handlers
├── database/ # Database configuration and models
├── middlewares/ # Custom middleware functions
├── models/ # Data models
├── routes/ # Route definitions
├── services/ # Business logic
├── test/ # Test utilities and e2e tests
│ ├── e2e/ # End-to-end tests
│ └── test_helper.go # Test utilities
└── .env # Environment variables
-
Clone the repository:
git clone git@github.com:Mlcarvalho1/golang-fiber-base.git cd golang-api-base
-
Install dependencies:
go mod download
-
Create a
.env
file in the root directory with the following variables:DB_HOST=localhost DB_PORT=5432 DB_USER=your_username DB_PASSWORD=your_password DB_NAME=your_database
-
Run the application:
go run app.go
The server will start on http://localhost:3000
The project includes end-to-end tests using SQLite for the test database. This allows for fast and isolated testing without affecting your production database.
To run all tests:
go test ./test/e2e/... -v
To run a specific test:
go test ./test/e2e/... -v -run TestDummyCRUD
The test infrastructure provides helper functions to make writing e2e tests easier:
test.SetupTestApp(t)
: Creates a new Fiber app with SQLite databasetest.MakeRequest(t, app, method, path, body)
: Makes HTTP requests to the test apptest.ParseResponseBody(t, resp, v)
: Parses response body into a struct
Example test structure:
func TestYourFeature(t *testing.T) {
app := test.SetupTestApp(t)
defer test.CleanupTestApp(t)
// Your test code here
}
Tests use SQLite instead of PostgreSQL to:
- Speed up test execution
- Provide isolated test environments
- Avoid affecting production data
- Enable parallel test execution
- Fiber - Fast and efficient web framework
- GORM - ORM for database operations
- godotenv - Environment variable loader
- testify - Testing utilities
- Controllers: Handle HTTP requests and responses
- Models: Define data structures and database schemas
- Services: Implement business logic
- Routes: Define API endpoints and middleware
- Database: Configure database connections and migrations
- Middlewares: Implement cross-cutting concerns
- Tests: End-to-end tests with SQLite
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Thanks to the Go community for their amazing tools and libraries
- Special thanks to the Fiber and GORM teams for their excellent work