Ejemplo n.º 1
0
func rpcHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer r.Body.Close()

	var service, method string
	var request interface{}

	// response content type
	w.Header().Set("Content-Type", "application/json")

	switch r.Header.Get("Content-Type") {
	case "application/json":
		b, err := ioutil.ReadAll(r.Body)
		if err != nil {
			e := errors.BadRequest("go.micro.api", err.Error())
			w.WriteHeader(400)
			w.Write([]byte(e.Error()))
			return
		}

		var body map[string]interface{}
		err = json.Unmarshal(b, &body)
		if err != nil {
			e := errors.BadRequest("go.micro.api", err.Error())
			w.WriteHeader(400)
			w.Write([]byte(e.Error()))
			return
		}

		service = body["service"].(string)
		method = body["method"].(string)
		request = body["request"]
	default:
		r.ParseForm()
		service = r.Form.Get("service")
		method = r.Form.Get("method")
		json.Unmarshal([]byte(r.Form.Get("request")), &request)
	}

	var response map[string]interface{}
	req := client.NewJsonRequest(service, method, request)
	err := client.Call(context.Background(), req, &response)
	if err != nil {
		log.Errorf("Error calling %s.%s: %v", service, method, err)
		ce := errors.Parse(err.Error())
		switch ce.Code {
		case 0:
			w.WriteHeader(500)
		default:
			w.WriteHeader(int(ce.Code))
		}
		w.Write([]byte(ce.Error()))
		return
	}

	b, _ := json.Marshal(response)
	w.Header().Set("Content-Length", strconv.Itoa(len(b)))
	w.Write(b)
}
Ejemplo n.º 2
0
func Commands() []cli.Command {
	commands := []cli.Command{
		{
			Name:        "registry",
			Usage:       "Query registry",
			Subcommands: registryCommands(),
		},
		{
			Name:  "query",
			Usage: "Query a service method using rpc",
			Action: func(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 := client.NewJsonRequest(service, method, request)
				err := 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))
			},
		},
		{
			Name:  "health",
			Usage: "Query the health of a service",
			Action: func(c *cli.Context) {
				if !c.Args().Present() {
					fmt.Println("require service name")
					return
				}
				service, err := registry.GetService(c.Args().First())
				if err != nil {
					fmt.Printf("error querying registry: %v", err)
					return
				}
				fmt.Println("node\t\taddress:port\t\tstatus")
				req := client.NewRequest(service.Name, "Debug.Health", &health.Request{})
				for _, node := range service.Nodes {
					address := node.Address
					if node.Port > 0 {
						address = fmt.Sprintf("%s:%d", address, node.Port)
					}
					rsp := &health.Response{}
					err := 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)
				}
			},
		},
	}

	return append(commands, registryCommands()...)
}