erigon-pulse/cmd/restapi/apis/remote_db_api.go
Alex Sharov b490192e67
Use KV Abstraction in RestAPI (#400)
* Introduce NoValuesCursor. From() method is useless because can be replaced by Seek().`
* implement NoValueCursor interface
* use abstract db in restapi
* cleanup .md
2020-03-24 09:12:55 +07:00

40 lines
808 B
Go

package apis
import (
"context"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/ledgerwatch/turbo-geth/ethdb"
)
func RegisterRemoteDBAPI(router *gin.RouterGroup, e *Env) error {
router.GET("/", e.GetDB)
router.POST("/", e.PostDB)
return nil
}
func (e *Env) GetDB(c *gin.Context) {
var host, port string
split := strings.Split(e.DB.Options().Remote.DialAddress, ":")
if len(split) == 2 {
host, port = split[0], split[1]
}
c.JSON(http.StatusOK, map[string]string{"host": host, "port": port})
}
func (e *Env) PostDB(c *gin.Context) {
newAddr := c.Query("host") + ":" + c.Query("port")
remoteDB, err := ethdb.NewRemote().Path(newAddr).Open(context.TODO())
if err != nil {
c.Error(err) //nolint:errcheck
return
}
e.DB.Close()
e.DB = remoteDB
c.Status(http.StatusOK)
}