This a template of GoLang Web API, using the Gin framework and Go ORM with PostgreSQL Database.
By Hidayat Arghandabi 2023
Last Update 06/19/2023
- HTTP Request
- Authorization and Authentication
- Middleware (Auth, Logger)
- PostgreSQL DB
- Containerization
- .env
- Migration
Run the docker-compose up
command
- Update DB Connection String in the .env file
- Create migration located in migrate/migrate.go using
go run migrate/migrate.go
. It will create the posts and users table in the database. - Install packages
go mod tidy
- You can run the project from main.go using the vscode debugger or
CompileDaemon -command="./go-crud"
the Daemon tool, make sure it is installed. - Enjoy this project
go mod init
creating a go mod file (like a node file for nodejs projects)
go get github.com/githubnemo/CompileDaemon
and install it, so that it can be run as command-line toolgo install github.com/githubnemo/CompileDaemon
watch files for changes and rebuildgo get github.com/joho/godotenv
Easy to load environment variablesgo get -u github.com/gin-gonic/gin
Gin Framework for Http Servergo get -u gorm.io/gorm
andgo get -u gorm.io/driver/postgres
Go ORM Library for Gogo get -u github.com/sirupsen/logrus
Package for logging in a Gin applicationgo get github.com/redis/go-redis/v9
Package for Redis go
go get -u golang.org/x/crypto/bcrypt
for cryptography visit https://pkg.go.dev/golang.org/x/cryptogo get -u github.com/golang-jwt/jwt/v4
jwt package visit https://pkg.go.dev/github.com/golang-jwt/jwt or https://github.com/golang-jwt/jwt
CompileDaemon -command="./go-crud"
Run the Daemon and given the package name
https://gorm.io/docs/models.html visit the link to see how to declare your models. we are using the following model
type User struct {
gorm.Model
Name string
}
// equals
type User struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Name string
to make auto db.AutoMigrate(&Product{})
visit link https://gorm.io/docs/index.html
as migrate/migrate.go is created run the migration using
go run migrate/migrate.go
, It will make the posts table in the database.
visit the link https://gorm.io/docs/create.html for Go ORM
docker-compose up --build