From e42885549e4bdd5501c59769bd7ae5881f7c42ce Mon Sep 17 00:00:00 2001 From: siddhantprateek Date: Sun, 20 Aug 2023 17:36:29 +0530 Subject: [PATCH] ft(db): auth svc db conn and svc discovery added --- authentication/db/connect.go | 6 +++--- authentication/main.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/authentication/db/connect.go b/authentication/db/connect.go index c4ab3bc..77dcbc2 100644 --- a/authentication/db/connect.go +++ b/authentication/db/connect.go @@ -16,7 +16,7 @@ func GetDBInstance() *gorm.DB { return DB } -func PGConnect() { +func PGConnect() error { postgresURI := config.GetEnvConfig("POSTGRES_URI") db, err := gorm.Open(postgres.Open(postgresURI), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), @@ -29,8 +29,8 @@ func PGConnect() { // Migrate the schema to Database log.Println("Running migrations...") - db.AutoMigrate(&models.User{}) + err = db.AutoMigrate(&models.User{}) DB = db - + return err } diff --git a/authentication/main.go b/authentication/main.go index 3799656..0da64dd 100644 --- a/authentication/main.go +++ b/authentication/main.go @@ -1,16 +1,39 @@ package main import ( + db "landate/authentication/db" auth_api "landate/authentication/routes" + + config "landate/config" + consul "landate/consul" "log" + "strconv" +) + +var ( + svcId = "auth_id" + svcName = "auth_svc" + envPORT = "AUTH_SERVICE_PORT" + svcTag = "auth" ) // This Service contain User Authentication information // and Its Profile Information func main() { + if err := db.PGConnect(); err != nil { + log.Fatal("Unable to connect auth-svc to database.") + } + + svc_port, err := strconv.Atoi(config.GetEnvConfig(envPORT)) + if err != nil { + log.Fatal(err) + } + auth_svc := consul.NewService(svcId, svcName, svcTag, svc_port) + if err := auth_api.Init(); err != nil { log.Fatalln("Unable to start auth service.") } + auth_svc.DeregisterService() }