Skip to content

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 e9790af commit c847912
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 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 @@ -35,16 +36,22 @@ import (
pipelinePB "github.com/instill-ai/protogen-go/pipeline/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 @@ -154,7 +161,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 @@ -116,9 +116,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 {
return err
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/instill-ai/x v0.1.0-alpha.0.20220517204940-5a70916ce425
github.com/knadh/koanf v1.4.0
github.com/mennanov/fieldmask-utils v0.5.0
github.com/rs/cors v1.8.2
github.com/stretchr/testify v1.7.0
go.temporal.io/sdk v1.13.1
go.uber.org/zap v1.21.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,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 c847912

Please sign in to comment.