forked from urdt/ussd
134 lines
3.1 KiB
Go
134 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path"
|
|
"strconv"
|
|
"syscall"
|
|
|
|
"git.defalsify.org/vise.git/engine"
|
|
"git.defalsify.org/vise.git/logging"
|
|
"git.defalsify.org/vise.git/resource"
|
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
|
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
|
)
|
|
|
|
var (
|
|
logg = logging.NewVanilla()
|
|
scriptDir = path.Join("services", "registration")
|
|
)
|
|
|
|
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()
|
|
|
|
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size)
|
|
|
|
ctx := context.Background()
|
|
pfp := path.Join(scriptDir, "pp.csv")
|
|
|
|
cfg := engine.Config{
|
|
Root: "root",
|
|
OutputSize: uint32(size),
|
|
FlagCount: uint32(16),
|
|
}
|
|
if stateDebug {
|
|
cfg.StateDebug = true
|
|
}
|
|
if engineDebug {
|
|
cfg.EngineDebug = true
|
|
}
|
|
|
|
resourceDir := scriptDir
|
|
menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir)
|
|
rs, err := menuStorageService.GetResource(ctx)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
err = menuStorageService.EnsureDbDir()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
userdataStore := menuStorageService.GetUserdataDb(ctx)
|
|
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)
|
|
}
|
|
|
|
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
|
|
lhs.WithDataStore(&userdataStore)
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
hl, err := lhs.GetHandler()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
stateStore, err := menuStorageService.GetStateStore(dbDir, ctx)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
defer stateStore.Close()
|
|
|
|
rp := &httpserver.DefaultRequestParser{}
|
|
bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
|
|
sh := httpserver.ToSessionHandler(bsh)
|
|
s := &http.Server{
|
|
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
|
|
Handler: sh,
|
|
}
|
|
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)
|
|
}()
|
|
err = s.ListenAndServe()
|
|
if err != nil {
|
|
logg.Infof("Server closed with error", "err", err)
|
|
}
|
|
}
|