From b87bc824402640abdfb9b18644878a93dac8837d Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 18 Jul 2023 00:25:13 +0100 Subject: [PATCH] Code Refactoring --- src/dbAccess/access.go | 7 +++-- src/dbAccess/client.go | 57 +++++++++++++++++++++++++++++------------ src/helpers/config.go | 25 +----------------- src/messaging/client.go | 4 ++- 4 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/dbAccess/access.go b/src/dbAccess/access.go index b23abb6..044b8b2 100644 --- a/src/dbAccess/access.go +++ b/src/dbAccess/access.go @@ -1,12 +1,15 @@ package dbAccess import ( - "git.fjla.uk/owlboard/go-types/pkg/database" "fmt" + + "git.fjla.uk/owlboard/go-types/pkg/database" + "git.fjla.uk/owlboard/mq-client/helpers" ) func init() { fmt.Println("dbAccess/access.init() Will be used to push the component version number to the database") + fmt.Printf("Version: %s\n", helpers.Version) } func PutManyServices(collection string, data []database.Service) bool { @@ -15,4 +18,4 @@ func PutManyServices(collection string, data []database.Service) bool { func PrintFromDbPackage() { fmt.Println("hello from the dbAccess package") -} \ No newline at end of file +} diff --git a/src/dbAccess/client.go b/src/dbAccess/client.go index 7226ff0..365f7fa 100644 --- a/src/dbAccess/client.go +++ b/src/dbAccess/client.go @@ -1,30 +1,55 @@ package dbAccess import ( - "git.fjla.uk/owlboard/mq-client/helpers" + "git.fjla.uk/owlboard/mq-client/helpers" "context" - "time" "fmt" + "os" + "time" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" - // "go.mongodb.org/mongo-driver/mongo/readpref" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + // "go.mongodb.org/mongo-driver/mongo/readpref" ) +// Generate DB Url +var dbUri string = getDbUri() + +func getDbUri() string { + var dbHost string = os.Getenv("OWL_DB_HOST") + if dbHost == "" { + dbHost = "localhost" + } + var dbPort string = os.Getenv("OWL_DB_PORT") + if dbPort == "" { + dbPort = "27017" + } + var dbUser string = os.Getenv("OWL_DB_USER") + if dbUser == "" { + dbUser = "owl" + } + var dbPass string = os.Getenv("OWL_DB_PASS") + if dbPass == "" { + dbPass = "twittwoo" + } + var uri = "mongodb://" + dbUser + ":" + dbPass + "@" + dbHost + ":" + dbPort + return uri +} + // Provide the DB Connection to other functions var MongoClient (*mongo.Client) = initDataAccess() // Initialise the DB Connection -func initDataAccess() (*mongo.Client) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - client, err := mongo.Connect(ctx, options.Client().ApplyURI(helpers.DbUri)) - if err != nil { +func initDataAccess() *mongo.Client { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + client, err := mongo.Connect(ctx, options.Client().ApplyURI(dbUri)) + if err != nil { fmt.Println(err) - helpers.Log("Error connecting to database", 4) - } else { - helpers.Log("Connected to Database", 1) - } - return client -} \ No newline at end of file + helpers.Log("Error connecting to database", 4) + } else { + helpers.Log("Connected to Database", 1) + } + return client +} diff --git a/src/helpers/config.go b/src/helpers/config.go index a5dbb20..792eee3 100644 --- a/src/helpers/config.go +++ b/src/helpers/config.go @@ -1,8 +1,8 @@ package helpers import ( - "os" "fmt" + "os" ) // Version Constants @@ -12,11 +12,9 @@ const Version string = versionNum + "-" + versionSuffix // Environment Variables var runtime string = getRuntime() -var DbUri string = getDbUri() var NrUser string = "nil" var NrPass string = "" - // Functions func getRuntime() string { var runtimeEnv string = os.Getenv("runtime") @@ -26,24 +24,3 @@ func getRuntime() string { Out(fmt.Sprintf("Runtime mode: %s", runtimeEnv), 1) return runtimeEnv } - -func getDbUri() string { - var dbHost string = os.Getenv("OWL_DB_HOST") - if dbHost == "" { - dbHost = "localhost" - } - var dbPort string = os.Getenv("OWL_DB_PORT") - if dbPort == "" { - dbPort = "27017" - } - var dbUser string = os.Getenv("OWL_DB_USER") - if dbUser == "" { - dbUser = "owl" - } - var dbPass string = os.Getenv("OWL_DB_PASS") - if dbPass == "" { - dbPass = "twittwoo" - } - var uri = "mongodb://" + dbUser + ":" + dbPass + "@" + dbHost + ":" + dbPort - return uri; -} \ No newline at end of file diff --git a/src/messaging/client.go b/src/messaging/client.go index 5a9875f..5ef9519 100644 --- a/src/messaging/client.go +++ b/src/messaging/client.go @@ -1 +1,3 @@ -package messaging \ No newline at end of file +package messaging + +// Register against the MQ Server and log each message for testing purposes