func registryCommands() []cli.Command { return []cli.Command{ { Name: "list", Usage: "List items in registry", Subcommands: []cli.Command{ { Name: "services", Usage: "List services in registry", Action: func(c *cli.Context) { rsp, err := registry.ListServices() if err != nil { fmt.Println(err.Error()) return } for _, service := range rsp { fmt.Println(service.Name) } }, }, }, }, { Name: "get", Usage: "Get item from registry", Subcommands: []cli.Command{ { Name: "service", Usage: "Get service from registry", Action: func(c *cli.Context) { if !c.Args().Present() { fmt.Println("Service required") return } service, err := registry.GetService(c.Args().First()) if err != nil { fmt.Println(err.Error()) return } fmt.Printf("%s\n\n", service.Name) fmt.Println("Id\tAddress\tPort\tMetadata") for _, node := range service.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, ",")) } }, }, }, }, } }
func registryHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() svc := r.Form.Get("service") if len(svc) > 0 { s, err := registry.GetService(svc) if err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } t, err := template.New("service").Funcs(template.FuncMap{ "format": format, }).Parse(serviceTemplate) if err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } if err := t.ExecuteTemplate(w, "T", s); err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } return } services, err := registry.ListServices() if err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } t, err := template.New("registry").Parse(registryTemplate) if err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } if err := t.ExecuteTemplate(w, "T", services); err != nil { http.Error(w, "Error occurred:"+err.Error(), 500) return } }
func registryCommands() []cli.Command { return []cli.Command{ { Name: "list", Usage: "List items in registry", Subcommands: []cli.Command{ { Name: "services", Usage: "List services in registry", Action: func(c *cli.Context) { rsp, err := registry.ListServices() if err != nil { fmt.Println(err.Error()) return } for _, service := range rsp { fmt.Println(service.Name) } }, }, }, }, { Name: "get", Usage: "Get item from registry", Subcommands: []cli.Command{ { Name: "service", Usage: "Get service from registry", Action: func(c *cli.Context) { if !c.Args().Present() { fmt.Println("Service required") return } service, err := registry.GetService(c.Args().First()) if err != nil { fmt.Println(err.Error()) return } fmt.Printf("%s\n\n", service.Name) fmt.Println("Id\tAddress\tPort\tMetadata") for _, node := range service.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.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 = formatEndpoint(e.Request.Values[0], 0) } else { request = " {}" } if e.Response != nil && len(e.Response.Values) > 0 { response = formatEndpoint(e.Response.Values[0], 0) } else { response = " {}" } fmt.Printf("\nEndpoint: %s\nMetadata: %s\n", e.Name, strings.Join(meta, ",")) fmt.Printf("Request:%s\nResponse:%s\n", request, response) } }, }, }, }, } }