erigon-pulse/cmd/restapi/rest/serve_rest.go
Alex Sharov c5ffc971c5
[WIP] Badger v2 (#378)
* badger v2 investigation

* buf pool - use native New method and avoid double checks

* db.Open prototype

* db.Tx/Bucket/Cursor prototypes

* Chained config

* Item concept added

* save changes to test on master

* make hack resumable

* Design document v0

* Cursor concept

* less brackets syntax of cursor builder

* benchmarks

* cleanup fs

* test for context cancelations

* test for context cancelations

* test for cursor.Prefix option

* add ForEachKey method

* add ForEachKey method

* add naming explanation

* experiment of non-pointers cursor/bucket

* .Bucket() and .Cursor() doesn't returns error

* .Bucket() and .Cursor() doesn't returns error

* .Bucket() and .Cursor() doesn't returns error

* remove CursorOpts concept

* more test-cases

* simplify open api

* Tx, Bucket, Cursor - now are interfaces

* Tx, Bucket, Cursor - now are interfaces

* switch to interfaces

* rebase master

Co-authored-by: alex.sharov <alex.sharov@lazada.com>
2020-03-11 11:02:37 +00:00

54 lines
1.1 KiB
Go

package rest
import (
"context"
"fmt"
"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 {
fmt.Printf("%v: SUCCESS", name)
} else {
fmt.Printf("%v: FAIL (err=%v)", name, err)
}
}
func ServeREST(localAddress, remoteDbAddress string) error {
r := gin.Default()
root := r.Group("api/v1")
allowCORS(root)
remoteDB, err := remote.Open(context.TODO(), remote.DefaultOpts.Addr(remoteDbAddress))
if err != nil {
return err
}
defer func() {
printError("Closing Remote DB", remoteDB.Close())
}()
if err = apis.RegisterAccountAPI(root.Group("accounts"), remoteDB); err != nil {
return err
}
fmt.Printf("serving on %v... press ctrl+C to abort\n", localAddress)
r.Run(localAddress) //nolint:errcheck
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()
})
}