Merge pull request #2024 from weaveworks/abstract-api-log

Abstract out logging function from weave api package
This commit is contained in:
Matthias Radestock committed 2016-03-09 15:45:00 +00:00
commit 52552b59fb
3 files changed
+12 -8

No files matched your search

+10 -6
View File
@@ -8,8 +8,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
. "github.com/weaveworks/weave/common"
) )
const ( const (
@@ -19,11 +17,12 @@ const (
type Client struct { type Client struct {
baseURL string baseURL string
log Logger
} }
func (client *Client) httpVerb(verb string, url string, values url.Values) (string, error) { func (client *Client) httpVerb(verb string, url string, values url.Values) (string, error) {
url = client.baseURL + url url = client.baseURL + url
Log.Debugf("weave %s to %s with %v", verb, url, values) client.log.Debugf("weave %s to %s with %v", verb, url, values)
var body io.Reader var body io.Reader
if values != nil { if values != nil {
body = strings.NewReader(values.Encode()) body = strings.NewReader(values.Encode())
@@ -50,7 +49,7 @@ func (client *Client) httpVerb(verb string, url string, values url.Values) (stri
return "", errors.New(resp.Status + ": " + string(rbody)) return "", errors.New(resp.Status + ": " + string(rbody))
} }
func NewClient(addr string) *Client { func NewClient(addr string, log Logger) *Client {
host := WeaveHTTPHost host := WeaveHTTPHost
port := fmt.Sprintf("%d", WeaveHTTPPort) port := fmt.Sprintf("%d", WeaveHTTPPort)
switch parts := strings.Split(addr, ":"); len(parts) { switch parts := strings.Split(addr, ":"); len(parts) {
@@ -67,12 +66,17 @@ func NewClient(addr string) *Client {
port = parts[1] port = parts[1]
} }
default: default:
return &Client{baseURL: fmt.Sprintf("http://%s", addr)} return &Client{baseURL: fmt.Sprintf("http://%s", addr), log: log}
} }
return &Client{baseURL: fmt.Sprintf("http://%s:%s", host, port)} return &Client{baseURL: fmt.Sprintf("http://%s:%s", host, port), log: log}
} }
func (client *Client) Connect(remote string) error { func (client *Client) Connect(remote string) error {
_, err := client.httpVerb("POST", "/connect", url.Values{"peer": {remote}}) _, err := client.httpVerb("POST", "/connect", url.Values{"peer": {remote}})
return err return err
} }
type Logger interface {
Infof(string, ...interface{})
Debugf(string, ...interface{})
}
+1 -1
View File
@@ -51,7 +51,7 @@ func main() {
Log.Fatalf("unable to connect to docker: %s", err) Log.Fatalf("unable to connect to docker: %s", err)
} }
weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR")) weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), Log)
Log.Println("Weave plugin", version, "Command line options:", os.Args[1:]) Log.Println("Weave plugin", version, "Command line options:", os.Args[1:])
Log.Info(dockerClient.Info()) Log.Info(dockerClient.Info())
+1 -1
View File
@@ -558,7 +558,7 @@ func (proxy *Proxy) getDNSDomain() string {
if proxy.WithoutDNS { if proxy.WithoutDNS {
return "" return ""
} }
weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR")) weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), Log)
domain, _ := weave.DNSDomain() domain, _ := weave.DNSDomain()
return domain return domain
} }