Skip to content

Commit

Permalink
Merge pull request #11 from graphql-editor/allowed-origins
Browse files Browse the repository at this point in the history
Added fetching cors options from envs
  • Loading branch information
Dennor authored Dec 21, 2023
2 parents 07eb9af + 7046594 commit b43e29c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
19 changes: 8 additions & 11 deletions cmd/local/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package localcmd

import (
"flag"
"fmt"
"net/http"

crs "github.com/graphql-editor/stucco/pkg/cors"
"github.com/graphql-editor/stucco/pkg/handlers"
"github.com/graphql-editor/stucco/pkg/server"
"github.com/graphql-editor/stucco/pkg/utils"
Expand Down Expand Up @@ -63,21 +65,16 @@ func NewStartCommand() *cobra.Command {
if err != nil {
return err
}
corsOptions := crs.NewCors()
fmt.Println(corsOptions.AllowedOrigins)
middleware := func(next http.Handler) http.Handler {
return handlers.RecoveryHandler(
httplog.WithLogging(
cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{
http.MethodHead,
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
AllowedOrigins: corsOptions.AllowedOrigins,
AllowedMethods: corsOptions.AllowedMethods,
AllowedHeaders: corsOptions.AllowedHeaders,
AllowCredentials: corsOptions.AllowedCredentials,
}).Handler(next),
httplog.DefaultStacktracePred,
),
Expand Down
53 changes: 53 additions & 0 deletions pkg/cors/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cors

import (
"net/http"
"os"
"strconv"
"strings"
)

type CorsOptions struct {
AllowedMethods, AllowedHeaders, AllowedOrigins []string
AllowedCredentials bool
}

func retriveOriginEnv(name string) []string {
return strings.Split(os.Getenv(name), " ")
}

func NewCors() CorsOptions {
allowedOrigins := []string{"*"}
if envOrigin := retriveOriginEnv("ALLOWED_ORIGINS"); envOrigin[0] != "" {
allowedOrigins = envOrigin
}
allowedMethods := []string{http.MethodHead,
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
}
if envMethod := retriveOriginEnv("ALLOWED_METHODS"); envMethod[0] == "" {
allowedMethods = []string{"POST", "GET", "OPTIONS"}
}
allowedHeaders := []string{"*"}
if envHeaders := retriveOriginEnv("ALLOWED_HEADERS"); envHeaders[0] == "" {
allowedHeaders = []string{"Accept", "Authorization", "Origin", "Content-Type"}
}
allowedCredentials := true
var err error
if envCredentials := os.Getenv("ALLOWED_CREDENTIALS"); envCredentials != "" {
allowedCredentials, err = strconv.ParseBool(envCredentials)
if err != nil {
panic("cannot parse ALLOWED_CREDENTIALS env to boolean")
}
}
c := CorsOptions{
AllowedMethods: allowedMethods,
AllowedHeaders: allowedHeaders,
AllowedOrigins: allowedOrigins,
AllowedCredentials: allowedCredentials,
}
return c
}

0 comments on commit b43e29c

Please sign in to comment.