erigon-pulse/cmd/restapi/rest/serve_rest.go
Alex Sharov 84ae2eb1f8
Intermediate Hashes UI (#395)
* implement NoValueCursor interface

* cleanup

* fix tests

* add more stats data to ui

* can't display error

* re-open DB low-level net interface when changing db

* re-open DB low-level net interface when changing db

* fix problem with displaying errors

* run ci

* improve prop check logic

* storage tombstones integrity checks UI

* storage page

* make DB configurable

Co-authored-by: alex.sharov <alex.sharov@lazada.com>
2020-03-20 10:06:14 +00:00

76 lines
1.6 KiB
Go

package rest
import (
"context"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/ledgerwatch/turbo-geth/cmd/restapi/apis"
"github.com/ledgerwatch/turbo-geth/ethdb/remote"
)
func printError(name string, err error) {
if err != nil {
log.Printf("%v: SUCCESS", name)
} else {
log.Printf("%v: FAIL (err=%v)", name, err)
}
}
func ServeREST(localAddress, remoteDbAddress string) error {
r := gin.Default()
root := r.Group("api/v1")
allowCORS(root)
root.Use(func(c *gin.Context) {
c.Next()
if len(c.Errors) > 0 {
c.AbortWithStatusJSON(http.StatusInternalServerError, c.Errors)
}
})
db, err := remote.Open(context.Background(), remote.DefaultOpts.Addr(remoteDbAddress))
if err != nil {
return err
}
e := &apis.Env{
DB: db,
}
//defer func() {
// printError("Closing Remote DB", remoteDB.Close())
//}()
if err = apis.RegisterRemoteDBAPI(root.Group("remote-db"), e); err != nil {
return err
}
if err = apis.RegisterAccountAPI(root.Group("accounts"), e); err != nil {
return err
}
if err = apis.RegisterStorageAPI(root.Group("storage"), e); err != nil {
return err
}
if err = apis.RegisterStorageTombstonesAPI(root.Group("storage-tombstones"), e); err != nil {
return err
}
log.Printf("serving on %v... press ctrl+C to abort\n", localAddress)
err = r.Run(localAddress) //nolint:errcheck
if err != nil {
return err
}
return nil
}
func allowCORS(r *gin.RouterGroup) {
r.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "Content-Type")
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Next()
})
}