ussd/cmd/main.go

111 lines
2.3 KiB
Go
Raw 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-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-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
var debug 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")
flag.BoolVar(&debug, "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
2024-09-19 14:54:23 +02:00
menuStorageService := storage.MenuStorageService{}
2024-09-19 16:01:23 +02:00
err := menuStorageService.EnsureDbDir(dbDir)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
2024-09-19 14:54:23 +02:00
rs, err := menuStorageService.GetResource(scriptDir, 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
2024-09-19 14:54:23 +02:00
pe, err := menuStorageService.GetPersister(dbDir, 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-19 14:54:23 +02:00
userdatastore := menuStorageService.GetUserdataDb(dbDir, 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.WithDataStore(&userdatastore)
lhs.WithPersister(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)
if debug {
en = en.WithDebug(nil)
}
2024-09-20 12:13:20 +02:00
_, err = en.Exec(ctx, []byte{})
2024-08-15 10:12:58 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
os.Exit(1)
}
2024-09-04 18:57:08 +02:00
2024-08-15 10:12:58 +02:00
err = engine.Loop(ctx, en, os.Stdin, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
os.Exit(1)
}
}