forked from urdt/ussd
Compare commits
19 Commits
lash/stale
...
http-logs
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a02ffcf0c | ||
|
|
dcd8fce59a
|
||
|
|
64a7b49218
|
||
|
|
1bcbb2079e
|
||
|
|
e63468433e
|
||
|
|
9c972ffa6b
|
||
|
|
a11776e1b3
|
||
|
|
6ac9ac29d8
|
||
| 46b2b354fd | |||
|
|
7676cfd40c
|
||
|
|
4e350aa25a
|
||
| 859de0513a | |||
|
|
266d3d06c3
|
||
|
|
92ea3df4aa
|
||
|
|
c46c31ea36
|
||
|
|
da91eed9d4
|
||
|
|
2b865a365b
|
||
|
|
c77558689a
|
||
|
|
a9641fd70d
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,3 +6,4 @@ go.work*
|
|||||||
cmd/.state/
|
cmd/.state/
|
||||||
id_*
|
id_*
|
||||||
*.gdbm
|
*.gdbm
|
||||||
|
*.log
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -27,10 +31,26 @@ import (
|
|||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla()
|
logg = logging.NewVanilla()
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
|
InfoLogger *log.Logger
|
||||||
|
ErrorLogger *log.Logger
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initializers.LoadEnvVariables()
|
initializers.LoadEnvVariables()
|
||||||
|
|
||||||
|
logFile := "urdt-ussd-africastalking.log"
|
||||||
|
|
||||||
|
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
|
// Inject into remote package
|
||||||
|
remote.InfoLogger = InfoLogger
|
||||||
|
remote.ErrorLogger = ErrorLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
type atRequestParser struct{}
|
type atRequestParser struct{}
|
||||||
@@ -38,9 +58,30 @@ type atRequestParser struct{}
|
|||||||
func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
||||||
rqv, ok := rq.(*http.Request)
|
rqv, ok := rq.(*http.Request)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
ErrorLogger.Println("got an invalid request:", rq)
|
||||||
return "", handlers.ErrInvalidRequest
|
return "", handlers.ErrInvalidRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Capture body (if any) for logging
|
||||||
|
body, err := io.ReadAll(rqv.Body)
|
||||||
|
if err != nil {
|
||||||
|
ErrorLogger.Println("failed to read request body:", err)
|
||||||
|
return "", fmt.Errorf("failed to read request body: %v", err)
|
||||||
|
}
|
||||||
|
// Reset the body for further reading
|
||||||
|
rqv.Body = io.NopCloser(bytes.NewReader(body))
|
||||||
|
|
||||||
|
// Log the body as JSON
|
||||||
|
bodyLog := map[string]string{"body": string(body)}
|
||||||
|
logBytes, err := json.Marshal(bodyLog)
|
||||||
|
if err != nil {
|
||||||
|
ErrorLogger.Println("failed to marshal request body:", err)
|
||||||
|
} else {
|
||||||
|
InfoLogger.Println("Received request:", string(logBytes))
|
||||||
|
}
|
||||||
|
|
||||||
if err := rqv.ParseForm(); err != nil {
|
if err := rqv.ParseForm(); err != nil {
|
||||||
|
ErrorLogger.Println("failed to parse form data: %v", err)
|
||||||
return "", fmt.Errorf("failed to parse form data: %v", err)
|
return "", fmt.Errorf("failed to parse form data: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
@@ -23,12 +24,27 @@ import (
|
|||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla()
|
logg = logging.NewVanilla()
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
|
InfoLogger *log.Logger
|
||||||
|
ErrorLogger *log.Logger
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initializers.LoadEnvVariables()
|
initializers.LoadEnvVariables()
|
||||||
}
|
|
||||||
|
|
||||||
|
logFile := "urdt-ussd-async.log"
|
||||||
|
|
||||||
|
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
|
// Inject into remote package
|
||||||
|
remote.InfoLogger = InfoLogger
|
||||||
|
remote.ErrorLogger = ErrorLogger
|
||||||
|
}
|
||||||
type asyncRequestParser struct {
|
type asyncRequestParser struct {
|
||||||
sessionId string
|
sessionId string
|
||||||
input []byte
|
input []byte
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -26,10 +27,26 @@ import (
|
|||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla()
|
logg = logging.NewVanilla()
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
|
InfoLogger *log.Logger
|
||||||
|
ErrorLogger *log.Logger
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initializers.LoadEnvVariables()
|
initializers.LoadEnvVariables()
|
||||||
|
|
||||||
|
logFile := "urdt-ussd-http.log"
|
||||||
|
|
||||||
|
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
|
// Inject into remote package
|
||||||
|
remote.InfoLogger = InfoLogger
|
||||||
|
remote.ErrorLogger = ErrorLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
17
cmd/main.go
17
cmd/main.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
@@ -20,10 +21,26 @@ import (
|
|||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla()
|
logg = logging.NewVanilla()
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
|
InfoLogger *log.Logger
|
||||||
|
ErrorLogger *log.Logger
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initializers.LoadEnvVariables()
|
initializers.LoadEnvVariables()
|
||||||
|
|
||||||
|
logFile := "urdt-ussd-cli.log"
|
||||||
|
|
||||||
|
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
|
// Inject into remote package
|
||||||
|
remote.InfoLogger = InfoLogger
|
||||||
|
remote.ErrorLogger = ErrorLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -5,14 +5,14 @@ go 1.23.0
|
|||||||
toolchain go1.23.2
|
toolchain go1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed
|
git.defalsify.org/vise.git v0.2.1-0.20241017112704-307fa6fcdc6b
|
||||||
github.com/alecthomas/assert/v2 v2.2.2
|
github.com/alecthomas/assert/v2 v2.2.2
|
||||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
||||||
github.com/peteole/testdata-loader v0.3.0
|
github.com/peteole/testdata-loader v0.3.0
|
||||||
gopkg.in/leonelquinteros/gotext.v1 v1.3.1
|
gopkg.in/leonelquinteros/gotext.v1 v1.3.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/grassrootseconomics/ussd-data-service v0.0.0-20241003123429-4904b4438a3a
|
require github.com/grassrootseconomics/ussd-data-service v0.0.0-20241003123429-4904b4438a3a // indirect
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
|||||||
git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed h1:4TrsfbK7NKgsa7KjMPlnV/tjYTkAAXP5PWAZzUfzCdI=
|
git.defalsify.org/vise.git v0.2.1-0.20241017112704-307fa6fcdc6b h1:dxBplsIlzJHV+5EH+gzB+w08Blt7IJbb2jeRe1OEjLU=
|
||||||
git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
git.defalsify.org/vise.git v0.2.1-0.20241017112704-307fa6fcdc6b/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
||||||
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
|
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
|
||||||
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
|
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
|
||||||
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=
|
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=
|
||||||
|
|||||||
@@ -1254,7 +1254,8 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by
|
|||||||
// Fetch vouchers from the API using the public key
|
// Fetch vouchers from the API using the public key
|
||||||
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return if there is no voucher
|
// Return if there is no voucher
|
||||||
|
|||||||
@@ -10,10 +10,6 @@ const (
|
|||||||
DATATYPE_USERSUB = 64
|
DATATYPE_USERSUB = 64
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
SUBPREFIX_TIME = uint16(1)
|
|
||||||
)
|
|
||||||
|
|
||||||
// PrefixDb interface abstracts the database operations.
|
// PrefixDb interface abstracts the database operations.
|
||||||
type PrefixDb interface {
|
type PrefixDb interface {
|
||||||
Get(ctx context.Context, key []byte) ([]byte, error)
|
Get(ctx context.Context, key []byte) ([]byte, error)
|
||||||
@@ -34,12 +30,8 @@ func NewSubPrefixDb(store db.Db, pfx []byte) *SubPrefixDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func(s *SubPrefixDb) SetSession(sessionId string) {
|
func (s *SubPrefixDb) toKey(k []byte) []byte {
|
||||||
s.store.SetSession(sessionId)
|
return append(s.pfx, k...)
|
||||||
}
|
|
||||||
|
|
||||||
func(s *SubPrefixDb) toKey(k []byte) []byte {
|
|
||||||
return append(s.pfx, k...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
|
func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ var (
|
|||||||
dbC map[string]chan db.Db
|
dbC map[string]chan db.Db
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type ThreadGdbmDb struct {
|
type ThreadGdbmDb struct {
|
||||||
db db.Db
|
db db.Db
|
||||||
connStr string
|
connStr string
|
||||||
|
|||||||
@@ -1,109 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
"encoding/binary"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TimedDb struct {
|
|
||||||
db.Db
|
|
||||||
tdb *SubPrefixDb
|
|
||||||
ttl time.Duration
|
|
||||||
parentPfx uint8
|
|
||||||
parentSession []byte
|
|
||||||
matchPfx map[uint8][][]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTimedDb(db db.Db, ttl time.Duration) *TimedDb {
|
|
||||||
var b [2]byte
|
|
||||||
binary.BigEndian.PutUint16(b[:], SUBPREFIX_TIME)
|
|
||||||
sdb := NewSubPrefixDb(db, b[:])
|
|
||||||
return &TimedDb{
|
|
||||||
Db: db,
|
|
||||||
tdb: sdb,
|
|
||||||
ttl: ttl,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func(tib *TimedDb) WithMatch(pfx uint8, keyPart []byte) *TimedDb {
|
|
||||||
if tib.matchPfx == nil {
|
|
||||||
tib.matchPfx = make(map[uint8][][]byte)
|
|
||||||
}
|
|
||||||
tib.matchPfx[pfx] = append(tib.matchPfx[pfx], keyPart)
|
|
||||||
return tib
|
|
||||||
}
|
|
||||||
|
|
||||||
func(tib *TimedDb) checkPrefix(pfx uint8, key []byte) bool {
|
|
||||||
var v []byte
|
|
||||||
if tib.matchPfx == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
for _, v = range(tib.matchPfx[pfx]) {
|
|
||||||
l := len(v)
|
|
||||||
if l > len(key) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if bytes.Equal(v, key[:l]) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func(tib *TimedDb) SetPrefix(pfx uint8) {
|
|
||||||
tib.Db.SetPrefix(pfx)
|
|
||||||
tib.parentPfx = pfx
|
|
||||||
}
|
|
||||||
|
|
||||||
func(tib *TimedDb) SetSession(session string) {
|
|
||||||
tib.Db.SetSession(session)
|
|
||||||
tib.parentSession = []byte(session)
|
|
||||||
}
|
|
||||||
|
|
||||||
func(tib *TimedDb) Put(ctx context.Context, key []byte, val []byte) error {
|
|
||||||
t := time.Now()
|
|
||||||
b, err := t.MarshalBinary()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = tib.Db.Put(ctx, key, val)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
tib.parentPfx = 0
|
|
||||||
tib.parentSession = nil
|
|
||||||
}()
|
|
||||||
if tib.checkPrefix(tib.parentPfx, key) {
|
|
||||||
tib.tdb.SetSession("")
|
|
||||||
k := db.ToSessionKey(tib.parentPfx, []byte(tib.parentSession), key)
|
|
||||||
k = append([]byte{tib.parentPfx}, k...)
|
|
||||||
err = tib.tdb.Put(ctx, k, b)
|
|
||||||
if err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "failed to update timestamp of record", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func(tib *TimedDb) Stale(ctx context.Context, pfx uint8, sessionId string, key []byte) bool {
|
|
||||||
tib.tdb.SetSession("")
|
|
||||||
b := db.ToSessionKey(pfx, []byte(sessionId), key)
|
|
||||||
b = append([]byte{pfx}, b...)
|
|
||||||
v, err := tib.tdb.Get(ctx, b)
|
|
||||||
if err != nil {
|
|
||||||
logg.WarnCtxf(ctx, "no time entry", "key", key, "b", b)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
t_now := time.Now()
|
|
||||||
t_then := time.Time{}
|
|
||||||
err = t_then.UnmarshalBinary(v)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return t_now.After(t_then.Add(tib.ttl))
|
|
||||||
}
|
|
||||||
@@ -1,125 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
|
||||||
memdb "git.defalsify.org/vise.git/db/mem"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestStaleDb(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
mdb := memdb.NewMemDb()
|
|
||||||
err := mdb.Connect(ctx, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tdb := NewTimedDb(mdb, time.Duration(time.Millisecond))
|
|
||||||
tdb.SetPrefix(db.DATATYPE_USERDATA)
|
|
||||||
k := []byte("foo")
|
|
||||||
err = tdb.Put(ctx, k, []byte("bar"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "", k) {
|
|
||||||
t.Fatal("expected not stale")
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond)
|
|
||||||
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "", k) {
|
|
||||||
t.Fatal("expected stale")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFilteredStaleDb(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
mdb := memdb.NewMemDb()
|
|
||||||
err := mdb.Connect(ctx, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
k := []byte("foo")
|
|
||||||
tdb := NewTimedDb(mdb, time.Duration(time.Millisecond))
|
|
||||||
tdb = tdb.WithMatch(db.DATATYPE_STATE, []byte("fo"))
|
|
||||||
tdb.SetPrefix(db.DATATYPE_USERDATA)
|
|
||||||
tdb.SetSession("inky")
|
|
||||||
err = tdb.Put(ctx, k, []byte("bar"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
tdb.SetPrefix(db.DATATYPE_STATE)
|
|
||||||
tdb.SetSession("inky")
|
|
||||||
err = tdb.Put(ctx, k, []byte("pinky"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
tdb.SetSession("blinky")
|
|
||||||
err = tdb.Put(ctx, k, []byte("clyde"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "inky", k) {
|
|
||||||
t.Fatal("expected not stale")
|
|
||||||
}
|
|
||||||
if tdb.Stale(ctx, db.DATATYPE_STATE, "inky", k) {
|
|
||||||
t.Fatal("expected not stale")
|
|
||||||
}
|
|
||||||
if tdb.Stale(ctx, db.DATATYPE_STATE, "blinky", k) {
|
|
||||||
t.Fatal("expected not stale")
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond)
|
|
||||||
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "inky", k) {
|
|
||||||
t.Fatal("expected not stale")
|
|
||||||
}
|
|
||||||
if !tdb.Stale(ctx, db.DATATYPE_STATE, "inky", k) {
|
|
||||||
t.Fatal("expected stale")
|
|
||||||
}
|
|
||||||
if tdb.Stale(ctx, db.DATATYPE_STATE, "blinky", k) {
|
|
||||||
t.Fatal("expected not stale")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFilteredSameKeypartStaleDb(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
mdb := memdb.NewMemDb()
|
|
||||||
err := mdb.Connect(ctx, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tdb := NewTimedDb(mdb, time.Duration(time.Millisecond))
|
|
||||||
tdb = tdb.WithMatch(db.DATATYPE_USERDATA, []byte("ba"))
|
|
||||||
tdb.SetPrefix(db.DATATYPE_USERDATA)
|
|
||||||
tdb.SetSession("xyzzy")
|
|
||||||
err = tdb.Put(ctx, []byte("bar"), []byte("inky"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
tdb.SetPrefix(db.DATATYPE_USERDATA)
|
|
||||||
tdb.SetSession("xyzzy")
|
|
||||||
err = tdb.Put(ctx, []byte("baz"), []byte("pinky"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
tdb.SetPrefix(db.DATATYPE_USERDATA)
|
|
||||||
tdb.SetSession("xyzzy")
|
|
||||||
err = tdb.Put(ctx, []byte("foo"), []byte("blinky"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond)
|
|
||||||
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("bar")) {
|
|
||||||
t.Fatal("expected stale")
|
|
||||||
}
|
|
||||||
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("baz")) {
|
|
||||||
t.Fatal("expected stale")
|
|
||||||
}
|
|
||||||
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("foo")) {
|
|
||||||
t.Fatal("expected not stale")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -54,7 +54,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1235",
|
"input": "1235",
|
||||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
"expectedContent": "Incorrect pin\n1:Retry\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1",
|
"input": "1",
|
||||||
@@ -95,7 +95,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1235",
|
"input": "1235",
|
||||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
"expectedContent": "Incorrect pin\n1:Retry\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1",
|
"input": "1",
|
||||||
@@ -141,7 +141,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1235",
|
"input": "1235",
|
||||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
"expectedContent": "Incorrect pin\n1:Retry\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1",
|
"input": "1",
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1111",
|
"input": "1111",
|
||||||
"expectedContent": "The PIN is not a match. Try again\n1:retry\n9:Quit"
|
"expectedContent": "The PIN is not a match. Try again\n1:Retry\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1",
|
"input": "1",
|
||||||
@@ -65,7 +65,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "000",
|
"input": "000",
|
||||||
"expectedContent": "000 is not registered or invalid, please try again:\n1:retry\n9:Quit"
|
"expectedContent": "000 is not registered or invalid, please try again:\n1:Retry\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1",
|
"input": "1",
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "10000000",
|
"input": "10000000",
|
||||||
"expectedContent": "Amount 10000000 is invalid, please try again:\n1:retry\n9:Quit"
|
"expectedContent": "Amount 10000000 is invalid, please try again:\n1:Retry\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1",
|
"input": "1",
|
||||||
@@ -89,7 +89,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1222",
|
"input": "1222",
|
||||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
"expectedContent": "Incorrect pin\n1:Retry\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1",
|
"input": "1",
|
||||||
@@ -140,7 +140,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "6",
|
"input": "6",
|
||||||
"expectedContent": "Address: {public_key}\n9:Quit"
|
"expectedContent": "Address: {public_key}\n0:Back\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "9",
|
"input": "9",
|
||||||
|
|||||||
@@ -1,20 +1,24 @@
|
|||||||
package remote
|
package remote
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
|
||||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/models"
|
"git.grassecon.net/urdt/ussd/models"
|
||||||
|
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||||
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
InfoLogger *log.Logger
|
||||||
|
ErrorLogger *log.Logger
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountServiceInterface interface {
|
type AccountServiceInterface interface {
|
||||||
@@ -51,7 +55,7 @@ func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey stri
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = doCustodialRequest(ctx, req, &r)
|
_, err = doCustodialRequest(ctx, req, &r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -79,7 +83,6 @@ func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*
|
|||||||
return &balanceResult, err
|
return &balanceResult, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// CreateAccount creates a new account in the custodial system.
|
// CreateAccount creates a new account in the custodial system.
|
||||||
// Returns:
|
// Returns:
|
||||||
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
|
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
|
||||||
@@ -93,9 +96,9 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
_, err = doCustodialRequest(ctx, req, &r)
|
||||||
_, err = doCustodialRequest(ctx, req, &r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("Failed to make custodial %s request to endpoint: %s with reason: %s", req.Method, req.URL, err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +121,7 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = doDataRequest(ctx, req, r)
|
_, err = doDataRequest(ctx, req, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -126,7 +129,6 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// FetchTransactions retrieves the last 10 transactions for a given public key from the data indexer API endpoint
|
// FetchTransactions retrieves the last 10 transactions for a given public key from the data indexer API endpoint
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - publicKey: The public key associated with the account.
|
// - publicKey: The public key associated with the account.
|
||||||
@@ -143,7 +145,7 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = doDataRequest(ctx, req, r)
|
_, err = doDataRequest(ctx, req, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -151,7 +153,6 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// VoucherData retrieves voucher metadata from the data indexer API endpoint.
|
// VoucherData retrieves voucher metadata from the data indexer API endpoint.
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - address: The voucher address.
|
// - address: The voucher address.
|
||||||
@@ -173,9 +174,8 @@ func (as *AccountService) VoucherData(ctx context.Context, address string) (*mod
|
|||||||
}
|
}
|
||||||
|
|
||||||
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||||
var okResponse api.OKResponse
|
var okResponse api.OKResponse
|
||||||
var errResponse api.ErrResponse
|
var errResponse api.ErrResponse
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -184,6 +184,7 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
InfoLogger.Printf("Received response for %s: Status Code: %d | Content-Type: %s", req.URL, resp.StatusCode, resp.Header.Get("Content-Type"))
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -202,7 +203,6 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
|
|||||||
if len(okResponse.Result) == 0 {
|
if len(okResponse.Result) == 0 {
|
||||||
return nil, errors.New("Empty api result")
|
return nil, errors.New("Empty api result")
|
||||||
}
|
}
|
||||||
return &okResponse, nil
|
|
||||||
|
|
||||||
v, err := json.Marshal(okResponse.Result)
|
v, err := json.Marshal(okResponse.Result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -215,10 +215,29 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
|
|||||||
|
|
||||||
func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||||
req.Header.Set("X-GE-KEY", config.CustodialAPIKey)
|
req.Header.Set("X-GE-KEY", config.CustodialAPIKey)
|
||||||
|
logRequestDetails(req)
|
||||||
return doRequest(ctx, req, rcpt)
|
return doRequest(ctx, req, rcpt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||||
req.Header.Set("X-GE-KEY", config.DataAPIKey)
|
req.Header.Set("X-GE-KEY", config.DataAPIKey)
|
||||||
|
logRequestDetails(req)
|
||||||
return doRequest(ctx, req, rcpt)
|
return doRequest(ctx, req, rcpt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logRequestDetails(req *http.Request) {
|
||||||
|
var bodyBytes []byte
|
||||||
|
contentType := req.Header.Get("Content-Type")
|
||||||
|
if req.Body != nil {
|
||||||
|
bodyBytes, err := io.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
ErrorLogger.Printf("Error reading request body: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||||
|
} else {
|
||||||
|
bodyBytes = []byte("-")
|
||||||
|
}
|
||||||
|
|
||||||
|
InfoLogger.Printf("URL: %s | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes))
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
LOAD check_identifier 0
|
LOAD check_identifier 0
|
||||||
RELOAD check_identifier
|
RELOAD check_identifier
|
||||||
MAP check_identifier
|
MAP check_identifier
|
||||||
|
MOUT back 0
|
||||||
MOUT quit 9
|
MOUT quit 9
|
||||||
HALT
|
HALT
|
||||||
|
INCMP _ 0
|
||||||
INCMP quit 9
|
INCMP quit 9
|
||||||
|
|||||||
1
services/registration/address_swa
Normal file
1
services/registration/address_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Anwani:{{.check_identifier}}
|
||||||
1
services/registration/confirm_others_new_pin_swa
Normal file
1
services/registration/confirm_others_new_pin_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Tafadhali thibitisha PIN mpya ya: {{.retrieve_blocked_number}}
|
||||||
1
services/registration/enter_other_number_swa
Normal file
1
services/registration/enter_other_number_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Weka nambari ya simu ili kutuma ombi la kubadilisha nambari ya siri:
|
||||||
1
services/registration/enter_others_new_pin_swa
Normal file
1
services/registration/enter_others_new_pin_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Tafadhali weka PIN mpya ya: {{.retrieve_blocked_number}}
|
||||||
1
services/registration/no_admin_privilege_swa
Normal file
1
services/registration/no_admin_privilege_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Huna mapendeleo ya kufanya kitendo hiki
|
||||||
@@ -1 +1 @@
|
|||||||
You need a voucher to send
|
You need a voucher to proceed
|
||||||
@@ -1 +1 @@
|
|||||||
Unahitaji sarafu kutuma
|
Unahitaji sarafu kuendelea
|
||||||
1
services/registration/others_pin_mismatch_swa
Normal file
1
services/registration/others_pin_mismatch_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
PIN uliyoweka hailingani.Jaribu tena.
|
||||||
1
services/registration/pin_reset_mismatch_swa
Normal file
1
services/registration/pin_reset_mismatch_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
PIN uliyoweka hailingani.Jaribu tena.
|
||||||
1
services/registration/pin_reset_result_swa
Normal file
1
services/registration/pin_reset_result_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Ombi la kuweka upya PIN ya {{.retrieve_blocked_number}} limefanikiwa
|
||||||
1
services/registration/retry_menu
Normal file
1
services/registration/retry_menu
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Retry
|
||||||
1
services/registration/retry_menu_swa
Normal file
1
services/registration/retry_menu_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Jaribu tena
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
CATCH no_voucher flag_no_active_voucher 1
|
||||||
LOAD get_vouchers 0
|
LOAD get_vouchers 0
|
||||||
MAP get_vouchers
|
MAP get_vouchers
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
|
|||||||
1
services/registration/select_voucher_menu_swa
Normal file
1
services/registration/select_voucher_menu_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Chagua Sarafu
|
||||||
1
services/registration/unregistered_number_swa
Normal file
1
services/registration/unregistered_number_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Nambari uliyoingiza haijasajiliwa na Sarafu au sio sahihi.
|
||||||
1
services/registration/voucher_details_menu_swa
Normal file
1
services/registration/voucher_details_menu_swa
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Maelezo ya Sarafu
|
||||||
Reference in New Issue
Block a user