164 lines
3.7 KiB
Go
164 lines
3.7 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/config"
|
|
"git.grassecon.net/urdt/ussd/initializers"
|
|
"git.grassecon.net/urdt/ussd/common"
|
|
"git.grassecon.net/urdt/ussd/remote"
|
|
"git.grassecon.net/urdt/ussd/handlers"
|
|
at "git.grassecon.net/grassrootseconomics/visedriver-africastalking/internal/africastalking"
|
|
)
|
|
|
|
var (
|
|
logg = logging.NewVanilla().WithDomain("AfricasTalking").WithContextKey("at-session-id")
|
|
scriptDir = path.Join("services", "registration")
|
|
build = "dev"
|
|
menuSeparator = ": "
|
|
)
|
|
|
|
func init() {
|
|
initializers.LoadEnvVariables()
|
|
}
|
|
|
|
func main() {
|
|
config.LoadConfig()
|
|
|
|
var connStr string
|
|
var resourceDir string
|
|
var size uint
|
|
var database string
|
|
var engineDebug bool
|
|
var host string
|
|
var port uint
|
|
var err error
|
|
|
|
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
|
flag.StringVar(&connStr, "c", "", "connection string")
|
|
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
|
flag.UintVar(&size, "s", 160, "max size of output")
|
|
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")
|
|
flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port")
|
|
flag.Parse()
|
|
|
|
if connStr != "" {
|
|
connStr = config.DbConn
|
|
}
|
|
connData, err := common.ToConnData(config.DbConn)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "connstr err: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
logg.Infof("start command", "build", build, "conn", connData, "resourcedir", resourceDir, "outputsize", size)
|
|
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, "Database", database)
|
|
pfp := path.Join(scriptDir, "pp.csv")
|
|
|
|
cfg := engine.Config{
|
|
Root: "root",
|
|
OutputSize: uint32(size),
|
|
FlagCount: uint32(128),
|
|
MenuSeparator: menuSeparator,
|
|
}
|
|
|
|
if engineDebug {
|
|
cfg.EngineDebug = true
|
|
}
|
|
|
|
menuStorageService, err := common.NewStorageService(connData)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
rs, err := menuStorageService.GetResource(ctx)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
userdataStore, err := 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(ctx, pfp, true, dbResource, cfg, rs)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
lhs.SetDataStore(&userdataStore)
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
accountService := remote.AccountService{}
|
|
hl, err := lhs.GetHandler(&accountService)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
stateStore, err := menuStorageService.GetStateStore(ctx)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
os.Exit(1)
|
|
}
|
|
defer stateStore.Close()
|
|
|
|
rp := &at.ATRequestParser{
|
|
Context: ctx,
|
|
}
|
|
bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
|
|
sh := at.NewATSessionHandler(bsh)
|
|
|
|
mux := http.NewServeMux()
|
|
mux.Handle(initializers.GetEnv("AT_ENDPOINT", "/"), sh)
|
|
|
|
s := &http.Server{
|
|
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
|
|
Handler: mux,
|
|
}
|
|
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)
|
|
}
|
|
}
|