client improvements; + error logging

This commit is contained in:
Helmut Merz 2023-07-12 14:24:20 +02:00
parent 235ba65c64
commit 8fac8579ef
5 changed files with 32 additions and 13 deletions

View file

@ -1,13 +1,17 @@
package client
import (
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/logging"
"golang.org/x/net/publicsuffix"
)
type Cfg struct {
@ -21,13 +25,19 @@ 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)
url, _ := url.JoinPath(cfg.Url, msg.Slice()...)
method := "GET"
var pdata io.Reader
if pl := msg.Payload(); pl != nil {
pdata = strings.NewReader(pl.String())
method = "POST"
}
req, _ := http.NewRequest(method, url, pdata)
client := http.DefaultClient
client.Jar = GlobalCookieJar
resp, err := client.Do(req)
if err != nil {
logging.ErrorA(act).Err(err).Msg("client.Send")
logging.ErrorA(act, err).Msg("client.Send")
return true
}
body, _ := ioutil.ReadAll(resp.Body)
@ -35,3 +45,9 @@ func Send(act lib.Action) bool {
logging.InfoA(act).Str("data", bodyStr).Msg("client.Send")
return true
}
// CookieJar initialization
func init() {
GlobalCookieJar, _ = cookiejar.New(
&cookiejar.Options{PublicSuffixList: publicsuffix.List})
}

View file

@ -7,7 +7,7 @@ import (
"git.sr.ht/~cco/go-scopes/logging"
)
func NoOp(_ lib.Context) {}
func None(_ lib.Context) {}
func Start(ctx lib.Context) {
logging.Debug(ctx).Msg("core.Start")

View file

@ -28,8 +28,8 @@ func Warn(ctx lib.Context) *Evt {
return WithContext(ctx, GetLogger(ctx).Warn())
}
func Error(ctx lib.Context) *Evt {
return WithContext(ctx, GetLogger(ctx).Error())
func Error(ctx lib.Context, err error) *Evt {
return WithContext(ctx, GetLogger(ctx).Error()).Err(err)
}
// logging for message handlers
@ -53,8 +53,8 @@ func WarnM(ctx lib.Context, msg lib.Message) *Evt {
return WithMessage(ctx, msg, GetLogger(ctx).Warn())
}
func ErrorM(ctx lib.Context, msg lib.Message) *Evt {
return WithMessage(ctx, msg, GetLogger(ctx).Error())
func ErrorM(ctx lib.Context, msg lib.Message, err error) *Evt {
return WithMessage(ctx, msg, GetLogger(ctx).Error()).Err(err)
}
// logging for action handlers
@ -80,6 +80,6 @@ func WarnA(act lib.Action) *Evt {
return WithAction(act, GetLogger(act.Context()).Warn())
}
func ErrorA(act lib.Action) *Evt {
return WithAction(act, GetLogger(act.Context()).Error())
func ErrorA(act lib.Action, err error) *Evt {
return WithAction(act, GetLogger(act.Context()).Error()).Err(err)
}

View file

@ -1,6 +1,8 @@
package server
import (
"fmt"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/logging"
"github.com/gin-gonic/gin"
@ -18,14 +20,15 @@ func Logger(ctx lib.Context) gin.HandlerFunc {
var evt *zerolog.Event
status := c.Writer.Status()
if status >= 500 {
evt = logging.Error(ctx)
evt = logging.Error(ctx, fmt.Errorf("gin error"))
} else if status >= 400 {
evt = logging.Warn(ctx)
} else {
evt = logging.Info(ctx)
}
evt.Str("method", c.Request.Method).
Str("path", c.Request.URL.Path).
// Str("path", c.Request.URL.Path).
Str("url", c.Request.URL.String()).
// Dur("resp_time", time.Since(t)).
Int("status", status).
Str("client_ip", c.ClientIP()).Msg(msg)

View file

@ -71,7 +71,7 @@ func ActionTest(t *testing.T) {
return true
}
}
cfg := config.Base("testing", core.NoOp).
cfg := config.Base("testing", core.None).
AddAction("start", action.Base(hdlrGen("started"))).
AddAction("scopes|doit|task", action.Base(hdlrGen("done")))
ctx := context.AppContext(cfg)