-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4698cac
commit cdff00e
Showing
5 changed files
with
119 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package pgkit | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/NTHU-LSALAB/NTHU-Distributed-System/pkg/logkit" | ||
"github.com/go-pg/pg/v10" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type PGConfig struct { | ||
URL string `long:"url" env:"URL" description:"the URL of PostgreSQL" required:"true"` | ||
} | ||
|
||
type PGClient struct { | ||
*pg.DB | ||
closeFunc func() | ||
} | ||
|
||
func (c *PGClient) Close() error { | ||
if c.closeFunc != nil { | ||
c.closeFunc() | ||
} | ||
return c.DB.Close() | ||
} | ||
|
||
func NewPGClient(ctx context.Context, conf *PGConfig) *PGClient { | ||
if url := os.ExpandEnv(conf.URL); url != "" { | ||
conf.URL = url | ||
} | ||
|
||
logger := logkit.FromContext(ctx).With(zap.String("url", conf.URL)) | ||
opts, err := pg.ParseURL(conf.URL) | ||
if err != nil { | ||
logger.Fatal("failed to parse PostgreSQL url", zap.Error(err)) | ||
} | ||
|
||
db := pg.Connect(opts).WithContext(ctx) | ||
if err := db.Ping(ctx); err != nil { | ||
logger.Fatal("failed to ping PostgreSQL", zap.Error(err)) | ||
} | ||
|
||
logger.Info("create PostgreSQL client successfully") | ||
|
||
return &PGClient{ | ||
DB: db, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package pgkit | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/NTHU-LSALAB/NTHU-Distributed-System/pkg/logkit" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("PGClient", func() { | ||
Describe("NewPGClient", func() { | ||
var ( | ||
ctx context.Context | ||
pgConf *PGConfig | ||
pgClient *PGClient | ||
) | ||
|
||
BeforeEach(func() { | ||
ctx = logkit.NewLogger(&logkit.LoggerConfig{ | ||
Development: true, | ||
}).WithContext(context.Background()) | ||
|
||
pgConf = &PGConfig{ | ||
URL: "postgres://postgres@postgres:5432/postgres?sslmode=disable", | ||
} | ||
if url := os.Getenv("POSTGRES_URL"); url != "" { | ||
pgConf.URL = url | ||
} | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
pgClient = NewPGClient(ctx, pgConf) | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(pgClient.Close()).NotTo(HaveOccurred()) | ||
}) | ||
|
||
When("success", func() { | ||
It("returns new PGClient without error", func() { | ||
Expect(pgClient).NotTo(BeNil()) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package pgkit | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestPGKit(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Test PG Kit") | ||
} |