forked from urdt/ussd
added an init to the binaries
This commit is contained in:
parent
b625b3cf5e
commit
1a6877c350
@ -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) {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
15
cmd/main.go
15
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
|
||||
|
35
initializers/connectToDb.go
Normal file
35
initializers/connectToDb.go
Normal 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")
|
||||
}
|
||||
}
|
14
initializers/loadEnvVariables.go
Normal file
14
initializers/loadEnvVariables.go
Normal 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")
|
||||
}
|
||||
}
|
10
initializers/syncDatabase.go
Normal file
10
initializers/syncDatabase.go
Normal 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{})
|
||||
}
|
Loading…
Reference in New Issue
Block a user