2 Star 0 Fork 0

eoslh/eos-light-node

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
handler.go 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
Your Name 提交于 2021-11-12 11:38 +08:00 . 1.启动参数es、http添加默认值
package httpserver
import (
"context"
"github.com/gin-gonic/gin"
"github.com/olivere/elastic/v7"
"net/http"
)
//type query struct {
// blocknum string `json: block_num`
// blockid string `json: block_id`
//}
var eser *elastic.Client
func RunHttpServer(bindAddress string, esClient *elastic.Client) {
r := gin.Default()
eser = esClient
r.GET("/v1/chain/get_block", getBlock)
r.Run(bindAddress)
}
func getBlock(ctx *gin.Context) {
blockNum := ctx.DefaultQuery("block_num", "")
blockId := ctx.DefaultQuery("block_id", "")
if len(blockNum) != 0 {
data, err := eser.Get().Index("chainblock").Id(blockNum).Do(context.Background())
if err != nil {
ctx.String(http.StatusInternalServerError, "%s", err.Error())
return
}
json, err2 := data.Source.MarshalJSON()
if err2 != nil {
ctx.String(http.StatusInternalServerError, "%s", err2.Error())
return
}
ctx.String(http.StatusOK, "%s", json)
} else if len(blockId) != 0 {
q := elastic.NewTermQuery("block_id", blockId)
res, err := eser.Search().Index("chainblock").Query(q).Pretty(false).Do(context.Background())
if err != nil {
ctx.String(http.StatusInternalServerError, "%s", err.Error())
return
}
if len(res.Hits.Hits) > 0 {
for _, item := range res.Hits.Hits {
ctx.String(http.StatusOK, "%s", string(item.Source))
}
} else {
ctx.String(http.StatusNotFound, "%s", "NOT FOUND")
}
} else {
ctx.String(http.StatusNotFound, "%s", "NOT FOUND")
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/eoslh/eos-light-node.git
git@gitee.com:eoslh/eos-light-node.git
eoslh
eos-light-node
eos-light-node
v1.0.0

搜索帮助