ussd/cmd/main.go

136 lines
3.1 KiB
Go
Raw Permalink Normal View History

2024-08-15 10:12:58 +02:00
package main
import (
"context"
"flag"
"fmt"
2024-11-07 14:46:12 +01:00
"log"
2024-08-15 10:12:58 +02:00
"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-10-15 22:41:16 +02:00
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers"
2024-09-19 14:54:23 +02:00
"git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/storage"
2024-10-30 14:09:15 +01:00
"git.grassecon.net/urdt/ussd/remote"
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-11-07 14:46:12 +01:00
InfoLogger *log.Logger
ErrorLogger *log.Logger
2024-08-15 12:49:36 +02:00
)
2024-10-15 22:41:16 +02:00
func init() {
initializers.LoadEnvVariables()
2024-11-07 14:46:12 +01:00
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
2024-10-15 22:41:16 +02:00
}
2024-08-15 10:12:58 +02:00
func main() {
2024-10-15 22:41:16 +02:00
config.LoadConfig()
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 database 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(&database, "db", "gdbm", "database to be used")
2024-09-05 16:07:20 +02:00
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)
ctx = context.WithValue(ctx, "Database", database)
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),
2024-10-15 12:59:49 +02:00
FlagCount: uint32(128),
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
lhs, err := handlers.NewLocalHandlerService(ctx, 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)
}
2024-10-30 14:09:15 +01:00
accountService := remote.AccountService{}
2024-10-17 11:54:11 +02:00
hl, err := lhs.GetHandler(&accountService)
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)
}
}