From 50b5113a9e147cd684e8eca3550ab59807fa7aa0 Mon Sep 17 00:00:00 2001 From: Jan Halfar Date: Tue, 15 Nov 2016 14:40:05 +0100 Subject: [PATCH] configs from remote servers --- README.md | 24 +++++++++++++++++++----- foomo/core/client.go | 13 ++++++++----- foomo/core/client_test.go | 3 ++- foomo/core/remoteclient.go | 30 ++++++++++++++++++++++++++++++ proxy/proxy.go | 2 +- 5 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 foomo/core/remoteclient.go diff --git a/README.md b/README.md index a75a187..3207fdd 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,12 @@ Go is a much younger and cleaner stack than LAMP. * Serve static files without bugging your prefork apache * Keep slow connections away from your php processes (not implemented yet) * Hijack foomo json rpc services methods -* Your code is also running the server, this puts you in a place, where you can solve problems, that you can not solve in php + * Your code is also running the server, this puts you in a place, whereyou can solve problems, that you can not solve in php * Go´s runtime model is pretty much the opposite of the php runtime model - * all requests vs one request per lifetime - * shared memory vs process and memory isolation - * one bug to kill them all vs one bug kills one request - * hard, but fast vs easy but slow + * all requests vs one request per lifetime + * shared memory vs process and memory isolation + * one bug to kill them all vs one bug kills one request + * hard, but fast vs easy but slow ## Sitting in front of your foomo LAMP app with Go @@ -60,3 +60,17 @@ foomo-bert reset : ## More to come, but not much more We are going to add features, as we are going to need them. The focus is to have a simple interface between foomo and Go. + +## A little more + +This example shows how to access a remote server to read configs. + +```Go +rc, err := core.NewRemoteClient("https://user:password@host.com") +if err != nil { + log.Fatal("could not start remote client", err) +} +c := &MyConf{} +configErr := rc.GetConfig(c, "My.Module", "My.conf", "") +fmt.Println(configErr, c) +``` diff --git a/foomo/core/client.go b/foomo/core/client.go index 9029dd9..571897d 100644 --- a/foomo/core/client.go +++ b/foomo/core/client.go @@ -10,8 +10,7 @@ import ( "github.com/foomo/gofoomo/foomo" ) -func get(foomo *foomo.Foomo, path ...string) (data []byte, err error) { - callURL := foomo.GetURLWithCredentialsForDefaultBasicAuthDomain() +func get(callURL string, path ...string) (data []byte, err error) { encodedPath := "" for _, pathEntry := range path { encodedPath += "/" + url.QueryEscape(pathEntry) @@ -36,15 +35,19 @@ func get(foomo *foomo.Foomo, path ...string) (data []byte, err error) { return data, err } -// GetJSON call into foomo and unmarshal response using encoding/json -func GetJSON(foomo *foomo.Foomo, target interface{}, path ...string) error { - data, err := get(foomo, path...) +func getJSON(callURL string, target interface{}, path ...string) error { + data, err := get(callURL, path...) if err == nil { return json.Unmarshal(data, &target) } return err } +// GetJSON call into foomo and unmarshal response using encoding/json +func GetJSON(foomo *foomo.Foomo, target interface{}, path ...string) error { + return getJSON(foomo.GetURLWithCredentialsForDefaultBasicAuthDomain(), target, path...) +} + // GetConfig retrieve a domain config func GetConfig(foomo *foomo.Foomo, target interface{}, moduleName string, configName string, domain string) (err error) { if len(domain) == 0 { diff --git a/foomo/core/client_test.go b/foomo/core/client_test.go index e912453..158fede 100644 --- a/foomo/core/client_test.go +++ b/foomo/core/client_test.go @@ -64,7 +64,7 @@ func getTestFoomo() *foomo.Foomo { func TestGet(t *testing.T) { f := getTestFoomo() - data, err := get(f, "config", "Foomo", "Foomo.core") + data, err := get(f.GetURLWithCredentialsForDefaultBasicAuthDomain(), "config", "Foomo", "Foomo.core") if err != nil { t.Fatal(err) } @@ -76,6 +76,7 @@ func TestGet(t *testing.T) { } func TestGetJSON(t *testing.T) { + f := getTestFoomo() config := new(CoreConfig) err := GetJSON(f, config, "config", "Foomo", "Foomo.core") diff --git a/foomo/core/remoteclient.go b/foomo/core/remoteclient.go new file mode 100644 index 0000000..f2da5ac --- /dev/null +++ b/foomo/core/remoteclient.go @@ -0,0 +1,30 @@ +package core + +import "net/url" + +type RemoteClient struct { + url string +} + +func NewRemoteClient(urlString string) (rc *RemoteClient, err error) { + _, e := url.Parse(urlString) + if e != nil { + err = e + return + } + rc = &RemoteClient{ + url: urlString, + } + return +} + +func (rc *RemoteClient) get(path ...string) (data []byte, err error) { + return get(rc.url, path...) +} + +func (rc *RemoteClient) GetConfig(target interface{}, moduleName string, configName string, domain string) (err error) { + if len(domain) == 0 { + return getJSON(rc.url, target, "config", moduleName, configName) + } + return getJSON(rc.url, target, "config", moduleName, configName, domain) +} diff --git a/proxy/proxy.go b/proxy/proxy.go index 102449b..83967ef 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -71,8 +71,8 @@ func (proxy *Proxy) serveHTTP(w http.ResponseWriter, incomingRequest *http.Reque } incomingRequest.Host = proxy.foomo.URL.Host // incomingRequest.URL.Opaque = incomingRequest.RequestURI + incomingRequest. - proxy.ReverseProxy.ServeHTTP(w, incomingRequest) + proxy.ReverseProxy.ServeHTTP(w, incomingRequest) for _, listener := range proxy.listeners { listener.ListenServeHTTPDone(w, incomingRequest) }