Compare commits

...

3 Commits

Author SHA1 Message Date
Branden J Brown
56c1ad3737 log find requests 2025-03-13 22:54:42 -04:00
Branden J Brown
ecea53685f actually return response value 2025-03-13 22:54:16 -04:00
Branden J Brown
62c9693788 fix shadowed error 2025-03-13 22:33:04 -04:00
4 changed files with 12 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt" "fmt"
"log/slog"
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
@ -35,6 +36,7 @@ func (cl *Client) Find(ctx context.Context, s chord.Peer, id chord.ID) (chord.Pe
if err != nil { if err != nil {
return chord.Peer{}, "", err return chord.Peer{}, "", err
} }
slog.InfoContext(ctx, "find", slog.String("url", url.String()))
resp, err := cl.HTTP.Do(req) resp, err := cl.HTTP.Do(req)
if err != nil { if err != nil {
return chord.Peer{}, "", err return chord.Peer{}, "", err
@ -43,6 +45,7 @@ func (cl *Client) Find(ctx context.Context, s chord.Peer, id chord.ID) (chord.Pe
if err != nil { if err != nil {
return chord.Peer{}, "", fmt.Errorf("%w (%s)", err, resp.Status) return chord.Peer{}, "", fmt.Errorf("%w (%s)", err, resp.Status)
} }
slog.InfoContext(ctx, "found", slog.String("peer", p.Peer.String()), slog.String("value", p.Value))
return chord.Address(p.Peer), p.Value, nil return chord.Address(p.Peer), p.Value, nil
} }

View File

@ -28,6 +28,9 @@ func writeError(w http.ResponseWriter, status int, msg string) {
} }
func readResponse[T any](r *http.Response) (x T, err error) { func readResponse[T any](r *http.Response) (x T, err error) {
if r.StatusCode != http.StatusOK {
return x, errors.New(r.Status)
}
var b struct { var b struct {
Data T `json:"data"` Data T `json:"data"`
Error string `json:"error"` Error string `json:"error"`
@ -38,7 +41,7 @@ func readResponse[T any](r *http.Response) (x T, err error) {
if b.Error != "" { if b.Error != "" {
return x, errors.New(b.Error) return x, errors.New(b.Error)
} }
return x, nil return b.Data, nil
} }
type neighbors struct { type neighbors struct {

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log/slog"
"net" "net"
"net/http" "net/http"
"net/netip" "net/netip"
@ -67,6 +68,7 @@ func (n *Node) Check(ctx context.Context) error {
func (n *Node) key(w http.ResponseWriter, r *http.Request) { func (n *Node) key(w http.ResponseWriter, r *http.Request) {
s := r.PathValue("id") s := r.PathValue("id")
slog.InfoContext(r.Context(), "received find", slog.String("id", s), slog.String("from", r.RemoteAddr))
id, err := chord.ParseID(s) id, err := chord.ParseID(s)
if err != nil { if err != nil {
writeError(w, http.StatusBadRequest, err.Error()) writeError(w, http.StatusBadRequest, err.Error())
@ -78,6 +80,7 @@ func (n *Node) key(w http.ResponseWriter, r *http.Request) {
return return
} }
_, addr := p.Values() _, addr := p.Values()
slog.InfoContext(r.Context(), "tell found", slog.String("id", s), slog.String("addr", addr.String()), slog.String("value", v))
pv := peervalue{addr, v} pv := peervalue{addr, v}
writeOk(w, pv) writeOk(w, pv)
} }

View File

@ -147,7 +147,8 @@ func cliJoin(ctx context.Context, cmd *cli.Command) error {
cl := &httpnode.Client{HTTP: http.Client{Timeout: 5 * time.Second}} cl := &httpnode.Client{HTTP: http.Client{Timeout: 5 * time.Second}}
var node *chord.Node var node *chord.Node
if peer := cmd.String("c"); peer != "" { if peer := cmd.String("c"); peer != "" {
p, err := netip.ParseAddrPort(peer) var p netip.AddrPort
p, err = netip.ParseAddrPort(peer)
if err != nil { if err != nil {
return err return err
} }