diff --git a/client/client.go b/client/client.go index 63b7d2b..f14b6d7 100644 --- a/client/client.go +++ b/client/client.go @@ -1,8 +1,13 @@ package client import ( + "io/ioutil" + "net/http" + "net/url" + "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/lib" + "git.sr.ht/~cco/go-scopes/logging" ) type Cfg struct { @@ -10,6 +15,23 @@ type Cfg struct { Url string } +var GlobalCookieJar http.CookieJar + func Send(act lib.Action) bool { + ctx, spec, msg := act.Context(), act.Spec(), act.Message() + _ = spec + cfg := lib.GetCfg[*Cfg](ctx) + url, _ := url.JoinPath(cfg.Url, msg.Action()) + req, _ := http.NewRequest("GET", url, nil) + client := http.DefaultClient + client.Jar = GlobalCookieJar + resp, err := client.Do(req) + if err != nil { + logging.ErrorA(act).Err(err).Msg("client.Send") + return true + } + body, _ := ioutil.ReadAll(resp.Body) + bodyStr := string(body) + logging.InfoA(act).Str("data", bodyStr).Msg("client.Send") return true } diff --git a/server/server.go b/server/server.go index 5c14ce9..d00679c 100644 --- a/server/server.go +++ b/server/server.go @@ -31,7 +31,7 @@ func HandleDone(ctx lib.Context) bool { func Serve(ctx lib.Context) { r := gin.Default() - r.GET("/", func(c *gin.Context) { + r.GET("/*action", func(c *gin.Context) { c.String(http.StatusOK, "Hello World") }) srv := &http.Server{ diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 998baae..47736fc 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -41,6 +41,7 @@ func Config() lib.Config { BaseCfg: b("test-client", core.Start), Url: ovr("http://localhost:8123", SERVER_URL), } + test_client.AddAction("demo", action.Base(client.Send)) test_rcvr := b("test-receiver", core.Start). AddAction("demo", action.Base(AH_Receiver)) diff --git a/tests/scopes_test.go b/tests/scopes_test.go index ed339f6..0bc01c5 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -17,8 +17,9 @@ func TestConfig(tb *tbase.T) { t.Run("app", AppTest) t.Run("config", ConfigTest) t.Run("send", SendTest) + t.Run("client", ClientTest) t.TearDownApp() - t.AssertEqual(t.LogCount(true), 8) + t.AssertEqual(t.LogCount(true), 11) } func AppTest(t *testing.T) { @@ -42,6 +43,13 @@ func SendTest(t *testing.T) { lib.Send(ctx, rcvr, msg) } +func ClientTest(t *testing.T) { + ctx := t.Ctx + rcvr := message.SimpleAddress("test-client") + msg := message.StrMessage("demo") + lib.Send(ctx, rcvr, msg) +} + // action handlers func Receiver(act lib.Action) bool {