Пример #1
0
// Create a backend server
func putServers(rw http.ResponseWriter, req *http.Request) {
	// /services/{svcId}/servers
	svcId := req.URL.Query().Get(":svcId")

	servers := []core.Server{}
	decoder := json.NewDecoder(req.Body)
	if err := decoder.Decode(&servers); err != nil {
		writeError(rw, req, BadJson, http.StatusBadRequest)
		return
	}

	for i := range servers {
		servers[i].GenId()

		// localhost doesn't work properly, use service.Host
		if servers[i].Host == "127.0.0.1" {
			servers[i].GenHost(svcId)
		}
	}

	// add to cluster
	err := cluster.SetServers(svcId, servers)
	if err != nil {
		writeError(rw, req, err, http.StatusInternalServerError)
		return
	}

	writeBody(rw, req, servers, http.StatusOK)
}
Пример #2
0
func TestNoneSetServers(t *testing.T) {
	servers := []core.Server{}
	servers = append(servers, testServer2)
	if err := cluster.SetServers(testService1.Id, servers); err != nil {
		t.Errorf("Failed to SET servers - %v", err)
		t.FailNow()
	}

	service, err := cluster.GetService(testService1.Id)
	if err != nil {
		t.Error(err)
	}

	svc := testService1
	svc.Servers = append(svc.Servers, testServer2)

	if service.Servers[0].Host != svc.Servers[0].Host {
		t.Errorf("Failed to clear old servers on PUT")
	}
}