2024-09-06 01:40:57 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-09-10 21:44:10 +02:00
|
|
|
"os/signal"
|
2024-09-06 01:40:57 +02:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2024-09-10 21:44:10 +02:00
|
|
|
"syscall"
|
2024-09-06 01:40:57 +02:00
|
|
|
|
|
|
|
"git.defalsify.org/vise.git/asm"
|
|
|
|
"git.defalsify.org/vise.git/engine"
|
|
|
|
"git.defalsify.org/vise.git/logging"
|
2024-09-19 15:16:17 +02:00
|
|
|
"git.defalsify.org/vise.git/resource"
|
2024-09-06 01:40:57 +02:00
|
|
|
|
2024-09-12 05:13:57 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
2024-09-06 01:40:57 +02:00
|
|
|
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
2024-09-19 15:16:17 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
2024-09-06 01:40:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-09-19 15:16:17 +02:00
|
|
|
logg = logging.NewVanilla()
|
2024-09-06 01:40:57 +02:00
|
|
|
scriptDir = path.Join("services", "registration")
|
|
|
|
)
|
|
|
|
|
|
|
|
func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
|
|
|
|
flagParser := asm.NewFlagParser().WithDebug()
|
|
|
|
_, err := flagParser.Load(fp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return flagParser, nil
|
|
|
|
}
|
|
|
|
|
2024-09-10 21:44:10 +02:00
|
|
|
func ensureDbDir(dbDir string) error {
|
2024-09-06 01:40:57 +02:00
|
|
|
err := os.MkdirAll(dbDir, 0700)
|
|
|
|
if err != nil {
|
2024-09-10 21:44:10 +02:00
|
|
|
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
2024-09-10 21:44:10 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-06 01:40:57 +02:00
|
|
|
func main() {
|
|
|
|
var dbDir string
|
|
|
|
var resourceDir string
|
|
|
|
var size uint
|
|
|
|
var engineDebug bool
|
|
|
|
var stateDebug bool
|
|
|
|
var host string
|
|
|
|
var port uint
|
|
|
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
|
|
|
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
|
|
|
flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output")
|
|
|
|
flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output")
|
|
|
|
flag.UintVar(&size, "s", 160, "max size of output")
|
|
|
|
flag.StringVar(&host, "h", "127.0.0.1", "http host")
|
|
|
|
flag.UintVar(&port, "p", 7123, "http port")
|
|
|
|
flag.Parse()
|
|
|
|
|
2024-09-19 15:16:17 +02:00
|
|
|
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size)
|
2024-09-06 01:40:57 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
pfp := path.Join(scriptDir, "pp.csv")
|
|
|
|
flagParser, err := getFlags(pfp, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := engine.Config{
|
|
|
|
Root: "root",
|
|
|
|
OutputSize: uint32(size),
|
|
|
|
FlagCount: uint32(16),
|
|
|
|
}
|
|
|
|
if stateDebug {
|
|
|
|
cfg.StateDebug = true
|
|
|
|
}
|
|
|
|
if engineDebug {
|
|
|
|
cfg.EngineDebug = true
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:16:17 +02:00
|
|
|
menuStorageService := storage.MenuStorageService{}
|
|
|
|
rs, err := menuStorageService.GetResource(scriptDir, ctx)
|
2024-09-06 01:40:57 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:16:17 +02:00
|
|
|
err = menuStorageService.EnsureDbDir(dbDir)
|
2024-09-10 21:44:10 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:16:17 +02:00
|
|
|
userdataStore := menuStorageService.GetUserdataDb(dbDir, ctx)
|
2024-09-06 01:40:57 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-09-10 21:44:10 +02:00
|
|
|
defer userdataStore.Close()
|
2024-09-06 01:40:57 +02:00
|
|
|
|
|
|
|
dbResource, ok := rs.(*resource.DbResource)
|
|
|
|
if !ok {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:16:17 +02:00
|
|
|
lhs := handlers.LocalHandlerService{
|
|
|
|
Parser: flagParser,
|
|
|
|
DbRs: dbResource,
|
|
|
|
UserdataStore: userdataStore,
|
|
|
|
Cfg: cfg,
|
|
|
|
Rs: rs,
|
|
|
|
}
|
|
|
|
|
|
|
|
hl, err := lhs.GetHandler()
|
2024-09-06 01:40:57 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:16:17 +02:00
|
|
|
stateStore, err := menuStorageService.GetStateStore(dbDir, ctx)
|
2024-09-06 01:40:57 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-09-10 21:44:10 +02:00
|
|
|
defer stateStore.Close()
|
2024-09-06 01:40:57 +02:00
|
|
|
|
2024-09-11 00:09:10 +02:00
|
|
|
rp := &httpserver.DefaultRequestParser{}
|
2024-09-12 05:13:57 +02:00
|
|
|
bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
|
|
|
|
sh := httpserver.ToSessionHandler(bsh)
|
2024-09-06 01:40:57 +02:00
|
|
|
s := &http.Server{
|
2024-09-19 15:16:17 +02:00
|
|
|
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
|
2024-09-06 01:40:57 +02:00
|
|
|
Handler: sh,
|
|
|
|
}
|
2024-09-10 21:44:10 +02:00
|
|
|
s.RegisterOnShutdown(sh.Shutdown)
|
|
|
|
|
|
|
|
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:
|
|
|
|
}
|
|
|
|
s.Shutdown(ctx)
|
|
|
|
}()
|
2024-09-06 01:40:57 +02:00
|
|
|
err = s.ListenAndServe()
|
|
|
|
if err != nil {
|
2024-09-10 21:44:10 +02:00
|
|
|
logg.Infof("Server closed with error", "err", err)
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
|
|
|
}
|