modulepackage
0.0.0-20241126132851-c4f5d7309c3a
Repository: https://github.com/hidayatarg/go-crud.git
Documentation: pkg.go.dev
# README
Go Web API
This a template of GoLang Web API, using the Gin framework and Go ORM with PostgreSQL Database.
By Hidayat Arghandabi 2023
Implementations
Last Update 06/19/2023
- HTTP Request
- Authorization and Authentication
- Middleware (Auth, Logger)
- PostgreSQL DB
- Containerization
- .env
- Migration
How to run using docker container
Run the docker-compose up
command
How to run/use this template without docker containers
- 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
Initalization
go mod init
creating a go mod file (like a node file for nodejs projects)
Required Packages
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
for JWT authentication
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
Running Development Server
CompileDaemon -command="./go-crud"
Run the Daemon and given the package name
Declaring Models
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.
Go ORM Create
visit the link https://gorm.io/docs/create.html for Go ORM
Important Commands
docker-compose up --build
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author