// 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) }
// Get information about a backend server func getServer(rw http.ResponseWriter, req *http.Request) { // /services/{svcId}/servers/{srvId} svcId := req.URL.Query().Get(":svcId") srvId := req.URL.Query().Get(":srvId") server, err := common.GetServer(svcId, srvId) if err != nil { writeError(rw, req, err, http.StatusNotFound) return } writeBody(rw, req, server, http.StatusOK) }
// DeleteServer tells all members to remove the server from their database. // rolls back on failure func (r *Redis) DeleteServer(svcId, srvId string) error { conn := pool.Get() defer conn.Close() oldServer, err := common.GetServer(svcId, srvId) // mustn't return nil here to ensure cluster removes the server if err != nil && !strings.Contains(err.Error(), "No Server Found") { return err } // publishStringJson to others // todo: swap srv/svc ids to match backender interface for better readability err = r.publishString(conn, "delete-server", srvId, svcId) if err != nil { return err } actionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("delete-server %s %s", srvId, svcId)))) // ensure all members applied action err = r.waitForMembers(conn, actionHash) if err != nil { uActionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-server %s %s", *oldServer, svcId)))) // cleanup rollback cruft. clear actionHash ensures no mistakes on re-submit defer conn.Do("DEL", uActionHash, actionHash) // attempt rollback - no need to waitForMembers here uerr := r.publishStringJson(conn, "set-server", svcId, oldServer) if uerr != nil { err = fmt.Errorf("%v - %v", err, uerr) } return err } if database.CentralStore { return database.DeleteServer(svcId, srvId) } return nil }
func (n None) GetServer(svcId, srvId string) (*core.Server, error) { return common.GetServer(svcId, srvId) }