Skip to content

Commit

Permalink
♻️ refactor: updated codebase #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Nov 29, 2023
1 parent dc1fa45 commit 588b067
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

# Go workspace file
go.work
Expand Down
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.PHONY: run build test tidy deps-upgrade deps-clean-cache

# ==============================================================================
# Start Rest
run:
go run main.go

build:
go build main.go

# ==============================================================================
# Modules support
test:
go test -cover ./...

tidy:
go mod tidy
go mod vendor

deps-upgrade:
# go get $(go list -f '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}' -m all)
go get -u -t -d -v ./...
go mod tidy
go mod vendor

deps-clean-cache:
go clean -modcache
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sivaosorg/govm v1.2.3 h1:Hwt6SQjwW5XADWqvfwzlf3t9kr5seoPpMk8EE9MO+6I=
github.com/sivaosorg/govm v1.2.3/go.mod h1:rXfPCNGc4ddPf1+VRX8Ytw/5xqehfPRrCr53Oi+cwpw=
github.com/sivaosorg/govm v1.2.6 h1:BCUCNk/yy/rFZ+clMF7ftfw99ZQFO+4QSFV/xe2mWfA=
github.com/sivaosorg/govm v1.2.6/go.mod h1:rXfPCNGc4ddPf1+VRX8Ytw/5xqehfPRrCr53Oi+cwpw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
12 changes: 8 additions & 4 deletions postgresconn/postgresconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgresconn
import (
"context"
"fmt"
"os"

"github.com/jmoiron/sqlx"
"github.com/sivaosorg/govm/callback"
Expand Down Expand Up @@ -58,7 +59,8 @@ func NewClient(config postgres.PostgresConfig) (*Postgres, dbx.Dbx) {
s.SetConnected(false).
SetMessage("Postgres unavailable").
SetError(fmt.Errorf(s.Message))
return &Postgres{}, *s
instance = NewPostgres().SetState(*s)
return instance, *s
}
if instance != nil {
s.SetConnected(true)
Expand All @@ -72,22 +74,24 @@ func NewClient(config postgres.PostgresConfig) (*Postgres, dbx.Dbx) {
client, err := sqlx.Open(common.EntryKeyPostgres, stringConn)
if err != nil {
s.SetError(err).SetConnected(false).SetMessage(err.Error())
return nil, *s
instance = NewPostgres().SetState(*s)
return instance, *s
}
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout)
defer cancel()
err = client.PingContext(ctx)
if err != nil {
s.SetError(err).SetConnected(false).SetMessage(err.Error())
return nil, *s
instance = NewPostgres().SetState(*s)
return instance, *s
}
if config.DebugMode {
_logger.Info(fmt.Sprintf("Connected successfully to postgres database %s:%d/%s", config.Host, config.Port, config.Database))
}
client.SetMaxIdleConns(config.MaxIdleConn)
client.SetMaxOpenConns(config.MaxOpenConn)
instance = NewPostgres().SetConn(client)
s.SetConnected(true).SetMessage("Connection established").SetNewInstance(true)
s.SetConnected(true).SetMessage("Connection established").SetNewInstance(true).SetPid(os.Getpid())
if config.DebugMode {
callback.MeasureTime(func() {
pid, err := GetPidConn(instance)
Expand Down

0 comments on commit 588b067

Please sign in to comment.