client component (Send) basically working

This commit is contained in:
Helmut Merz 2023-06-30 10:11:17 +02:00
parent c6d5fafa7d
commit c6ac480ad7
4 changed files with 33 additions and 2 deletions

View file

@ -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
}

View file

@ -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{

View file

@ -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))

View file

@ -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 {