Skip to content

Commit

Permalink
fix: db error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Feb 23, 2024
1 parent ab66d6e commit e27ba79
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
15 changes: 15 additions & 0 deletions node/pkg/admin/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package utils

import (
"context"
"errors"
"fmt"
"log"
"os"
"runtime/debug"
"strings"

"bisonai.com/orakl/node/pkg/db"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
Expand All @@ -17,6 +20,18 @@ func Setup(version string) (*fiber.App, error) {
if version == "" {
version = "test"
}

ctx := context.Background()
_, err := db.GetPool(ctx)
if err != nil {
return nil, errors.New("error getting db pool")
}

_, err = db.GetRedisConn(ctx)
if err != nil {
return nil, errors.New("error getting redis conn")
}

app := fiber.New(fiber.Config{
AppName: "Node API " + version,
EnablePrintRoutes: true,
Expand Down
13 changes: 8 additions & 5 deletions node/pkg/db/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ func GetPool(ctx context.Context) (*pgxpool.Pool, error) {

func getPool(ctx context.Context, once *sync.Once) (*pgxpool.Pool, error) {
var err error

connectionString := loadPgsqlConnectionString()
if connectionString == "" {
err = errors.New("DATABASE_URL is not set")
return nil, err
}

once.Do(func() {
connectionString := loadPgsqlConnectionString()
if connectionString == "" {
err = errors.New("DATABASE_URL is not set")
return
}
pool, err = connectToPgsql(ctx, connectionString)
})

return pool, err
}

Expand Down
13 changes: 5 additions & 8 deletions node/pkg/db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ func GetRedisConn(ctx context.Context) (*redis.Conn, error) {

func getRedisConn(ctx context.Context, once *sync.Once) (*redis.Conn, error) {
var err error
once.Do(func() {
rdb, err = connectRdb(ctx)
})
return rdb, err

}

func connectRdb(ctx context.Context) (*redis.Conn, error) {
connectionInfo, err := loadRedisConnectionString()
if err != nil {
return nil, err
}
return connectToRedis(ctx, connectionInfo)

once.Do(func() {
rdb, err = connectToRedis(ctx, connectionInfo)
})
return rdb, err

}

Expand Down

0 comments on commit e27ba79

Please sign in to comment.