Skip to content

Commit

Permalink
log
Browse files Browse the repository at this point in the history
  • Loading branch information
orvice committed Oct 23, 2024
1 parent 63ea57e commit 87594b6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ip
import (
"encoding/json"
"io"
"log/slog"
"net/http"
)

Expand All @@ -26,22 +27,27 @@ func NewIfconfigCo() *IfconfigCo {
}

func (i *IfconfigCo) GetIP() (string, error) {
logger := slog.Default()
cli := http.Client{}
defer cli.CloseIdleConnections()
resp, err := cli.Get(ipConfigCoAddr)
if err != nil {
logger.Error("get ip error", "error", err)
return "", err
}
defer resp.Body.Close()

s, err := io.ReadAll(resp.Body)
if err != nil {
logger.Error("read body error", "error", err)
return "", err
}
var ret Response
err = json.Unmarshal(s, &ret)
if err != nil {
logger.Error("unmarshal error", "error", err)
return "", err
}
logger.Info("get ip", "ip", ret.IP)
return ret.IP, nil
}

0 comments on commit 87594b6

Please sign in to comment.