Skip to content

Commit ea82bfc

Browse files
committed
golang gorm postgres setup
0 parents  commit ea82bfc

File tree

8 files changed

+853
-0
lines changed

8 files changed

+853
-0
lines changed

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
.DS_Store
17+
TODO.md
18+
logs.txt
19+
.idea/
20+
secret.md
21+
app.env

docker-compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
postgres:
3+
image: postgres
4+
container_name: postgres
5+
ports:
6+
- 6500:5432
7+
env_file:
8+
- ./app.env
9+
volumes:
10+
- postgres:/var/lib/postgresql/data
11+
volumes:
12+
postgres:

go.mod

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module github.com/wpcodevo/golang-gorm-postgres
2+
3+
go 1.18
4+
5+
require (
6+
github.com/spf13/viper v1.12.0
7+
gorm.io/driver/postgres v1.3.8
8+
gorm.io/gorm v1.23.8
9+
)
10+
11+
require (
12+
github.com/fsnotify/fsnotify v1.5.4 // indirect
13+
github.com/gin-contrib/sse v0.1.0 // indirect
14+
github.com/gin-gonic/gin v1.8.1 // indirect
15+
github.com/go-playground/locales v0.14.0 // indirect
16+
github.com/go-playground/universal-translator v0.18.0 // indirect
17+
github.com/go-playground/validator/v10 v10.11.0 // indirect
18+
github.com/goccy/go-json v0.9.10 // indirect
19+
github.com/hashicorp/hcl v1.0.0 // indirect
20+
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
21+
github.com/jackc/pgconn v1.12.1 // indirect
22+
github.com/jackc/pgio v1.0.0 // indirect
23+
github.com/jackc/pgpassfile v1.0.0 // indirect
24+
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
25+
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
26+
github.com/jackc/pgtype v1.11.0 // indirect
27+
github.com/jackc/pgx/v4 v4.16.1 // indirect
28+
github.com/jinzhu/inflection v1.0.0 // indirect
29+
github.com/jinzhu/now v1.1.4 // indirect
30+
github.com/json-iterator/go v1.1.12 // indirect
31+
github.com/leodido/go-urn v1.2.1 // indirect
32+
github.com/magiconair/properties v1.8.6 // indirect
33+
github.com/mattn/go-isatty v0.0.14 // indirect
34+
github.com/mitchellh/mapstructure v1.5.0 // indirect
35+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
36+
github.com/modern-go/reflect2 v1.0.2 // indirect
37+
github.com/pelletier/go-toml v1.9.5 // indirect
38+
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
39+
github.com/spf13/afero v1.8.2 // indirect
40+
github.com/spf13/cast v1.5.0 // indirect
41+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
42+
github.com/spf13/pflag v1.0.5 // indirect
43+
github.com/subosito/gotenv v1.3.0 // indirect
44+
github.com/ugorji/go/codec v1.2.7 // indirect
45+
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
46+
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect
47+
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
48+
golang.org/x/text v0.3.7 // indirect
49+
google.golang.org/protobuf v1.28.1 // indirect
50+
gopkg.in/ini.v1 v1.66.4 // indirect
51+
gopkg.in/yaml.v2 v2.4.0 // indirect
52+
gopkg.in/yaml.v3 v3.0.1 // indirect
53+
)

go.sum

Lines changed: 673 additions & 0 deletions
Large diffs are not rendered by default.

initializers/connectDB.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package initializers
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"gorm.io/driver/postgres"
8+
"gorm.io/gorm"
9+
)
10+
11+
var DB *gorm.DB
12+
13+
func ConnectDB(config *Config) {
14+
var err error
15+
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", config.DBHost, config.DBUserName, config.DBUserPassword, config.DBName, config.DBPort)
16+
17+
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
18+
if err != nil {
19+
log.Fatal("Failed to connect to the Database")
20+
}
21+
fmt.Println("🚀 Connected Successfully to the Database")
22+
}

initializers/loadEnv.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package initializers
2+
3+
import (
4+
"github.com/spf13/viper"
5+
)
6+
7+
type Config struct {
8+
DBHost string `mapstructure:"POSTGRES_HOST"`
9+
DBUserName string `mapstructure:"POSTGRES_USER"`
10+
DBUserPassword string `mapstructure:"POSTGRES_PASSWORD"`
11+
DBName string `mapstructure:"POSTGRES_DB"`
12+
DBPort string `mapstructure:"POSTGRES_PORT"`
13+
ServerPort string `mapstructure:"PORT"`
14+
15+
ClientOrigin string `mapstructure:"CLIENT_ORIGIN"`
16+
}
17+
18+
func LoadConfig(path string) (config Config, err error) {
19+
viper.AddConfigPath(path)
20+
viper.SetConfigType("env")
21+
viper.SetConfigName("app")
22+
23+
viper.AutomaticEnv()
24+
25+
err = viper.ReadInConfig()
26+
if err != nil {
27+
return
28+
}
29+
30+
err = viper.Unmarshal(&config)
31+
return
32+
}

main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/wpcodevo/golang-gorm-postgres/initializers"
9+
)
10+
11+
var (
12+
server *gin.Engine
13+
)
14+
15+
func init() {
16+
config, err := initializers.LoadConfig(".")
17+
if err != nil {
18+
log.Fatal("🚀 Could not load environment variables", err)
19+
}
20+
21+
initializers.ConnectDB(&config)
22+
23+
server = gin.Default()
24+
}
25+
26+
func main() {
27+
config, err := initializers.LoadConfig(".")
28+
if err != nil {
29+
log.Fatal("🚀 Could not load environment variables", err)
30+
}
31+
32+
router := server.Group("/api")
33+
router.GET("/healthchecker", func(ctx *gin.Context) {
34+
message := "Welcome to Golang with Gorm and Postgres"
35+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "message": message})
36+
})
37+
38+
log.Fatal(server.Run(":" + config.ServerPort))
39+
}

tmp/build-errors.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exit status 0xc000013aexit status 2

0 commit comments

Comments
 (0)