client component (Send) basically working
This commit is contained in:
parent
c6d5fafa7d
commit
c6ac480ad7
4 changed files with 33 additions and 2 deletions
|
@ -1,8 +1,13 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
"git.sr.ht/~cco/go-scopes/config"
|
"git.sr.ht/~cco/go-scopes/config"
|
||||||
"git.sr.ht/~cco/go-scopes/lib"
|
"git.sr.ht/~cco/go-scopes/lib"
|
||||||
|
"git.sr.ht/~cco/go-scopes/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
|
@ -10,6 +15,23 @@ type Cfg struct {
|
||||||
Url string
|
Url string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var GlobalCookieJar http.CookieJar
|
||||||
|
|
||||||
func Send(act lib.Action) bool {
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func HandleDone(ctx lib.Context) bool {
|
||||||
|
|
||||||
func Serve(ctx lib.Context) {
|
func Serve(ctx lib.Context) {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.GET("/", func(c *gin.Context) {
|
r.GET("/*action", func(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "Hello World")
|
c.String(http.StatusOK, "Hello World")
|
||||||
})
|
})
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
|
|
|
@ -41,6 +41,7 @@ func Config() lib.Config {
|
||||||
BaseCfg: b("test-client", core.Start),
|
BaseCfg: b("test-client", core.Start),
|
||||||
Url: ovr("http://localhost:8123", SERVER_URL),
|
Url: ovr("http://localhost:8123", SERVER_URL),
|
||||||
}
|
}
|
||||||
|
test_client.AddAction("demo", action.Base(client.Send))
|
||||||
|
|
||||||
test_rcvr := b("test-receiver", core.Start).
|
test_rcvr := b("test-receiver", core.Start).
|
||||||
AddAction("demo", action.Base(AH_Receiver))
|
AddAction("demo", action.Base(AH_Receiver))
|
||||||
|
|
|
@ -17,8 +17,9 @@ func TestConfig(tb *tbase.T) {
|
||||||
t.Run("app", AppTest)
|
t.Run("app", AppTest)
|
||||||
t.Run("config", ConfigTest)
|
t.Run("config", ConfigTest)
|
||||||
t.Run("send", SendTest)
|
t.Run("send", SendTest)
|
||||||
|
t.Run("client", ClientTest)
|
||||||
t.TearDownApp()
|
t.TearDownApp()
|
||||||
t.AssertEqual(t.LogCount(true), 8)
|
t.AssertEqual(t.LogCount(true), 11)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AppTest(t *testing.T) {
|
func AppTest(t *testing.T) {
|
||||||
|
@ -42,6 +43,13 @@ func SendTest(t *testing.T) {
|
||||||
lib.Send(ctx, rcvr, msg)
|
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
|
// action handlers
|
||||||
|
|
||||||
func Receiver(act lib.Action) bool {
|
func Receiver(act lib.Action) bool {
|
||||||
|
|
Loading…
Add table
Reference in a new issue