2024-09-12 05:07:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-09-12 05:13:57 +02:00
|
|
|
"os/signal"
|
2024-09-12 05:07:55 +02:00
|
|
|
"path"
|
2024-09-12 05:13:57 +02:00
|
|
|
"syscall"
|
2024-09-12 05:07:55 +02:00
|
|
|
|
|
|
|
"git.defalsify.org/vise.git/engine"
|
|
|
|
"git.defalsify.org/vise.git/logging"
|
2024-09-19 15:18:21 +02:00
|
|
|
"git.defalsify.org/vise.git/resource"
|
2024-09-12 05:07:55 +02:00
|
|
|
|
2024-10-15 22:41:16 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/config"
|
|
|
|
"git.grassecon.net/urdt/ussd/initializers"
|
2024-09-12 05:07:55 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
2024-10-17 11:54:11 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
2024-09-19 15:18:21 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
2024-09-12 05:07:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-09-19 15:18:21 +02:00
|
|
|
logg = logging.NewVanilla()
|
2024-09-12 05:07:55 +02:00
|
|
|
scriptDir = path.Join("services", "registration")
|
|
|
|
)
|
|
|
|
|
2024-10-15 22:41:16 +02:00
|
|
|
func init() {
|
|
|
|
initializers.LoadEnvVariables()
|
|
|
|
}
|
|
|
|
|
2024-09-12 05:07:55 +02:00
|
|
|
type asyncRequestParser struct {
|
|
|
|
sessionId string
|
2024-09-19 15:18:21 +02:00
|
|
|
input []byte
|
2024-09-12 05:07:55 +02:00
|
|
|
}
|
|
|
|
|
2024-09-19 15:18:21 +02:00
|
|
|
func (p *asyncRequestParser) GetSessionId(r any) (string, error) {
|
2024-09-12 05:07:55 +02:00
|
|
|
return p.sessionId, nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:18:21 +02:00
|
|
|
func (p *asyncRequestParser) GetInput(r any) ([]byte, error) {
|
2024-09-12 05:07:55 +02:00
|
|
|
return p.input, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-10-15 22:41:16 +02:00
|
|
|
config.LoadConfig()
|
|
|
|
|
2024-09-12 05:07:55 +02:00
|
|
|
var sessionId string
|
|
|
|
var dbDir string
|
|
|
|
var resourceDir string
|
|
|
|
var size uint
|
2024-10-17 11:46:20 +02:00
|
|
|
var database string
|
2024-09-12 05:07:55 +02:00
|
|
|
var engineDebug bool
|
|
|
|
var host string
|
|
|
|
var port uint
|
|
|
|
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
|
|
|
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
|
|
|
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
2024-10-17 11:46:20 +02:00
|
|
|
flag.StringVar(&database, "db", "gdbm", "database to be used")
|
2024-09-24 15:11:22 +02:00
|
|
|
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
2024-09-12 05:07:55 +02:00
|
|
|
flag.UintVar(&size, "s", 160, "max size of output")
|
2024-10-15 22:41:16 +02:00
|
|
|
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")
|
|
|
|
flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port")
|
2024-09-12 05:07:55 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-09-19 15:18:21 +02:00
|
|
|
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId)
|
2024-09-12 05:07:55 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2024-10-17 11:46:20 +02:00
|
|
|
ctx = context.WithValue(ctx, "Database", database)
|
2024-09-12 05:07:55 +02:00
|
|
|
pfp := path.Join(scriptDir, "pp.csv")
|
|
|
|
|
|
|
|
cfg := engine.Config{
|
|
|
|
Root: "root",
|
|
|
|
OutputSize: uint32(size),
|
2024-10-15 12:59:49 +02:00
|
|
|
FlagCount: uint32(128),
|
2024-09-12 05:07:55 +02:00
|
|
|
}
|
2024-09-24 15:11:22 +02:00
|
|
|
|
2024-09-12 05:07:55 +02:00
|
|
|
if engineDebug {
|
|
|
|
cfg.EngineDebug = true
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir)
|
|
|
|
rs, err := menuStorageService.GetResource(ctx)
|
2024-09-12 05:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
err = menuStorageService.EnsureDbDir()
|
2024-09-12 05:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-22 03:33:11 +02:00
|
|
|
userdataStore, err := menuStorageService.GetUserdataDb(ctx)
|
2024-09-12 05:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer userdataStore.Close()
|
|
|
|
|
|
|
|
dbResource, ok := rs.(*resource.DbResource)
|
|
|
|
if !ok {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-10-31 18:11:26 +01:00
|
|
|
lhs, err := handlers.NewLocalHandlerService(ctx, pfp, true, dbResource, cfg, rs)
|
2024-09-22 03:33:11 +02:00
|
|
|
lhs.SetDataStore(&userdataStore)
|
2024-10-31 18:11:26 +01:00
|
|
|
|
|
|
|
err = lhs.AdminStore.Seed()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:54:11 +02:00
|
|
|
accountService := server.AccountService{}
|
2024-09-19 15:18:21 +02:00
|
|
|
|
2024-10-17 11:54:11 +02:00
|
|
|
hl, err := lhs.GetHandler(&accountService)
|
2024-09-12 05:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
stateStore, err := menuStorageService.GetStateStore(ctx)
|
2024-09-12 05:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer stateStore.Close()
|
|
|
|
|
|
|
|
rp := &asyncRequestParser{
|
|
|
|
sessionId: sessionId,
|
|
|
|
}
|
|
|
|
sh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
|
|
|
|
cfg.SessionId = sessionId
|
|
|
|
rqs := handlers.RequestSession{
|
2024-09-19 15:18:21 +02:00
|
|
|
Ctx: ctx,
|
2024-09-12 05:07:55 +02:00
|
|
|
Writer: os.Stdout,
|
|
|
|
Config: cfg,
|
|
|
|
}
|
2024-09-12 05:13:57 +02:00
|
|
|
|
|
|
|
cint := make(chan os.Signal)
|
|
|
|
cterm := make(chan os.Signal)
|
|
|
|
signal.Notify(cint, os.Interrupt, syscall.SIGINT)
|
|
|
|
signal.Notify(cterm, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case _ = <-cint:
|
|
|
|
case _ = <-cterm:
|
|
|
|
}
|
|
|
|
sh.Shutdown()
|
|
|
|
}()
|
|
|
|
|
2024-09-12 05:07:55 +02:00
|
|
|
for true {
|
|
|
|
rqs, err = sh.Process(rqs)
|
|
|
|
if err != nil {
|
2024-09-23 18:35:17 +02:00
|
|
|
logg.ErrorCtxf(ctx, "error in process: %v", "err", err)
|
2024-09-12 05:07:55 +02:00
|
|
|
fmt.Errorf("error in process: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
rqs, err = sh.Output(rqs)
|
|
|
|
if err != nil {
|
2024-09-23 18:35:17 +02:00
|
|
|
logg.ErrorCtxf(ctx, "error in output: %v", "err", err)
|
2024-09-12 05:07:55 +02:00
|
|
|
fmt.Errorf("error in output: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
rqs, err = sh.Reset(rqs)
|
|
|
|
if err != nil {
|
2024-09-23 18:35:17 +02:00
|
|
|
logg.ErrorCtxf(ctx, "error in reset: %v", "err", err)
|
2024-09-12 05:07:55 +02:00
|
|
|
fmt.Errorf("error in reset: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Println("")
|
|
|
|
_, err = fmt.Scanln(&rqs.Input)
|
|
|
|
if err != nil {
|
2024-09-23 18:35:17 +02:00
|
|
|
logg.ErrorCtxf(ctx, "error in input", "err", err)
|
2024-09-19 15:18:21 +02:00
|
|
|
fmt.Errorf("error in input: %v", err)
|
2024-09-12 05:07:55 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|