Code Refactoring
This commit is contained in:
parent
a6253f6cf5
commit
b87bc82440
@ -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")
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
helpers.Log("Error connecting to database", 4)
|
||||
} else {
|
||||
helpers.Log("Connected to Database", 1)
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
@ -1 +1,3 @@
|
||||
package messaging
|
||||
package messaging
|
||||
|
||||
// Register against the MQ Server and log each message for testing purposes
|
||||
|
Loading…
Reference in New Issue
Block a user