mirror of
https://github.com/foomo/gofoomo.git
synced 2025-10-16 12:25:44 +00:00
configs from remote servers
This commit is contained in:
parent
360bdfac5a
commit
50b5113a9e
24
README.md
24
README.md
@ -13,12 +13,12 @@ Go is a much younger and cleaner stack than LAMP.
|
|||||||
* Serve static files without bugging your prefork apache
|
* Serve static files without bugging your prefork apache
|
||||||
* Keep slow connections away from your php processes (not implemented yet)
|
* Keep slow connections away from your php processes (not implemented yet)
|
||||||
* Hijack foomo json rpc services methods
|
* 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
|
* Go´s runtime model is pretty much the opposite of the php runtime model
|
||||||
* all requests vs one request per lifetime
|
* all requests vs one request per lifetime
|
||||||
* shared memory vs process and memory isolation
|
* shared memory vs process and memory isolation
|
||||||
* one bug to kill them all vs one bug kills one request
|
* one bug to kill them all vs one bug kills one request
|
||||||
* hard, but fast vs easy but slow
|
* hard, but fast vs easy but slow
|
||||||
|
|
||||||
## Sitting in front of your foomo LAMP app with Go
|
## Sitting in front of your foomo LAMP app with Go
|
||||||
|
|
||||||
@ -60,3 +60,17 @@ foomo-bert reset :
|
|||||||
## More to come, but not much more
|
## 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.
|
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)
|
||||||
|
```
|
||||||
|
|||||||
@ -10,8 +10,7 @@ import (
|
|||||||
"github.com/foomo/gofoomo/foomo"
|
"github.com/foomo/gofoomo/foomo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func get(foomo *foomo.Foomo, path ...string) (data []byte, err error) {
|
func get(callURL string, path ...string) (data []byte, err error) {
|
||||||
callURL := foomo.GetURLWithCredentialsForDefaultBasicAuthDomain()
|
|
||||||
encodedPath := ""
|
encodedPath := ""
|
||||||
for _, pathEntry := range path {
|
for _, pathEntry := range path {
|
||||||
encodedPath += "/" + url.QueryEscape(pathEntry)
|
encodedPath += "/" + url.QueryEscape(pathEntry)
|
||||||
@ -36,15 +35,19 @@ func get(foomo *foomo.Foomo, path ...string) (data []byte, err error) {
|
|||||||
return data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetJSON call into foomo and unmarshal response using encoding/json
|
func getJSON(callURL string, target interface{}, path ...string) error {
|
||||||
func GetJSON(foomo *foomo.Foomo, target interface{}, path ...string) error {
|
data, err := get(callURL, path...)
|
||||||
data, err := get(foomo, path...)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return json.Unmarshal(data, &target)
|
return json.Unmarshal(data, &target)
|
||||||
}
|
}
|
||||||
return err
|
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
|
// GetConfig retrieve a domain config
|
||||||
func GetConfig(foomo *foomo.Foomo, target interface{}, moduleName string, configName string, domain string) (err error) {
|
func GetConfig(foomo *foomo.Foomo, target interface{}, moduleName string, configName string, domain string) (err error) {
|
||||||
if len(domain) == 0 {
|
if len(domain) == 0 {
|
||||||
|
|||||||
@ -64,7 +64,7 @@ func getTestFoomo() *foomo.Foomo {
|
|||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
f := getTestFoomo()
|
f := getTestFoomo()
|
||||||
data, err := get(f, "config", "Foomo", "Foomo.core")
|
data, err := get(f.GetURLWithCredentialsForDefaultBasicAuthDomain(), "config", "Foomo", "Foomo.core")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -76,6 +76,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetJSON(t *testing.T) {
|
func TestGetJSON(t *testing.T) {
|
||||||
|
|
||||||
f := getTestFoomo()
|
f := getTestFoomo()
|
||||||
config := new(CoreConfig)
|
config := new(CoreConfig)
|
||||||
err := GetJSON(f, config, "config", "Foomo", "Foomo.core")
|
err := GetJSON(f, config, "config", "Foomo", "Foomo.core")
|
||||||
|
|||||||
30
foomo/core/remoteclient.go
Normal file
30
foomo/core/remoteclient.go
Normal 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)
|
||||||
|
}
|
||||||
@ -71,8 +71,8 @@ func (proxy *Proxy) serveHTTP(w http.ResponseWriter, incomingRequest *http.Reque
|
|||||||
}
|
}
|
||||||
incomingRequest.Host = proxy.foomo.URL.Host
|
incomingRequest.Host = proxy.foomo.URL.Host
|
||||||
// incomingRequest.URL.Opaque = incomingRequest.RequestURI + incomingRequest.
|
// incomingRequest.URL.Opaque = incomingRequest.RequestURI + incomingRequest.
|
||||||
proxy.ReverseProxy.ServeHTTP(w, incomingRequest)
|
|
||||||
|
|
||||||
|
proxy.ReverseProxy.ServeHTTP(w, incomingRequest)
|
||||||
for _, listener := range proxy.listeners {
|
for _, listener := range proxy.listeners {
|
||||||
listener.ListenServeHTTPDone(w, incomingRequest)
|
listener.ListenServeHTTPDone(w, incomingRequest)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user