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
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 {

View File

@ -4,22 +4,47 @@ import (
"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"
)
// 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) {
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))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(dbUri))
if err != nil {
fmt.Println(err)
helpers.Log("Error connecting to database", 4)

View File

@ -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;
}

View File

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