ussd/cmd/main.go

121 lines
2.5 KiB
Go
Raw Permalink Normal View History

2024-08-15 10:12:58 +02:00
package main
import (
"context"
"flag"
"fmt"
"os"
"path"
2024-08-15 12:49:36 +02:00
"git.defalsify.org/vise.git/engine"
2024-09-07 21:05:09 +02:00
"git.defalsify.org/vise.git/logging"
2024-08-15 10:12:58 +02:00
"git.defalsify.org/vise.git/resource"
2024-09-19 14:54:23 +02:00
"git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/storage"
2024-10-14 13:38:12 +02:00
"github.com/joho/godotenv"
"git.grassecon.net/urdt/ussd/initializers"
2024-08-15 10:12:58 +02:00
)
2024-08-15 12:49:36 +02:00
var (
2024-09-07 21:05:09 +02:00
logg = logging.NewVanilla()
2024-08-15 12:49:36 +02:00
scriptDir = path.Join("services", "registration")
)
2024-10-14 13:38:12 +02:00
func init() {
initializers.LoadEnvVariables()
initializers.ConnectToDb()
initializers.SyncDatabase()
err := godotenv.Load()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
}
2024-08-15 10:12:58 +02:00
func main() {
2024-09-05 16:07:20 +02:00
var dbDir string
2024-08-15 10:12:58 +02:00
var size uint
var sessionId string
2024-09-24 13:10:10 +02:00
var engineDebug bool
2024-09-05 16:07:20 +02:00
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
2024-09-24 15:11:22 +02:00
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
2024-09-05 16:07:20 +02:00
flag.UintVar(&size, "s", 160, "max size of output")
flag.Parse()
2024-09-10 11:32:19 +02:00
logg.Infof("start command", "dbdir", dbDir, "outputsize", size)
2024-08-15 10:12:58 +02:00
ctx := context.Background()
2024-09-07 21:05:09 +02:00
ctx = context.WithValue(ctx, "SessionId", sessionId)
pfp := path.Join(scriptDir, "pp.csv")
2024-08-19 09:59:20 +02:00
2024-08-15 12:49:36 +02:00
cfg := engine.Config{
2024-09-05 16:07:20 +02:00
Root: "root",
SessionId: sessionId,
OutputSize: uint32(size),
FlagCount: uint32(16),
2024-08-15 12:49:36 +02:00
}
2024-09-05 16:07:20 +02:00
resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir)
2024-09-19 14:54:23 +02:00
err := menuStorageService.EnsureDbDir()
2024-09-19 16:01:23 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
rs, err := menuStorageService.GetResource(ctx)
2024-08-15 10:12:58 +02:00
if err != nil {
2024-09-05 16:07:20 +02:00
fmt.Fprintf(os.Stderr, err.Error())
2024-08-15 10:12:58 +02:00
os.Exit(1)
}
2024-08-21 20:37:29 +02:00
pe, err := menuStorageService.GetPersister(ctx)
2024-08-15 12:49:36 +02:00
if err != nil {
2024-09-05 16:07:20 +02:00
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
2024-09-04 18:57:08 +02:00
}
2024-09-22 03:33:11 +02:00
userdatastore, err := menuStorageService.GetUserdataDb(ctx)
2024-09-05 16:07:20 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
2024-09-04 18:57:08 +02:00
dbResource, ok := rs.(*resource.DbResource)
2024-09-04 18:57:08 +02:00
if !ok {
fmt.Fprintf(os.Stderr, err.Error())
2024-09-04 18:57:08 +02:00
os.Exit(1)
2024-08-15 12:49:36 +02:00
}
2024-08-31 09:21:20 +02:00
2024-09-19 20:04:09 +02:00
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
lhs.SetDataStore(&userdatastore)
lhs.SetPersister(pe)
2024-09-19 14:54:23 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
hl, err := lhs.GetHandler()
2024-09-02 15:05:13 +02:00
if err != nil {
2024-09-05 16:07:20 +02:00
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
2024-08-31 09:21:20 +02:00
}
2024-09-19 14:54:23 +02:00
en := lhs.GetEngine()
en = en.WithFirst(hl.Init)
2024-09-24 13:10:10 +02:00
if engineDebug {
en = en.WithDebug(nil)
}
err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil)
2024-08-15 10:12:58 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
os.Exit(1)
}
}