Compare commits

...

4 Commits

Author SHA1 Message Date
1a6877c350
added an init to the binaries 2024-10-14 14:38:12 +03:00
b625b3cf5e
added models 2024-10-14 14:33:33 +03:00
27895bf211
added uuid and env packages 2024-10-14 14:32:36 +03:00
ba6c95e347
added env example file 2024-10-14 11:23:40 +03:00
14 changed files with 276 additions and 0 deletions

17
.env.example Normal file
View File

@ -0,0 +1,17 @@
#Serve Http
PORT=7123
HOST=127.0.0.1
#PostgreSQL
DB_HOST=localhost
DB_USER=postgres
DB_PASSWORD=strongpass
DB_NAME=urdt_ussd
DB_PORT=5432
DB_SSLMODE=disable
DB_TIMEZONE=Africa/Nairobi
#External API Calls
CREATE_ACCOUNT_URL="https://custodial.sarafu.africa/api/account/create"
TRACK_STATUS_URL="https://custodial.sarafu.africa/api/track/"
BALANCE_URL="https://custodial.sarafu.africa/api/account/status/"

View File

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

View File

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

View File

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

View File

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

16
go.mod
View File

@ -9,18 +9,34 @@ require (
gopkg.in/leonelquinteros/gotext.v1 v1.3.1
)
require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.14.0 // indirect
gorm.io/gorm v1.25.12 // indirect
)
require (
github.com/alecthomas/participle/v2 v2.0.0 // indirect
github.com/alecthomas/repr v0.2.0 // indirect
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/joho/godotenv v1.5.1
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0
github.com/x448/float16 v0.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.5.9
)

33
go.sum
View File

@ -8,29 +8,62 @@ github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c h1:H9Nm+I7Cg/YVPpEV1RzU3Wq2pjamPc/UtHDgItcb7lE=
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c/go.mod h1:rGod7o6KPeJ+hyBpHfhi4v7blx9sf+QsHsA7KAsdN6U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qfbBF8gh7Vsbuz+cVmhf5kt6pE9bYYyLo=
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a h1:0Q3H0YXzMHiciXtRcM+j0jiCe8WKPQHoRgQiRTnfcLY=
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a/go.mod h1:CdTTBOYzS5E4mWS1N8NWP6AHI19MP0A2B18n3hLzRMk=
github.com/peteole/testdata-loader v0.3.0 h1:8jckE9KcyNHgyv/VPoaljvKZE0Rqr8+dPVYH6rfNr9I=
github.com/peteole/testdata-loader v0.3.0/go.mod h1:Mt0ZbRtb56u8SLJpNP+BnQbENljMorYBpqlvt3cS83U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/leonelquinteros/gotext.v1 v1.3.1 h1:8d9/fdTG0kn/B7NNGV1BsEyvektXFAbkMsTZS2sFSCc=
gopkg.in/leonelquinteros/gotext.v1 v1.3.1/go.mod h1:X1WlGDeAFIYsW6GjgMm4VwUwZ2XjI7Zan2InxSUQWrU=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

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

View File

@ -0,0 +1,17 @@
package models
import (
"time"
"github.com/gofrs/uuid"
"gorm.io/gorm"
)
type ActiveVoucher struct {
gorm.Model
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()"`
UserID uuid.UUID
VoucherID uuid.UUID
CreatedAt time.Time
UpdatedAt time.Time
}

View File

@ -0,0 +1,21 @@
package models
import (
"time"
"github.com/gofrs/uuid"
"gorm.io/gorm"
)
type Transaction struct {
gorm.Model
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()"`
UserID uuid.UUID
VoucherID uuid.UUID
Amount string
Sender string
Receiver string
Status string
CreatedAt time.Time
UpdatedAt time.Time
}

View File

@ -0,0 +1,31 @@
package models
import (
"time"
"github.com/gofrs/uuid"
"gorm.io/gorm"
)
type User struct {
gorm.Model
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()"`
SessionID string `gorm:"unique"`
PublicKey string
CustodialID string
TrackingID string
FirstName string
FamilyName string
Offerings string
Gender string
YOB string
Location string
Status string
AccountPIN string
TemporaryPIN string
ActiveVouchers []ActiveVoucher `gorm:"foreignKey:UserID"`
Transactions []Transaction `gorm:"foreignKey:UserID"`
Vouchers []Voucher `gorm:"foreignKey:UserID"`
CreatedAt time.Time
UpdatedAt time.Time
}

View File

@ -0,0 +1,22 @@
package models
import (
"time"
"github.com/gofrs/uuid"
"gorm.io/gorm"
)
type Voucher struct {
gorm.Model
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()"`
UserID uuid.UUID
ContractAddress string
TokenSymbol string
TokenDecimals int
Balance float64
ActiveVouchers []ActiveVoucher `gorm:"foreignKey:VoucherID"`
Transactions []Transaction `gorm:"foreignKey:VoucherID"`
CreatedAt time.Time
UpdatedAt time.Time
}