func getService(c *cli.Context) { rsp, err := command.GetService(c, c.Args()) if err != nil { fmt.Println(err) return } fmt.Println(string(rsp)) }
func queryHealth(c *cli.Context) { rsp, err := command.QueryHealth(c, c.Args()) if err != nil { fmt.Println(err) return } fmt.Println(string(rsp)) }
func getService(c *cli.Context) { if !c.Args().Present() { fmt.Println("Service required") return } service, err := (*cmd.DefaultOptions().Registry).GetService(c.Args().First()) if err != nil { fmt.Println(err.Error()) return } if len(service) == 0 { fmt.Println("Service not found") return } fmt.Printf("service %s\n", service[0].Name) for _, serv := range service { fmt.Println("\nversion ", serv.Version) fmt.Println("\nId\tAddress\tPort\tMetadata") for _, node := range serv.Nodes { var meta []string for k, v := range node.Metadata { meta = append(meta, k+"="+v) } fmt.Printf("%s\t%s\t%d\t%s\n", node.Id, node.Address, node.Port, strings.Join(meta, ",")) } } for _, e := range service[0].Endpoints { var request, response string var meta []string for k, v := range e.Metadata { meta = append(meta, k+"="+v) } if e.Request != nil && len(e.Request.Values) > 0 { request = "{\n" for _, v := range e.Request.Values { request += formatEndpoint(v, 0) } request += "}" } else { request = "{}" } if e.Response != nil && len(e.Response.Values) > 0 { response = "{\n" for _, v := range e.Response.Values { response += formatEndpoint(v, 0) } response += "}" } else { response = "{}" } fmt.Printf("\nEndpoint: %s\nMetadata: %s\n", e.Name, strings.Join(meta, ",")) fmt.Printf("Request: %s\n\nResponse: %s\n", request, response) } }
// TODO: stream via HTTP func streamService(c *cli.Context) { if len(c.Args()) < 2 { fmt.Println("require service and method") return } service := c.Args()[0] method := c.Args()[1] var request map[string]interface{} json.Unmarshal([]byte(strings.Join(c.Args()[2:], " ")), &request) req := (*cmd.DefaultOptions().Client).NewJsonRequest(service, method, request) stream, err := (*cmd.DefaultOptions().Client).Stream(context.Background(), req) if err != nil { fmt.Printf("error calling %s.%s: %v\n", service, method, err) return } if err := stream.Send(request); err != nil { fmt.Printf("error sending to %s.%s: %v\n", service, method, err) return } for { var response map[string]interface{} if err := stream.Recv(&response); err != nil { fmt.Printf("error receiving from %s.%s: %v\n", service, method, err) return } b, _ := json.MarshalIndent(response, "", "\t") fmt.Println(string(b)) // artificial delay time.Sleep(time.Millisecond * 10) } }
func deregisterService(c *cli.Context) { if len(c.Args()) != 1 { fmt.Println("require service definition") return } var service *registry.Service if err := json.Unmarshal([]byte(c.Args().First()), &service); err != nil { fmt.Println(err.Error()) return } if err := (*cmd.DefaultOptions().Registry).Deregister(service); err != nil { fmt.Println(err.Error()) return } }
func queryService(c *cli.Context) { if len(c.Args()) < 2 { fmt.Println("require service and method") return } service := c.Args()[0] method := c.Args()[1] var request map[string]interface{} var response map[string]interface{} if p := c.GlobalString("proxy_address"); len(p) > 0 { request = map[string]interface{}{ "service": service, "method": method, "request": []byte(strings.Join(c.Args()[2:], " ")), } b, err := json.Marshal(request) if err != nil { fmt.Println(err.Error()) return } if err := post(p+"/rpc", b, &response); err != nil { fmt.Println(err.Error()) return } } else { json.Unmarshal([]byte(strings.Join(c.Args()[2:], " ")), &request) req := (*cmd.DefaultOptions().Client).NewJsonRequest(service, method, request) err := (*cmd.DefaultOptions().Client).Call(context.Background(), req, &response) if err != nil { fmt.Printf("error calling %s.%s: %v\n", service, method, err) return } } b, _ := json.MarshalIndent(response, "", "\t") fmt.Println(string(b)) }
func queryHealth(c *cli.Context) { if !c.Args().Present() { fmt.Println("require service name") return } service, err := (*cmd.DefaultOptions().Registry).GetService(c.Args().First()) if err != nil { fmt.Println(err.Error()) return } if service == nil || len(service) == 0 { fmt.Println("Service not found") return } req := (*cmd.DefaultOptions().Client).NewRequest(service[0].Name, "Debug.Health", &proto.HealthRequest{}) fmt.Printf("service %s\n\n", service[0].Name) for _, serv := range service { fmt.Println("\nversion ", serv.Version) fmt.Println("\nnode\t\taddress:port\t\tstatus") for _, node := range serv.Nodes { address := node.Address if node.Port > 0 { address = fmt.Sprintf("%s:%d", address, node.Port) } rsp := &proto.HealthResponse{} err := (*cmd.DefaultOptions().Client).CallRemote(context.Background(), address, req, rsp) var status string if err != nil { status = err.Error() } else { status = rsp.Status } fmt.Printf("%s\t\t%s:%d\t\t%s\n", node.Id, node.Address, node.Port, status) } } }
func queryService(c *cli.Context) { if len(c.Args()) < 2 { fmt.Println("require service and method") return } service := c.Args()[0] method := c.Args()[1] var request map[string]interface{} var response map[string]interface{} json.Unmarshal([]byte(strings.Join(c.Args()[2:], " ")), &request) req := (*cmd.DefaultOptions().Client).NewJsonRequest(service, method, request) err := (*cmd.DefaultOptions().Client).Call(context.Background(), req, &response) if err != nil { fmt.Printf("error calling %s.%s: %v\n", service, method, err) return } b, _ := json.MarshalIndent(response, "", "\t") fmt.Println(string(b)) }
func deregisterService(c *cli.Context) { if len(c.Args()) != 1 { fmt.Println("require service definition") return } if p := c.GlobalString("proxy_address"); len(p) > 0 { if err := del(p+"/registry", []byte(c.Args().First()), nil); err != nil { fmt.Println(err.Error()) } return } var service *registry.Service if err := json.Unmarshal([]byte(c.Args().First()), &service); err != nil { fmt.Println(err.Error()) return } if err := (*cmd.DefaultOptions().Registry).Deregister(service); err != nil { fmt.Println(err.Error()) return } }
func run(ctx *cli.Context) { namespace := ctx.String("namespace") alias := ctx.String("alias") fqdn := ctx.String("fqdn") atype := ctx.String("type") dir := ctx.Args().First() if len(dir) == 0 { fmt.Println("specify service name") return } if len(namespace) == 0 { fmt.Println("namespace not defined") return } if len(atype) == 0 { fmt.Println("type not defined") return } // check if the path is absolute, we don't want this // we want to a relative path so we can install in GOPATH if path.IsAbs(dir) { fmt.Println("require relative path as service will be installed in GOPATH") return } goPath := os.Getenv("GOPATH") // don't know GOPATH, runaway.... if len(goPath) == 0 { fmt.Println("unknown GOPATH") return } // attempt to split path if not windows if runtime.GOOS != "windows" { goPath = strings.Split(goPath, ":")[0] } goDir := filepath.Join(goPath, "src", path.Clean(dir)) if len(alias) == 0 { // set as last part alias = filepath.Base(dir) } if len(fqdn) == 0 { fqdn = strings.Join([]string{namespace, atype, alias}, ".") } var c config switch atype { case "srv": // create srv config c = config{ Alias: alias, Namespace: namespace, Type: atype, FQDN: fqdn, Dir: dir, GoDir: goDir, GoPath: goPath, Files: []file{ {"main.go", tmpl.MainSRV}, {"handler/example.go", tmpl.HandlerSRV}, {"subscriber/example.go", tmpl.SubscriberSRV}, {"proto/example/example.proto", tmpl.ProtoSRV}, {"Dockerfile", tmpl.DockerSRV}, {"README.md", tmpl.Readme}, }, Comments: []string{ "\ndownload protobuf for micro:\n", "go get github.com/micro/protobuf/{proto,protoc-gen-go}", "\ncompile the proto file example.proto:\n", fmt.Sprintf("protoc -I%s \\\n\t--go_out=plugins=micro:%s \\\n\t%s\n", goPath+"/src", goPath+"/src", goDir+"/proto/example/example.proto"), }, } case "api": // create api config c = config{ Alias: alias, Namespace: namespace, Type: atype, FQDN: fqdn, Dir: dir, GoDir: goDir, GoPath: goPath, Files: []file{ {"main.go", tmpl.MainAPI}, {"client/example.go", tmpl.WrapperAPI}, {"handler/example.go", tmpl.HandlerAPI}, {"proto/example/example.proto", tmpl.ProtoAPI}, {"Dockerfile", tmpl.DockerSRV}, {"README.md", tmpl.Readme}, }, Comments: []string{ "\ndownload protobuf for micro:\n", "go get github.com/micro/protobuf/{proto,protoc-gen-go}", "\ncompile the proto file example.proto:\n", fmt.Sprintf("protoc -I%s \\\n\t--go_out=plugins=micro:%s \\\n\t%s\n", goPath+"/src", goPath+"/src", goDir+"/proto/example/example.proto"), }, } case "web": // create srv config c = config{ Alias: alias, Namespace: namespace, Type: atype, FQDN: fqdn, Dir: dir, GoDir: goDir, GoPath: goPath, Files: []file{ {"main.go", tmpl.MainWEB}, {"handler/handler.go", tmpl.HandlerWEB}, {"html/index.html", tmpl.HTMLWEB}, {"Dockerfile", tmpl.DockerWEB}, {"README.md", tmpl.Readme}, }, Comments: []string{}, } default: fmt.Println("Unknown type", atype) return } if err := create(c); err != nil { fmt.Println(err) return } }
func queryHealth(c *cli.Context) { if !c.Args().Present() { fmt.Println("require service name") return } service, err := (*cmd.DefaultOptions().Registry).GetService(c.Args().First()) if err != nil { fmt.Println(err.Error()) return } if service == nil || len(service) == 0 { fmt.Println("Service not found") return } req := (*cmd.DefaultOptions().Client).NewRequest(service[0].Name, "Debug.Health", &proto.HealthRequest{}) // print things fmt.Printf("service %s\n\n", service[0].Name) for _, serv := range service { // print things fmt.Println("\nversion ", serv.Version) fmt.Println("\nnode\t\taddress:port\t\tstatus") // query health for every node for _, node := range serv.Nodes { address := node.Address if node.Port > 0 { address = fmt.Sprintf("%s:%d", address, node.Port) } rsp := &proto.HealthResponse{} var err error if p := c.GlobalString("proxy_address"); len(p) > 0 { // call using proxy request := map[string]interface{}{ "service": service[0].Name, "method": "Debug.Health", "address": address, } b, err := json.Marshal(request) if err != nil { fmt.Println(err.Error()) return } if err := post(p+"/rpc", b, &rsp); err != nil { fmt.Println(err.Error()) return } } else { // call using client err = (*cmd.DefaultOptions().Client).CallRemote(context.Background(), address, req, rsp) } var status string if err != nil { status = err.Error() } else { status = rsp.Status } fmt.Printf("%s\t\t%s:%d\t\t%s\n", node.Id, node.Address, node.Port, status) } } }