Commit a330bbc2 authored by 李伟@五瓣科技's avatar 李伟@五瓣科技

txs list page

parent c2d0ede1
package multisend
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
......@@ -165,12 +166,34 @@ func (web *WebServicer) GetTreeHandler(w http.ResponseWriter, r *http.Request) {
}
type resp struct {
HashList []string `json:"hash_list"`
Total uint64 `json:"total"`
}
func (web *WebServicer) GetTxsList(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hashStr := vars["hash"]
// hashAsBytes := []byte(hashStr)
startAsStr := r.URL.Query().Get("start")
numStr := r.URL.Query().Get("num")
startAsInt, err := strconv.Atoi(startAsStr)
if err != nil {
startAsInt = 0
}
numInt, err := strconv.Atoi(numStr)
if err != nil {
numInt = 10
}
if startAsInt != 0 {
startAsInt = startAsInt - 1
}
fmt.Printf("startAsStr: %s offsetStr: %s startAs: %d offset: %d \n", startAsStr, numStr, startAsInt, numInt)
hashAsBytes, err := base64.StdEncoding.DecodeString(hashStr)
......@@ -179,12 +202,38 @@ func (web *WebServicer) GetTxsList(w http.ResponseWriter, r *http.Request) {
return
}
// if err := json.Unmarshal(hashStr, &hashAsBytes); err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// return
// }
total, err := redisCli.LLen(context.Background(), fmt.Sprintf("%x", hashAsBytes)).Uint64()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
res, err := redisCli.LRange(context.Background(), fmt.Sprintf("%x", hashAsBytes), int64(startAsInt*numInt), int64((startAsInt+1)*numInt)-1).Result()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Printf("hashStr: %s len(hashStr): %d len(hashAsBytes): %d int64(startAsInt*numInt): %d int64((startAsInt+1)*numInt): %d res: %v \n", hashStr, len(hashStr), len(hashAsBytes), int64(startAsInt*numInt), int64((startAsInt+1)*numInt-1), res)
resp := resp{
HashList: res,
Total: total,
}
recordAsJon, err := json.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(recordAsJon)
return
fmt.Printf("hashStr: %s len(hashStr): %d len(hashAsBytes): %d \n", hashStr, len(hashStr), len(hashAsBytes))
}
func (web *WebServicer) WebService(config Config) error {
......
......@@ -13,6 +13,8 @@ import (
"golang.org/x/time/rate"
)
var redisCli *redis.Client
func initClient(poolSize int, redisAddr, passwd string) *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
......@@ -36,6 +38,11 @@ func Start(redisAddr, passwd string) {
logger := logging.NewLogrusLogger(fmt.Sprintf("redis[%s]", redisAddr))
client := initClient(10, redisAddr, passwd)
if redisCli == nil {
redisCli = client
}
count := 0
limiter := rate.NewLimiter(rate.Every(time.Millisecond*100), 1)
cxt, _ := context.WithCancel(context.TODO())
......@@ -78,14 +85,19 @@ func Start(redisAddr, passwd string) {
panic(err)
}
hashlistAsJson, err := json.Marshal(hashs)
// hashlistAsJson, err := json.Marshal(hashs)
if err != nil {
panic(err)
}
// if err != nil {
// panic(err)
// }
if err := client.LPush(context.Background(), fmt.Sprintf("%x", batchTxs.Hash), hashlistAsJson); err != nil {
panic(err)
for k, v := range hashs {
fmt.Printf("idx: %d key: %s v: %s \n", k, fmt.Sprintf("%x", batchTxs.Hash), v.Hex())
if err := client.LPush(context.Background(), fmt.Sprintf("%x", batchTxs.Hash), v.Hex()); err != nil {
//panic(err)
}
}
count += 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment