Skip to content

Commit

Permalink
Add cors support for eth rpc (#1409)
Browse files Browse the repository at this point in the history
* Add cors support for eth rpc

* drop literal
  • Loading branch information
rongyi authored Jun 3, 2022
1 parent 821e66e commit e5cf4f1
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion http/ethrpc/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
cfg "github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/http/ethrpc/eth"
Expand All @@ -31,6 +32,16 @@ import (
tp "github.com/ontio/ontology/txnpool/proc"
)

var (
vhosts = []string{
"*",
}
// just like 20336/20334's Access-Control-Allow-Origin
cors = []string{
"*",
}
)

func StartEthServer(txpool *tp.TXPoolServer) error {
log.Root().SetHandler(utils.OntLogHandler())
ethAPI := eth.NewEthereumAPI(txpool)
Expand All @@ -49,9 +60,14 @@ func StartEthServer(txpool *tp.TXPoolServer) error {
if err != nil {
return err
}
err = http.ListenAndServe(":"+strconv.Itoa(int(cfg.DefConfig.Rpc.EthJsonPort)), server)

// add cors wrapper
wrappedCORSHandler := node.NewHTTPHandlerStack(server, cors, vhosts)

err = http.ListenAndServe(":"+strconv.Itoa(int(cfg.DefConfig.Rpc.EthJsonPort)), wrappedCORSHandler)
if err != nil {
return err
}

return nil
}

0 comments on commit e5cf4f1

Please sign in to comment.