This repository is a lightweight and structured Go (Golang) boilerplate codebase. It follows best practices to ensure a clean and maintainable codebase.
The codebase provides the following features:
- Clean code
- Dependency injection
- Unit test
- Environment dependent application configuration management
- Database migration
- Live reloading during development
The codebase uses the following Go packages.
- Web framework: gofiber
- Database ORM: gorm
- Dot env file reader: godotenv
- Redis client (optional): go-redis
.
├── cmd main applications of the project
│ └── server the API server application
├── pkg reusable packages
│ ├── config config
│ ├── handler routes and handlers
│ ├── model database entities
│ ├── service bussiness logic
│ └── utils utility functions like initialize database connection
└── test test files
The top level directories cmd
, pkg
are commonly found in other popular Go projects,
also you can create internal
directory to separate reusable packages into public and private packages.
Standard Go Project Layout.
To run this project you have to have running postgres database and Go 1.18 or above installed locally. Then create pkg/config/envs/.env
and set environment variables.
# download repository
git clone https://github.com/serod11/gofiber-boilerplate.git
cd gofiber-boilerplate
# run the REST API server
make run
# or run the API server with live reloading, which is useful during development
# requires air to be installed locally (https://github.com/cosmtrek/air)
make run-live
# run unit test
make unit-test
# build binary executable
make build
At this time, you have a REST API server running at http://127.0.0.1:8080
. It provides following endpoints:
GET /health
: a healthcheck serviceGET /book
: returns all books from database book tableGET /book/:id
: returns a book by idPOST /book
: add new book
Try the URL http://localhost:8080/healthcheck
, and you should see {"status": "OK"}
displayed.
curl -X GET http://localhost:8080/health