func Start(httpAddr string, rep *representative.Representative) { http.HandleFunc("/total_resources", func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(rep.TotalResources()) }) http.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(rep.Instances()) }) http.HandleFunc("/reset", func(w http.ResponseWriter, r *http.Request) { rep.Reset() }) http.HandleFunc("/set_instances", func(w http.ResponseWriter, r *http.Request) { var instances []instance.Instance err := json.NewDecoder(r.Body).Decode(&instances) if err != nil { w.WriteHeader(http.StatusBadRequest) return } rep.SetInstances(instances) }) http.HandleFunc("/vote", func(w http.ResponseWriter, r *http.Request) { var inst instance.Instance err := json.NewDecoder(r.Body).Decode(&inst) if err != nil { w.WriteHeader(http.StatusBadRequest) return } score, err := rep.Vote(inst) if err != nil { w.WriteHeader(http.StatusServiceUnavailable) return } json.NewEncoder(w).Encode(score) }) http.HandleFunc("/reserve_and_recast_vote", func(w http.ResponseWriter, r *http.Request) { var inst instance.Instance err := json.NewDecoder(r.Body).Decode(&inst) if err != nil { w.WriteHeader(http.StatusBadRequest) return } score, err := rep.ReserveAndRecastVote(inst) if err != nil { w.WriteHeader(http.StatusServiceUnavailable) return } json.NewEncoder(w).Encode(score) }) http.HandleFunc("/release", func(w http.ResponseWriter, r *http.Request) { var inst instance.Instance err := json.NewDecoder(r.Body).Decode(&inst) if err != nil { w.WriteHeader(http.StatusBadRequest) return } rep.Release(inst) w.WriteHeader(http.StatusOK) }) http.HandleFunc("/claim", func(w http.ResponseWriter, r *http.Request) { var inst instance.Instance err := json.NewDecoder(r.Body).Decode(&inst) if err != nil { w.WriteHeader(http.StatusBadRequest) return } rep.Claim(inst) w.WriteHeader(http.StatusOK) }) fmt.Printf("[%s] serving http on %s\n", rep.Guid(), httpAddr) panic(http.ListenAndServe(httpAddr, nil)) }
func Start(natsAddrs []string, rep *representative.Representative) { client := yagnats.NewClient() clusterInfo := &yagnats.ConnectionCluster{} for _, addr := range natsAddrs { clusterInfo.Members = append(clusterInfo.Members, &yagnats.ConnectionInfo{ Addr: addr, }) } err := client.Connect(clusterInfo) if err != nil { log.Fatalln("no nats:", err) } guid := rep.Guid() client.Subscribe(guid+".guid", func(msg *yagnats.Message) { jguid, _ := json.Marshal(rep.Guid()) client.Publish(msg.ReplyTo, jguid) }) client.Subscribe(guid+".total_resources", func(msg *yagnats.Message) { jresources, _ := json.Marshal(rep.TotalResources()) client.Publish(msg.ReplyTo, jresources) }) client.Subscribe(guid+".reset", func(msg *yagnats.Message) { rep.Reset() client.Publish(msg.ReplyTo, successResponse) }) client.Subscribe(guid+".set_instances", func(msg *yagnats.Message) { var instances []instance.Instance err := json.Unmarshal(msg.Payload, &instances) if err != nil { client.Publish(msg.ReplyTo, errorResponse) } rep.SetInstances(instances) client.Publish(msg.ReplyTo, successResponse) }) client.Subscribe(guid+".instances", func(msg *yagnats.Message) { jinstances, _ := json.Marshal(rep.Instances()) client.Publish(msg.ReplyTo, jinstances) }) client.Subscribe(guid+".vote", func(msg *yagnats.Message) { var inst instance.Instance err := json.Unmarshal(msg.Payload, &inst) if err != nil { panic(err) } response := types.VoteResult{ Rep: guid, } defer func() { payload, _ := json.Marshal(response) client.Publish(msg.ReplyTo, payload) }() score, err := rep.Vote(inst) if err != nil { // log.Println(guid, "failed to vote:", err) response.Error = err.Error() return } response.Score = score }) client.Subscribe(guid+".reserve_and_recast_vote", func(msg *yagnats.Message) { var inst instance.Instance responsePayload := errorResponse defer func() { client.Publish(msg.ReplyTo, responsePayload) }() err := json.Unmarshal(msg.Payload, &inst) if err != nil { // log.Println(guid, "invalid reserve_and_recast_vote request:", err) return } score, err := rep.ReserveAndRecastVote(inst) if err != nil { // log.Println(guid, "failed to reserve_and_recast_vote:", err) return } responsePayload, _ = json.Marshal(score) }) client.Subscribe(guid+".release", func(msg *yagnats.Message) { var inst instance.Instance responsePayload := errorResponse defer func() { client.Publish(msg.ReplyTo, responsePayload) }() err := json.Unmarshal(msg.Payload, &inst) if err != nil { log.Println(guid, "invalid reserve_and_recast_vote request:", err) return } rep.Release(inst) responsePayload = successResponse }) client.Subscribe(guid+".claim", func(msg *yagnats.Message) { var inst instance.Instance responsePayload := errorResponse defer func() { client.Publish(msg.ReplyTo, responsePayload) }() err := json.Unmarshal(msg.Payload, &inst) if err != nil { log.Println(guid, "invalid reserve_and_recast_vote request:", err) return } rep.Claim(inst) responsePayload = successResponse }) fmt.Printf("[%s] listening for nats\n", guid) select {} }