Skip to content

Commit 28363b0

Browse files
committed
wip(api): use postgres
1 parent a59a167 commit 28363b0

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

api/server.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/labstack/echo/v4"
99
"github.com/shellhub-io/shellhub/api/routes"
1010
"github.com/shellhub-io/shellhub/api/services"
11-
"github.com/shellhub-io/shellhub/api/store/mongo"
12-
"github.com/shellhub-io/shellhub/api/store/mongo/options"
11+
"github.com/shellhub-io/shellhub/api/store/pg"
12+
"github.com/shellhub-io/shellhub/api/store/pg/options"
1313
"github.com/shellhub-io/shellhub/pkg/api/internalclient"
1414
"github.com/shellhub-io/shellhub/pkg/cache"
1515
"github.com/shellhub-io/shellhub/pkg/geoip/geolite2"
@@ -83,14 +83,13 @@ func (s *Server) Setup(ctx context.Context) error {
8383

8484
log.Debug("Redis cache initialized successfully")
8585

86-
store, err := mongo.NewStore(ctx, s.env.MongoURI, cache, options.RunMigatrions)
86+
uri := pg.URI(s.env.PostgresHost, s.env.PostgresPort, s.env.PostgresUser, s.env.PostgresPassword, s.env.PostgresDB)
87+
store, err := pg.New(ctx, uri, options.Migrate())
8788
if err != nil {
88-
log.
89-
WithError(err).
90-
Fatal("failed to create the store")
89+
return err
9190
}
9291

93-
log.Debug("MongoDB store connected successfully")
92+
log.Debug("PostgreSQL store connected successfully")
9493

9594
apiClient, err := internalclient.NewClient(internalclient.WithAsynqWorker(s.env.RedisURI))
9695
if err != nil {

api/store/pg/pg.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
type pg struct {
17-
db *bun.DB
17+
driver *bun.DB
1818
}
1919

2020
func URI(host, port, user, password, db string) string {
@@ -34,10 +34,16 @@ func New(ctx context.Context, uri string, opts ...options.Option) (store.Store,
3434
return nil, err
3535
}
3636

37-
pg := &pg{db: bun.NewDB(stdlib.OpenDBFromPool(pool), pgdialect.New())}
38-
if err := pg.db.Ping(); err != nil {
37+
pg := &pg{driver: bun.NewDB(stdlib.OpenDBFromPool(pool), pgdialect.New())}
38+
if err := pg.driver.Ping(); err != nil {
3939
return nil, err
4040
}
4141

42+
for _, opt := range opts {
43+
if err := opt(ctx, pg.driver.DB); err != nil {
44+
return nil, err
45+
}
46+
}
47+
4248
return pg, nil
4349
}

0 commit comments

Comments
 (0)