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-template
Copy the example .env file and configure it as needed:
cp .env.example .env
# then generate secret key
go run . generate:key
Start http server with default configuration:
go run . serve
# OR
go run . serve --host=127.0.0.1 -p 9000
4. (Optional) Enable Hot Reloading with Air
Install Air if you don't have it:
go install github.com/air-verse/air@latest
Run the server with hot reload
air serve
# or
air serve --host=127.0.0.1 --port=9000
Run 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 -D
Make 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/20250518
Make sure to register your tables Database.tables
at /registry/database.go.
This project also supports other command-line operations:
go run . make:model --name=user
Make sure to register your tables Database.models
at /registry/database.go.
go run . make:repo --name=user
After created adjust registry at /registry/repository.go.
go run . make:service --name=auth
After created adjust registry at /registry/services.go.
go run . make:controller --name=home
After created adjust registry at /registry/controller.go.
command to create factory:
go run . make:factory --name=user
with specific output directory, default directory is /database/factory
go run . make:factory --name=user --output=./factory
# or
go run . make:factory --n user -o ./factory
command to run database seed with the factories
:
go run . db:seed
Make 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