From b5d0c9c1a3c543198361c763d16c05cb53107944 Mon Sep 17 00:00:00 2001 From: programmer10110 Date: Fri, 16 Oct 2020 14:00:04 +0300 Subject: [PATCH] get/save an offset for a storage --- client/http_server.go | 45 ++++++++++++++++++++++++++++++++- cmd/dc4bc_cli/main.go | 59 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/client/http_server.go b/client/http_server.go index 95659e7..4eff00f 100644 --- a/client/http_server.go +++ b/client/http_server.go @@ -8,7 +8,6 @@ import ( "io/ioutil" "log" "net/http" - "time" "github.com/depools/dc4bc/client/types" @@ -80,6 +79,9 @@ func (c *BaseClient) StartHTTPServer(listenAddr string) error { mux.HandleFunc("/startDKG", c.startDKGHandler) mux.HandleFunc("/proposeSignMessage", c.proposeSignDataHandler) + mux.HandleFunc("/saveOffset", c.saveOffsetHandler) + mux.HandleFunc("/getOffset", c.getOffsetHandler) + c.Logger.Log("Starting HTTP server on address: %s", listenAddr) return http.ListenAndServe(listenAddr, mux) } @@ -100,6 +102,47 @@ func (c *BaseClient) getPubkeyHandler(w http.ResponseWriter, r *http.Request) { successResponse(w, c.GetPubKey()) } +func (c *BaseClient) getOffsetHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") + return + } + offset, err := c.state.LoadOffset() + if err != nil { + errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to load offset: %v", err)) + return + } + successResponse(w, offset) +} + +func (c *BaseClient) saveOffsetHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") + return + } + reqBytes, err := ioutil.ReadAll(r.Body) + if err != nil { + errorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to read request body: %v", err)) + return + } + defer r.Body.Close() + + var req map[string]uint64 + if err = json.Unmarshal(reqBytes, &req); err != nil { + errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to unmarshal request: %v", err)) + return + } + if _, ok := req["offset"]; !ok { + errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("offset cannot be null: %v", err)) + return + } + if err = c.state.SaveOffset(req["offset"]); err != nil { + errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to save offset: %v", err)) + return + } + successResponse(w, "ok") +} + func (c *BaseClient) sendMessageHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") diff --git a/cmd/dc4bc_cli/main.go b/cmd/dc4bc_cli/main.go index 4c98776..3b89256 100644 --- a/cmd/dc4bc_cli/main.go +++ b/cmd/dc4bc_cli/main.go @@ -12,6 +12,7 @@ import ( "net/http" "path/filepath" "sort" + "strconv" "time" "github.com/depools/dc4bc/fsm/fsm" @@ -56,6 +57,8 @@ func main() { getHashOfStartDKGCommand(), getSignaturesCommand(), getSignatureCommand(), + saveOffsetCommand(), + getOffsetCommand(), ) if err := rootCmd.Execute(); err != nil { log.Fatalf("Failed to execute root command: %v", err) @@ -325,6 +328,62 @@ func getPubKeyCommand() *cobra.Command { } } +func saveOffsetCommand() *cobra.Command { + return &cobra.Command{ + Use: "save_offset [offset]", + Short: "saves a new offset for a storage", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + listenAddr, err := cmd.Flags().GetString(flagListenAddr) + if err != nil { + return fmt.Errorf("failed to read configuration: %v", err) + } + + offset, err := strconv.ParseUint(args[0], 10, 64) + if err != nil { + return fmt.Errorf("failed to parse uint: %w", err) + } + req := map[string]uint64{"offset": offset} + data, err := json.Marshal(req) + if err != nil { + return fmt.Errorf("failed to create request: %w", err) + } + resp, err := rawPostRequest(fmt.Sprintf("http://%s/saveOffset", listenAddr), "application/json", data) + if err != nil { + return fmt.Errorf("failed to save offset: %w", err) + } + if resp.ErrorMessage != "" { + return fmt.Errorf("failed to save offset: %v", resp.ErrorMessage) + } + fmt.Println(resp.Result.(string)) + return nil + }, + } +} + +func getOffsetCommand() *cobra.Command { + return &cobra.Command{ + Use: "get_offset", + Short: "returns a current offset for the storage", + RunE: func(cmd *cobra.Command, args []string) error { + listenAddr, err := cmd.Flags().GetString(flagListenAddr) + if err != nil { + return fmt.Errorf("failed to read configuration: %v", err) + } + + resp, err := rawGetRequest(fmt.Sprintf("http://%s//getOffset", listenAddr)) + if err != nil { + return fmt.Errorf("failed to get offset: %w", err) + } + if resp.ErrorMessage != "" { + return fmt.Errorf("failed to get offset: %v", resp.ErrorMessage) + } + fmt.Println(uint64(resp.Result.(float64))) + return nil + }, + } +} + func getUsernameCommand() *cobra.Command { return &cobra.Command{ Use: "get_username",