added an init to the binaries

This commit is contained in:
Alfred Kamanda 2024-10-14 14:38:12 +03:00
parent b625b3cf5e
commit 1a6877c350
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703
7 changed files with 119 additions and 0 deletions

View File

@ -19,6 +19,9 @@ import (
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
httpserver "git.grassecon.net/urdt/ussd/internal/http" httpserver "git.grassecon.net/urdt/ussd/internal/http"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
"git.grassecon.net/urdt/ussd/initializers"
"github.com/joho/godotenv"
) )
var ( var (
@ -26,6 +29,18 @@ var (
scriptDir = path.Join("services", "registration") 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{} type atRequestParser struct{}
func (arp *atRequestParser) GetSessionId(rq any) (string, error) { func (arp *atRequestParser) GetSessionId(rq any) (string, error) {

View File

@ -15,6 +15,9 @@ import (
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
"git.grassecon.net/urdt/ussd/initializers"
"github.com/joho/godotenv"
) )
var ( var (
@ -22,6 +25,18 @@ var (
scriptDir = path.Join("services", "registration") 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 { type asyncRequestParser struct {
sessionId string sessionId string
input []byte input []byte

View File

@ -18,6 +18,9 @@ import (
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
httpserver "git.grassecon.net/urdt/ussd/internal/http" httpserver "git.grassecon.net/urdt/ussd/internal/http"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
"github.com/joho/godotenv"
"git.grassecon.net/urdt/ussd/initializers"
) )
var ( var (
@ -25,6 +28,18 @@ var (
scriptDir = path.Join("services", "registration") 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() { func main() {
var dbDir string var dbDir string
var resourceDir string var resourceDir string

View File

@ -12,6 +12,9 @@ import (
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
"github.com/joho/godotenv"
"git.grassecon.net/urdt/ussd/initializers"
) )
var ( var (
@ -19,6 +22,18 @@ var (
scriptDir = path.Join("services", "registration") 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() { func main() {
var dbDir string var dbDir string
var size uint var size uint

View File

@ -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")
}
}

View File

@ -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")
}
}

View File

@ -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{})
}