erigon-pulse/cmd/restapi/rest/serve_rest.go
2020-02-09 13:31:52 +03:00

53 lines
1.1 KiB
Go

package rest
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/ledgerwatch/turbo-geth/cmd/restapi/apis"
"github.com/ledgerwatch/turbo-geth/cmd/restapi/util"
)
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 := util.ConnectRemoteDB(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()
})
}