Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
feat: add cors support
Browse files Browse the repository at this point in the history
  • Loading branch information
pinglin committed May 22, 2022
1 parent 47df1b1 commit 5ac2a53
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
27 changes: 17 additions & 10 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"syscall"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/cors"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
Expand All @@ -33,16 +34,22 @@ import (
connectorPB "github.com/instill-ai/protogen-go/connector/v1alpha"
)

func grpcHandlerFunc(grpcServer *grpc.Server, gwHandler http.Handler) http.Handler {
func grpcHandlerFunc(grpcServer *grpc.Server, gwHandler http.Handler, CORSOrigins []string) http.Handler {
return h2c.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
gwHandler.ServeHTTP(w, r)
}
}),
&http2.Server{})
cors.New(cors.Options{
AllowedOrigins: CORSOrigins,
AllowCredentials: true,
Debug: false,
}).Handler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
gwHandler.ServeHTTP(w, r)
}
})),
&http2.Server{},
)
}

func main() {
Expand Down Expand Up @@ -141,7 +148,7 @@ func main() {

httpServer := &http.Server{
Addr: fmt.Sprintf(":%v", config.Config.Server.Port),
Handler: grpcHandlerFunc(grpcS, gwS),
Handler: grpcHandlerFunc(grpcS, gwS, config.Config.Server.CORSOrigins),
}

// Wait for interrupt signal to gracefully shutdown the server with a timeout of 5 seconds.
Expand Down
9 changes: 6 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ func Init() error {
logger.Fatal(err.Error())
}

if err := k.Load(env.Provider("CFG_", ".", func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, "CFG_")), "_", ".", -1)
if err := k.Load(env.ProviderWithValue("CFG_", ".", func(s string, v string) (string, interface{}) {
key := strings.Replace(strings.ToLower(strings.TrimPrefix(s, "CFG_")), "_", ".", -1)
if strings.Contains(v, ",") {
return key, strings.Split(strings.TrimSpace(v), ",")
}
return key, v
}), nil); err != nil {
logger.Fatal(err.Error())
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ require (
require (
github.com/instill-ai/protogen-go v0.1.5-alpha.0.20220519185924-775d2feab526
github.com/instill-ai/x v0.1.0-alpha.0.20220517204940-5a70916ce425
github.com/jackc/pgconn v1.11.0
github.com/mennanov/fieldmask-utils v0.5.0
github.com/rs/cors v1.8.2
)

require (
Expand All @@ -37,7 +39,6 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.11.0
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
Expand Down

0 comments on commit 5ac2a53

Please sign in to comment.