rookie-ninja / rk-db

Enterprise level database bootstrapper with YAML based on rk-entry in Golang
Apache License 2.0
5 stars 4 forks source link

Failed to get Postgres entry using rk-boot in Golang #6

Open premier213 opened 5 months ago

premier213 commented 5 months ago

I'm developing a gRPC server in Go using rookie-ninja/rk-boot, rookie-ninja/rk-db/postgres, and rookie-ninja/rk-grpc/boot. I'm trying to connect to a Postgres database, but I'm encountering the error: "failed to get Postgres entry".

Configuration

Below is my configuration for the Postgres database:

postgres:
  - name: user
    enabled: true
    domain: "*"
    addr: "192.168.8.104:5432"
    user: postgres
    pass: 12345678
    database:
      - name: webchat
        autoCreate: true

Code

Here is the relevant part of my code:

package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "time"

    greeter "github.com/chater-co/web-chat/api/gen/v1"
    rkboot "github.com/rookie-ninja/rk-boot"
    rkpostgres "github.com/rookie-ninja/rk-db/postgres"
    rkgrpc "github.com/rookie-ninja/rk-grpc/boot"
    "google.golang.org/grpc"
    "google.golang.org/grpc/grpclog"
    "gorm.io/gorm"
)

var userDb *gorm.DB

func init() {
    grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stdout, os.Stdout, os.Stdout))
}

func main() {
    boot := rkboot.NewBoot()
    boot.Bootstrap(context.TODO())

    pgEntry := rkpostgres.GetPostgresEntry("user")
    if pgEntry == nil {
        log.Fatalf("failed to get Postgres entry")
    }
    userDb = pgEntry.GetDB("webchat")
    if !userDb.DryRun {
        userDb.AutoMigrate(&User{})
    }

    grpcEntry := rkgrpc.GetGrpcEntry("chat-app")
    grpcEntry.AddRegFuncGrpc(registerGreeter)
    grpcEntry.AddRegFuncGw(greeter.RegisterGreeterHandlerFromEndpoint)
    boot.Bootstrap(context.Background())
    boot.WaitForShutdownSig(context.TODO())
}

func registerGreeter(server *grpc.Server) {
    greeter.RegisterGreeterServer(server, &GreeterServer{})
}

type GreeterServer struct{}

type Base struct {
    CreatedAt time.Time      `yaml:"-" json:"-"`
    UpdatedAt time.Time      `yaml:"-" json:"-"`
    DeletedAt gorm.DeletedAt `yaml:"-" json:"-" gorm:"index"`
}

type User struct {
    Base
    Id   int    `yaml:"id" json:"id" gorm:"primaryKey"`
    Name string `yaml:"name" json:"name"`
}

func (server *GreeterServer) Greeter(ctx context.Context, request *greeter.GreeterRequest) (*greeter.GreeterResponse, error) {
    return &greeter.GreeterResponse{
        Message: fmt.Sprintf("Hello %s!", request.Name),
    }, nil
}

What I've Tried

Double-checked the YAML configuration for errors.

Verified that the database server is running and accessible at 192.168.8.104:5432.

Checked for any network issues between my application and the database server.

Expected Behavior

The application should successfully connect to the Postgres database and perform auto-migration for the User table.

Actual Behavior

The application logs the error: "failed to get Postgres entry" and exits.

Additional Information

Any help or guidance on what might be causing this issue would be greatly appreciated.

sqweso commented 1 week ago

same here