示例#1
0
func main() {
	res := Resolver{"echo": reflect.ValueOf(echo), "add": reflect.ValueOf(add)}
	serv := rpc.NewServer(res, true, nil)
	l, err := net.Listen("tcp", "127.0.0.1:50000")
	serv.Listen(l)
	go (func() { serv.Run() })()

	conn, err := net.Dial("tcp", "127.0.0.1:50000")
	if err != nil {
		fmt.Println("fail to connect to server.")
		return
	}
	client := rpc.NewSession(conn, true)

	retval, xerr := client.Send("echo", "world")
	if xerr != nil {
		fmt.Println(xerr)
		return
	}
	fmt.Println(retval.String())

	retval, xerr = client.Send("add", 2, 3)
	if xerr != nil {
		fmt.Println(xerr)
		return
	}
	fmt.Println(rpc.CoerceInt(retval))
}
示例#2
0
/*
Test with this curl command:

curl -s -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"kind":"linux","size":"S","disksize":20,"leasedays":1,"environment_type":"dev","centrify_zone":"zd_fr_cld_01","description":"my_description","usergroup":"clddevsudo01","service_account":"poctst01","app_trigram":"TRG","region":"eu-fr-paris","availability_zone":"eu-fr-paris-1","subnet":"192.160.64.0/21","notifymail":"[email protected],[email protected]"}' -k https://localhost:8080/v2/nodes -u

*/
func NodeCreate(w http.ResponseWriter, r *http.Request) {
	var nodeRequest NodeRequest
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		panic(err)
	}
	if err := r.Body.Close(); err != nil {
		panic(err)
	}
	if err := json.Unmarshal(body, &nodeRequest); err != nil {
		w.Header().Set("Content-Type", "application/json; charset=UTF-8")
		w.WriteHeader(http.StatusBadRequest) // unprocessable entity
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
	}

	conn, err := net.Dial("tcp", "127.0.0.1:18800")
	if err != nil {
		fmt.Println("fail to connect to server.")
		return

	}
	client := rpc.NewSession(conn, true)
	t, err := client.Send("NodeCreate", nodeRequest.Kind, nodeRequest.Size, nodeRequest.Disksize, nodeRequest.Leasedays, nodeRequest.EnvironmentType, nodeRequest.Description)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Println(err)
		return

	}
	fmt.Println(t)

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusAccepted)
	if err := json.NewEncoder(w).Encode(t); err != nil {
		panic(err)
	}
}