Add network proxying (#1556)

* Add network proxying

* go fmt
This commit is contained in:
Asim Aslam
2020-04-21 15:54:40 +01:00
committed by GitHub
parent 05d2b34e10
commit d7ecb58f6c
3 changed files with 51 additions and 55 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
"os"
"strconv"
"strings"
)
@@ -77,3 +78,40 @@ func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, e
// why are we here?
return nil, fmt.Errorf("unable to bind to %s", addr)
}
// Proxy returns the proxy and the address if it exits
func Proxy(service string, address []string) (string, []string, bool) {
var hasProxy bool
// get proxy
if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 {
// default name
if prx == "service" {
prx = "go.micro.proxy"
}
service = prx
hasProxy = true
}
// get proxy address
if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 {
address = []string{prx}
hasProxy = true
}
if prx := os.Getenv("MICRO_NETWORK"); len(prx) > 0 {
// default name
if prx == "service" {
prx = "go.micro.network"
}
service = prx
hasProxy = true
}
if prx := os.Getenv("MICRO_NEWORK_ADDRESS"); len(prx) > 0 {
address = []string{prx}
hasProxy = true
}
return service, address, hasProxy
}