minimal starter template for building Go REST APIs using:
- ⚙️ Gin – Fast and flexible HTTP web framework
- 🧬 GORM – ORM for PostgreSQL and more
- 🐘 PostgreSQL – Reliable and powerful relational database
git clone https://github.com/fiqryx/go-webservice-template.git
cd go-webservice-templateCopy the example .env file and configure it as needed:
cp .env.example .env
# then generate secret key
go run . generate:keyStart http server with default configuration:
go run . serve
# OR
go run . serve --host=127.0.0.1 -p 90004. (Optional) Enable Hot Reloading with Air
Install Air if you don't have it:
go install github.com/air-verse/air@latestRun the server with hot reload
air serve
# or
air serve --host=127.0.0.1 --port=9000Run database migrations using GORM's AutoMigrate feature. This will automatically create or update tables based on your Go model definitions.
go run . migrate
# or
go run . migrate -DMake sure to register your models in the Database.models at /registry/database.go.
command to backup database with registry tables:
go run . db:backup
# or
go run . db:backup --output=./storage/backup/20250518Make sure to register your tables Database.tables at /registry/database.go.
This project also supports other command-line operations:
go run . make:model --name=userMake sure to register your tables Database.models at /registry/database.go.
go run . make:repo --name=userAfter created adjust registry at /registry/repository.go.
go run . make:service --name=authAfter created adjust registry at /registry/services.go.
go run . make:controller --name=homeAfter created adjust registry at /registry/controller.go.
command to create factory:
go run . make:factory --name=userwith specific output directory, default directory is /database/factory
go run . make:factory --name=user --output=./factory
# or
go run . make:factory --n user -o ./factorycommand to run database seed with the factories:
go run . db:seedMake sure the configuration Database.factories at /registry/database.go.
How to use module.sh
This script helps rename the Go module path in go.mod and across your project files.
# using default values (e.g., template.go → webservices)
./module.sh
# using custom module paths
./module.sh "old/module/path" "new/module/path"$ go build -o ./bin/api
# or with vendor
$ go build -mod=vendor -o ./bin/api