Skip to content

Commit

Permalink
Add cors support
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed May 16, 2022
1 parent 127f9d7 commit a9dd611
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ make build
./build/gnoapi
```

Custom port and cors enabled.

```
./build/gnoapi --port 1317 --cors
```
15 changes: 14 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gnolang/gno/pkgs/bft/rpc/client"

"github.com/gorilla/mux"
"github.com/rs/cors"

"github.com/disperze/gno-api/cmd/handler"

Expand All @@ -19,6 +20,7 @@ import (
var (
remotePtr = flag.String("remote", "http://gno.land:36657", "Remote rpc")
apiPortPtr = flag.String("port", "8888", "Api port")
corsPtr = flag.Bool("cors", false, "Enable CORS")
)

func main() {
Expand All @@ -32,6 +34,12 @@ func main() {
log.Fatal("api port is required")
}

c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{http.MethodGet, http.MethodPost},
AllowedHeaders: []string{"Content-Type", "Accept"},
})

apiPort := *apiPortPtr
cli := client.NewHTTP(*remotePtr, "/websocket")

Expand All @@ -45,6 +53,11 @@ func main() {
r.HandleFunc("/txs/decode", handler.TxDecodeHandler(cli)).Methods(http.MethodGet)
r.HandleFunc("/txs", handler.TxsHandler(cli)).Methods(http.MethodPost)

var h http.Handler = r
if *corsPtr {
h = c.Handler(r)
}

fmt.Println("Running on port", apiPort)
log.Fatal(http.ListenAndServe(":"+apiPort, r))
log.Fatal(http.ListenAndServe(":"+apiPort, h))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/gnolang/gno v0.0.0-20220505214555-31c139670944
github.com/gorilla/mux v1.8.0
github.com/rs/cors v1.8.2
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down

0 comments on commit a9dd611

Please sign in to comment.