func (s *Server) writeHandler(w http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) // Read the value from the POST body. b, err := ioutil.ReadAll(req.Body) if err != nil { w.WriteHeader(http.StatusBadRequest) return } value := string(b) // Execute the command against the Raft server. //_, err = s.raftServer.Do(command.NewSetCommand(vars["key"], value)) _, err = s.raftServer.Do(command.NewSetCommand(vars["key"], value, "", uint32(Verb_SET), "")) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } }
func (s *Server) requestAction(reqItem *ServerRequestItem) { req := reqItem.Req addr := reqItem.Addr value := req.GetValue() tag := req.GetTag() path := req.GetPath() verb := req.GetVerb() //log.Printf("serRecv [%v]\n", req.String()) var resp *Response opServerInner := verb&uint32(Verb_SERVER) > 0 if opServerInner { jsonStr := s.serverLevelPro(addr, value) if len(jsonStr) > 0 { resp = NewRespByReq(req, jsonStr, Err_OK) } } else { opGet := verb&uint32(Verb_GET) > 0 opWatch := verb&uint32(Verb_WATCH) > 0 opWatchAll := verb&uint32(Verb_WATCH_ALL) > 0 opPat := false if (opWatch || opWatchAll) || opGet { opPat = strings.ContainsAny(path, "*?") } if opWatch || opWatchAll { opCreate := verb&uint32(Verb_CREATE) > 0 opDel := verb&uint32(Verb_DEL) > 0 //fmt.Printf("opWatch: [%v]\n", path) if opCreate { s.fs.SetWatch(path, addr, tag, opWatchAll) resp = NewRespByReq(req, value, Err_OK) } else if opDel { s.fs.DelWatch(path, addr) resp = NewRespByReq(req, value, Err_OK) } } else if opGet { value, verB, err := s.fs.Get(path, opPat) resp = NewRespByReq(req, value, err) verb32 := uint32(verB) resp.Verb = &verb32 } else { //处理Raft的数据 if strings.HasPrefix(path, KRootDir) { //log.Printf("WK 1 requestAction %v %v [%v]\n", path, KRootDir, req.String()) resp = NewRespByReq(req, value, Err_ROOT_IS_READONLY) } else { // log.Printf("WK 2 requestAction %v %v [%v]\n", path, KRootDir, req.String()) //log.Printf("serDoStart: %v\n", tag) resp = s.serverDo(req, command.NewSetCommand(path, value, tag, verb, addr)) //log.Printf("serDoEnd: %v\n", tag) } } } if resp == nil { resp = NewRespByReq(req, value, Err_UNKNOWN) } // log.Printf("serLog: \n%v\n%v\n", req.String(), resp.String()) s.sendRespQ <- &ServerResponceItem{Addr: addr, Resp: resp} }