github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/proxy/go/proxy.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io/ioutil" 7 "net/http" 8 "net/url" 9 10 "github.com/micro/go-micro/v2/registry" 11 ) 12 13 var ( 14 registryURI = "http://localhost:8081/registry" 15 proxyURI = "http://localhost:8081" 16 ) 17 18 func register(s *registry.Service) { 19 b, _ := json.Marshal(s) 20 http.Post(registryURI, "application/json", bytes.NewReader(b)) 21 } 22 23 func deregister(s *registry.Service) { 24 b, _ := json.Marshal(s) 25 req, _ := http.NewRequest("DELETE", registryURI, bytes.NewReader(b)) 26 req.Header.Set("Content-Type", "application/json") 27 http.DefaultClient.Do(req) 28 } 29 30 func rpcCall(path string, req map[string]interface{}) (string, error) { 31 b, _ := json.Marshal(req) 32 rsp, err := http.Post(proxyURI+path, "application/json", bytes.NewReader(b)) 33 if err != nil { 34 return "", err 35 } 36 defer rsp.Body.Close() 37 b, err = ioutil.ReadAll(rsp.Body) 38 if err != nil { 39 return "", err 40 } 41 return string(b), nil 42 } 43 44 func httpCall(path string, req url.Values) (string, error) { 45 rsp, err := http.PostForm(proxyURI+path, req) 46 if err != nil { 47 return "", err 48 } 49 b, err := ioutil.ReadAll(rsp.Body) 50 if err != nil { 51 return "", err 52 } 53 return string(b), nil 54 }