From 1a6877c35075ed70ed22ee9fd34994a99a02ebd6 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 14 Oct 2024 14:38:12 +0300 Subject: [PATCH] added an init to the binaries --- cmd/africastalking/main.go | 15 ++++++++++++++ cmd/async/main.go | 15 ++++++++++++++ cmd/http/main.go | 15 ++++++++++++++ cmd/main.go | 15 ++++++++++++++ initializers/connectToDb.go | 35 ++++++++++++++++++++++++++++++++ initializers/loadEnvVariables.go | 14 +++++++++++++ initializers/syncDatabase.go | 10 +++++++++ 7 files changed, 119 insertions(+) create mode 100644 initializers/connectToDb.go create mode 100644 initializers/loadEnvVariables.go create mode 100644 initializers/syncDatabase.go diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index c24c4b1..2cbb285 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -19,6 +19,9 @@ import ( "git.grassecon.net/urdt/ussd/internal/handlers" httpserver "git.grassecon.net/urdt/ussd/internal/http" "git.grassecon.net/urdt/ussd/internal/storage" + + "git.grassecon.net/urdt/ussd/initializers" + "github.com/joho/godotenv" ) var ( @@ -26,6 +29,18 @@ var ( scriptDir = path.Join("services", "registration") ) +func init() { + initializers.LoadEnvVariables() + initializers.ConnectToDb() + initializers.SyncDatabase() + + err := godotenv.Load() + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) + } +} + type atRequestParser struct{} func (arp *atRequestParser) GetSessionId(rq any) (string, error) { diff --git a/cmd/async/main.go b/cmd/async/main.go index 09236fd..06bb15b 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -15,6 +15,9 @@ import ( "git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/storage" + + "git.grassecon.net/urdt/ussd/initializers" + "github.com/joho/godotenv" ) var ( @@ -22,6 +25,18 @@ var ( scriptDir = path.Join("services", "registration") ) +func init() { + initializers.LoadEnvVariables() + initializers.ConnectToDb() + initializers.SyncDatabase() + + err := godotenv.Load() + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) + } +} + type asyncRequestParser struct { sessionId string input []byte diff --git a/cmd/http/main.go b/cmd/http/main.go index 6b868ed..900404a 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -18,6 +18,9 @@ import ( "git.grassecon.net/urdt/ussd/internal/handlers" httpserver "git.grassecon.net/urdt/ussd/internal/http" "git.grassecon.net/urdt/ussd/internal/storage" + + "github.com/joho/godotenv" + "git.grassecon.net/urdt/ussd/initializers" ) var ( @@ -25,6 +28,18 @@ var ( scriptDir = path.Join("services", "registration") ) +func init() { + initializers.LoadEnvVariables() + initializers.ConnectToDb() + initializers.SyncDatabase() + + err := godotenv.Load() + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) + } +} + func main() { var dbDir string var resourceDir string diff --git a/cmd/main.go b/cmd/main.go index 9db5e0a..f5e6de6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,6 +12,9 @@ import ( "git.defalsify.org/vise.git/resource" "git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/storage" + + "github.com/joho/godotenv" + "git.grassecon.net/urdt/ussd/initializers" ) var ( @@ -19,6 +22,18 @@ var ( scriptDir = path.Join("services", "registration") ) +func init() { + initializers.LoadEnvVariables() + initializers.ConnectToDb() + initializers.SyncDatabase() + + err := godotenv.Load() + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) + } +} + func main() { var dbDir string var size uint diff --git a/initializers/connectToDb.go b/initializers/connectToDb.go new file mode 100644 index 0000000..33fd980 --- /dev/null +++ b/initializers/connectToDb.go @@ -0,0 +1,35 @@ +package initializers + +import ( + "os" + + "github.com/joho/godotenv" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func ConnectToDb() { + var err error + + // Load the .env file + err = godotenv.Load() + if err != nil { + panic("Error loading .env file") + } + + dsn := "host=" + os.Getenv("DB_HOST") + + " user=" + os.Getenv("DB_USER") + + " password=" + os.Getenv("DB_PASSWORD") + + " dbname=" + os.Getenv("DB_NAME") + + " port=" + os.Getenv("DB_PORT") + + " sslmode=" + os.Getenv("DB_SSLMODE") + + " TimeZone=" + os.Getenv("DB_TIMEZONE") + + DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) + + if err != nil { + panic("Failed to connect to DB") + } +} diff --git a/initializers/loadEnvVariables.go b/initializers/loadEnvVariables.go new file mode 100644 index 0000000..150e700 --- /dev/null +++ b/initializers/loadEnvVariables.go @@ -0,0 +1,14 @@ +package initializers + +import ( + "log" + + "github.com/joho/godotenv" +) + +func LoadEnvVariables() { + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } +} \ No newline at end of file diff --git a/initializers/syncDatabase.go b/initializers/syncDatabase.go new file mode 100644 index 0000000..133d5bb --- /dev/null +++ b/initializers/syncDatabase.go @@ -0,0 +1,10 @@ +package initializers + +import "git.grassecon.net/urdt/ussd/internal/models" + +func SyncDatabase() { + DB.AutoMigrate(&models.User{}) + DB.AutoMigrate(&models.ActiveVoucher{}) + DB.AutoMigrate(&models.Voucher{}) + DB.AutoMigrate(&models.Transaction{}) +}