2020-02-09 10:31:52 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2020-03-11 11:02:37 +00:00
|
|
|
"context"
|
2020-02-09 10:31:52 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/cmd/restapi/apis"
|
2020-03-11 11:02:37 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb/remote"
|
2020-02-09 10:31:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-03-11 11:02:37 +00:00
|
|
|
remoteDB, err := remote.Open(context.TODO(), remote.DefaultOpts.Addr(remoteDbAddress))
|
2020-02-09 10:31:52 +00:00
|
|
|
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
|
|
|
|
}
|
2020-03-16 22:00:48 +00:00
|
|
|
if err = apis.RegisterStorageTombstonesAPI(root.Group("storage-tombstones"), remoteDB); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-09 10:31:52 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|