Skip to content

Commit

Permalink
API - Switch to GIN and use cors
Browse files Browse the repository at this point in the history
  • Loading branch information
dviejokfs committed Jul 9, 2022
1 parent 40a9205 commit db31992
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 19 deletions.
53 changes: 41 additions & 12 deletions api/cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/hyperledger/fabric-gateway/pkg/client"
"github.com/hyperledger/fabric-gateway/pkg/identity"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
Expand All @@ -22,6 +24,7 @@ import (
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"io"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"net/http"
Expand Down Expand Up @@ -177,28 +180,54 @@ func (s serveCmd) run() error {
Cache: lru.New(100),
})
h.Use(apollotracing.Tracer{})
playgroundHandler := playground.Handler("GraphQL", "/graphql")

graphqlHandler := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin", "*")
writer.Header().Set("Access-Control-Allow-Credentials", "true")
writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With, X-Identity")
writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
h.ServeHTTP(writer, request)
serverMux := gin.Default()
serverMux.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "PUT", "PATCH"},
AllowHeaders: []string{"X-MC-User", "X-MC-MSPID", "Authorization", "Content-Type"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return true
},
MaxAge: 12 * time.Hour,
}))
graphqlHandler := gin.HandlerFunc(func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With, X-Identity")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
h.ServeHTTP(c.Writer, c.Request)
})
serverMux := http.NewServeMux()
serverMux.HandleFunc(
serverMux.Any(
"/graphql",
graphqlHandler,
)
serverMux.HandleFunc(
serverMux.GET(
"/playground",
playgroundHandler,
playgroundHandler(),
)
serverMux.GET(
"/healthz",
func(c *gin.Context) {
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Header().Set("Content-Type", "application/json")
io.WriteString(c.Writer, `{"alive": true}`)
},
)
log.Infof("Server listening on %s", s.address)
return http.ListenAndServe(s.address, serverMux)
}

// Defining the Playground handler
func playgroundHandler() gin.HandlerFunc {
h := playground.Handler("GraphQL", "/graphql")

return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}

// newGrpcConnection creates a gRPC connection to the Gateway server.
func newGrpcConnection(peerEndpoint string, tlsCert []byte) (*grpc.ClientConn, error) {
certificate, err := identity.CertificateFromPEM(tlsCert)
Expand Down
17 changes: 14 additions & 3 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ require (
)

require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/golang/protobuf v1.5.2
github.com/hyperledger/fabric-protos-go v0.0.0-20220315113721-7dc293e117f7
github.com/kfsoftware/hlf-operator v1.6.4
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.10.1
google.golang.org/grpc v1.43.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -33,9 +35,14 @@ require (
github.com/cloudflare/cfssl v1.4.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-kit/kit v0.10.0 // indirect
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/certificate-transparency-go v1.0.21 // indirect
Expand All @@ -49,14 +56,17 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/operator-framework/operator-lib v0.1.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.10.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand All @@ -66,8 +76,9 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/weppos/publicsuffix-go v0.5.0 // indirect
github.com/zmap/zcrypto v0.0.0-20190729165852-9051775e6a2e // indirect
github.com/zmap/zlint v0.0.0-20190806154020-fd021b4cfbeb // indirect
Expand All @@ -80,7 +91,7 @@ require (
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit db31992

Please sign in to comment.