configs from remote servers

This commit is contained in:
Jan Halfar 2016-11-15 14:40:05 +01:00
parent 360bdfac5a
commit 50b5113a9e
5 changed files with 60 additions and 12 deletions

View File

@ -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)
```

View File

@ -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 {

View File

@ -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")

View File

@ -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)
}

View File

@ -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)
}