// Create a backend server func postServer(rw http.ResponseWriter, req *http.Request) { // /services/{svcId}/servers svcId := req.URL.Query().Get(":svcId") server, err := parseReqServer(req) if err != nil { writeError(rw, req, err, http.StatusBadRequest) return } // idempotent additions (don't update server on post) if srv, _ := common.GetServer(svcId, server.Id); srv != nil { writeBody(rw, req, server, http.StatusOK) return } // localhost doesn't work properly, use service.Host if server.Host == "127.0.0.1" { server.GenHost(svcId) } // save to cluster err = cluster.SetServer(svcId, server) if err != nil { writeError(rw, req, err, http.StatusInternalServerError) return } // todo: or service (which would include server) writeBody(rw, req, server, http.StatusOK) }
//////////////////////////////////////////////////////////////////////////////// // SERVERS //////////////////////////////////////////////////////////////////////////////// func TestNoneSetServer(t *testing.T) { cluster.SetService(&testService1) if err := cluster.SetServer(testService1.Id, &testServer1); err != nil { t.Errorf("Failed to SET server - %v", err) t.FailNow() } service, err := cluster.GetService(testService1.Id) if err != nil { t.Error(err) } svc := testService1 svc.Servers = append(svc.Servers, testServer1) if service.Servers[0].Host != svc.Servers[0].Host { t.Errorf("Read service differs from written service") } }