diff --git a/cmd/main.go b/cmd/main.go index d7fb9b1..29cc368 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,14 +13,17 @@ import ( "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/lang" "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/common" "git.grassecon.net/urdt/ussd/handlers" + at "git.grassecon.net/grassrootseconomics/visedriver-africastalking/internal/africastalking" + "git.grassecon.net/grassrootseconomics/visedriver-africastalking/internal/args" ) var ( @@ -45,6 +48,9 @@ func main() { var host string var port uint var err error + var gettextDir string + var langs args.LangVar + flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") flag.StringVar(&connStr, "c", "", "connection string") @@ -52,9 +58,11 @@ func main() { 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.StringVar(&gettextDir, "gettext", "", "use gettext translations from given directory") + flag.Var(&langs, "language", "add symbol resolution for language") flag.Parse() - if connStr != "" { + if connStr == "" { connStr = config.DbConn } connData, err := common.ToConnData(config.DbConn) @@ -67,6 +75,13 @@ func main() { ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) + ln, err := lang.LanguageFromCode(config.DefaultLanguage) + if err != nil { + fmt.Fprintf(os.Stderr, "default language set error: %v", err) + os.Exit(1) + } + ctx = context.WithValue(ctx, "Language", ln) + pfp := path.Join(scriptDir, "pp.csv") cfg := engine.Config{ @@ -130,9 +145,7 @@ func main() { } defer stateStore.Close() - rp := &at.ATRequestParser{ - Context: ctx, - } + rp := &at.ATRequestParser{} bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl) sh := at.NewATSessionHandler(bsh) diff --git a/go.mod b/go.mod index e860c1c..e0a30a1 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.23.0 require ( git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d - git.grassecon.net/urdt/ussd v0.8.0-beta.5.0.20250105111758-6789c4f550fa + git.grassecon.net/urdt/ussd v0.8.0-beta.11.0.20250109134950-b9712098eff5 ) require ( diff --git a/go.sum b/go.sum index 3226127..05d6647 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSGhfOdcj7kc555f8dc9DmMd2YAyC2AMw= git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= -git.grassecon.net/urdt/ussd v0.8.0-beta.5.0.20250105111758-6789c4f550fa h1:aJaDujoO/zIci2ORGPYwo/Q9wM4tMWTSyRy5BVutm0g= -git.grassecon.net/urdt/ussd v0.8.0-beta.5.0.20250105111758-6789c4f550fa/go.mod h1:Xct45L7FUE4pYtLN4gmhkMCoafUNpcOJ7horP9kPDAc= +git.grassecon.net/urdt/ussd v0.8.0-beta.11.0.20250109134950-b9712098eff5 h1:xDmrSUzN2IdfHt4C0gKGidACqI7t7+bKakiS+Qrd+tA= +git.grassecon.net/urdt/ussd v0.8.0-beta.11.0.20250109134950-b9712098eff5/go.mod h1:Xct45L7FUE4pYtLN4gmhkMCoafUNpcOJ7horP9kPDAc= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= diff --git a/internal/africastalking/parse.go b/internal/africastalking/parse.go index 894d9dc..4013c3d 100644 --- a/internal/africastalking/parse.go +++ b/internal/africastalking/parse.go @@ -18,7 +18,7 @@ type ATRequestParser struct { Context context.Context } -func (arp *ATRequestParser) GetSessionId(rq any) (string, error) { +func (arp *ATRequestParser) GetSessionId(ctx context.Context, rq any) (string, error) { rqv, ok := rq.(*http.Request) if !ok { logg.Warnf("got an invalid request", "req", rq) diff --git a/internal/africastalking/server.go b/internal/africastalking/server.go index 8ccde3e..d53c4c3 100644 --- a/internal/africastalking/server.go +++ b/internal/africastalking/server.go @@ -34,7 +34,7 @@ func (ash *ATSessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) rp := ash.GetRequestParser() cfg := ash.GetConfig() - cfg.SessionId, err = rp.GetSessionId(req) + cfg.SessionId, err = rp.GetSessionId(req.Context(), req) if err != nil { logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err) ash.WriteError(w, 400, err) diff --git a/internal/africastalking/server_test.go b/internal/africastalking/server_test.go index dd45c25..3283215 100644 --- a/internal/africastalking/server_test.go +++ b/internal/africastalking/server_test.go @@ -11,8 +11,9 @@ import ( "testing" "git.defalsify.org/vise.git/engine" - "git.grassecon.net/urdt/ussd/internal/handlers" - "git.grassecon.net/urdt/ussd/internal/testutil/mocks/httpmocks" + "git.grassecon.net/urdt/ussd/request" + verrors "git.grassecon.net/urdt/ussd/errors" + "git.grassecon.net/urdt/ussd/testutil/mocks/httpmocks" ) func TestNewATSessionHandler(t *testing.T) { @@ -49,15 +50,15 @@ func TestATSessionHandler_ServeHTTP(t *testing.T) { parts := strings.Split(text, "*") return []byte(parts[len(parts)-1]), nil } - mh.ProcessFunc = func(rqs handlers.RequestSession) (handlers.RequestSession, error) { + mh.ProcessFunc = func(rqs request.RequestSession) (request.RequestSession, error) { rqs.Continue = true rqs.Engine = me return rqs, nil } mh.GetConfigFunc = func() engine.Config { return engine.Config{} } - mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp } - mh.OutputFunc = func(rs handlers.RequestSession) (handlers.RequestSession, error) { return rs, nil } - mh.ResetFunc = func(rs handlers.RequestSession) (handlers.RequestSession, error) { return rs, nil } + mh.GetRequestParserFunc = func() request.RequestParser { return mrp } + mh.OutputFunc = func(rs request.RequestSession) (request.RequestSession, error) { return rs, nil } + mh.ResetFunc = func(rs request.RequestSession) (request.RequestSession, error) { return rs, nil } me.FlushFunc = func(context.Context, io.Writer) (int, error) { return 0, nil } }, formData: url.Values{ @@ -74,7 +75,7 @@ func TestATSessionHandler_ServeHTTP(t *testing.T) { return "", errors.New("no phone number found") } mh.GetConfigFunc = func() engine.Config { return engine.Config{} } - mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp } + mh.GetRequestParserFunc = func() request.RequestParser { return mrp } }, formData: url.Values{ "text": []string{"1*2*3"}, @@ -93,7 +94,7 @@ func TestATSessionHandler_ServeHTTP(t *testing.T) { return nil, errors.New("no input found") } mh.GetConfigFunc = func() engine.Config { return engine.Config{} } - mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp } + mh.GetRequestParserFunc = func() request.RequestParser { return mrp } }, formData: url.Values{ "phoneNumber": []string{"+1234567890"}, @@ -114,11 +115,11 @@ func TestATSessionHandler_ServeHTTP(t *testing.T) { parts := strings.Split(text, "*") return []byte(parts[len(parts)-1]), nil } - mh.ProcessFunc = func(rqs handlers.RequestSession) (handlers.RequestSession, error) { - return rqs, handlers.ErrStorage + mh.ProcessFunc = func(rqs request.RequestSession) (request.RequestSession, error) { + return rqs, verrors.ErrStorage } mh.GetConfigFunc = func() engine.Config { return engine.Config{} } - mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp } + mh.GetRequestParserFunc = func() request.RequestParser { return mrp } }, formData: url.Values{ "phoneNumber": []string{"+1234567890"}, @@ -158,13 +159,13 @@ func TestATSessionHandler_ServeHTTP(t *testing.T) { func TestATSessionHandler_Output(t *testing.T) { tests := []struct { name string - input handlers.RequestSession + input request.RequestSession expectedPrefix string expectedError bool }{ { name: "Continue true", - input: handlers.RequestSession{ + input: request.RequestSession{ Continue: true, Engine: &httpmocks.MockEngine{ FlushFunc: func(context.Context, io.Writer) (int, error) { @@ -178,7 +179,7 @@ func TestATSessionHandler_Output(t *testing.T) { }, { name: "Continue false", - input: handlers.RequestSession{ + input: request.RequestSession{ Continue: false, Engine: &httpmocks.MockEngine{ FlushFunc: func(context.Context, io.Writer) (int, error) { @@ -192,7 +193,7 @@ func TestATSessionHandler_Output(t *testing.T) { }, { name: "Flush error", - input: handlers.RequestSession{ + input: request.RequestSession{ Continue: true, Engine: &httpmocks.MockEngine{ FlushFunc: func(context.Context, io.Writer) (int, error) { diff --git a/internal/args/lang.go b/internal/args/lang.go new file mode 100644 index 0000000..f9afdc9 --- /dev/null +++ b/internal/args/lang.go @@ -0,0 +1,34 @@ +package args + +import ( + "strings" + + "git.defalsify.org/vise.git/lang" +) + +type LangVar struct { + v []lang.Language +} + +func(lv *LangVar) Set(s string) error { + v, err := lang.LanguageFromCode(s) + if err != nil { + return err + } + lv.v = append(lv.v, v) + return err +} + +func(lv *LangVar) String() string { + var s []string + for _, v := range(lv.v) { + s = append(s, v.Code) + } + return strings.Join(s, ",") +} + +func(lv *LangVar) Langs() []lang.Language { + return lv.v +} + +