Skip to content

fiqryx/go-webservice-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡Go Webservice API Template

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

How to Run

1. Clone the Repository

git clone https://github.com/fiqryx/go-webservice-template.git
cd go-webservice-template

2. Setup Environment Variables

Copy the example .env file and configure it as needed:

cp .env.example .env
# then generate secret key
go run . generate:key

3. Run the Application

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

Database Migration

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.

Database backup

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.


Additional CLI Commands

This project also supports other command-line operations:

Create model:

go run . make:model --name=user

Make sure to register your tables Database.models at /registry/database.go.

Create repository:

go run . make:repo --name=user

After created adjust registry at /registry/repository.go.

Create service:

go run . make:service --name=auth

After created adjust registry at /registry/services.go.

Create controller:

go run . make:controller --name=home

After created adjust registry at /registry/controller.go.

Create factory

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

Database seeding

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"

How to Build

$ go build -o ./bin/api
# or with vendor
$ go build -mod=vendor -o ./bin/api

About

minimal starter template for building Go REST APIs

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published