Merge pull request #62 from lidofinance/feat/save-offset-cli

get/save an offset for a storage
This commit is contained in:
Andrew Zavgorodny 2020-10-30 14:35:51 +03:00 committed by GitHub
commit 0dbe500c05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 103 additions and 1 deletions

View File

@ -8,7 +8,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"time" "time"
"github.com/depools/dc4bc/client/types" "github.com/depools/dc4bc/client/types"
@ -80,6 +79,9 @@ func (c *BaseClient) StartHTTPServer(listenAddr string) error {
mux.HandleFunc("/startDKG", c.startDKGHandler) mux.HandleFunc("/startDKG", c.startDKGHandler)
mux.HandleFunc("/proposeSignMessage", c.proposeSignDataHandler) mux.HandleFunc("/proposeSignMessage", c.proposeSignDataHandler)
mux.HandleFunc("/saveOffset", c.saveOffsetHandler)
mux.HandleFunc("/getOffset", c.getOffsetHandler)
mux.HandleFunc("/getFSMDump", c.getFSMDumpHandler) mux.HandleFunc("/getFSMDump", c.getFSMDumpHandler)
mux.HandleFunc("/getFSMList", c.getFSMList) mux.HandleFunc("/getFSMList", c.getFSMList)
@ -138,6 +140,47 @@ func (c *BaseClient) getPubkeyHandler(w http.ResponseWriter, r *http.Request) {
successResponse(w, c.GetPubKey()) 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) { func (c *BaseClient) sendMessageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")

View File

@ -13,6 +13,7 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
@ -58,6 +59,8 @@ func main() {
getHashOfStartDKGCommand(), getHashOfStartDKGCommand(),
getSignaturesCommand(), getSignaturesCommand(),
getSignatureCommand(), getSignatureCommand(),
saveOffsetCommand(),
getOffsetCommand(),
getFSMStatusCommand(), getFSMStatusCommand(),
getFSMListCommand(), getFSMListCommand(),
) )
@ -329,6 +332,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 { func getUsernameCommand() *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "get_username", Use: "get_username",