Code Refactoring

This commit is contained in:
Fred Boniface 2023-07-18 00:25:13 +01:00
parent a6253f6cf5
commit b87bc82440
4 changed files with 50 additions and 43 deletions

View File

@ -1,12 +1,15 @@
package dbAccess package dbAccess
import ( import (
"git.fjla.uk/owlboard/go-types/pkg/database"
"fmt" "fmt"
"git.fjla.uk/owlboard/go-types/pkg/database"
"git.fjla.uk/owlboard/mq-client/helpers"
) )
func init() { func init() {
fmt.Println("dbAccess/access.init() Will be used to push the component version number to the database") 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 { func PutManyServices(collection string, data []database.Service) bool {
@ -15,4 +18,4 @@ func PutManyServices(collection string, data []database.Service) bool {
func PrintFromDbPackage() { func PrintFromDbPackage() {
fmt.Println("hello from the dbAccess package") fmt.Println("hello from the dbAccess package")
} }

View File

@ -1,30 +1,55 @@
package dbAccess package dbAccess
import ( import (
"git.fjla.uk/owlboard/mq-client/helpers" "git.fjla.uk/owlboard/mq-client/helpers"
"context" "context"
"time"
"fmt" "fmt"
"os"
"time"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
// "go.mongodb.org/mongo-driver/mongo/readpref" // "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 // Provide the DB Connection to other functions
var MongoClient (*mongo.Client) = initDataAccess() var MongoClient (*mongo.Client) = initDataAccess()
// Initialise the DB Connection // Initialise the DB Connection
func initDataAccess() (*mongo.Client) { func initDataAccess() *mongo.Client {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(helpers.DbUri)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(dbUri))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
helpers.Log("Error connecting to database", 4) helpers.Log("Error connecting to database", 4)
} else { } else {
helpers.Log("Connected to Database", 1) helpers.Log("Connected to Database", 1)
} }
return client return client
} }

View File

@ -1,8 +1,8 @@
package helpers package helpers
import ( import (
"os"
"fmt" "fmt"
"os"
) )
// Version Constants // Version Constants
@ -12,11 +12,9 @@ const Version string = versionNum + "-" + versionSuffix
// Environment Variables // Environment Variables
var runtime string = getRuntime() var runtime string = getRuntime()
var DbUri string = getDbUri()
var NrUser string = "nil" var NrUser string = "nil"
var NrPass string = "" var NrPass string = ""
// Functions // Functions
func getRuntime() string { func getRuntime() string {
var runtimeEnv string = os.Getenv("runtime") var runtimeEnv string = os.Getenv("runtime")
@ -26,24 +24,3 @@ func getRuntime() string {
Out(fmt.Sprintf("Runtime mode: %s", runtimeEnv), 1) Out(fmt.Sprintf("Runtime mode: %s", runtimeEnv), 1)
return runtimeEnv 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;
}

View File

@ -1 +1,3 @@
package messaging package messaging
// Register against the MQ Server and log each message for testing purposes