Skip to content

Commit ff1da07

Browse files
committed
[API] feat: add CORS middleware
1 parent 8db52f6 commit ff1da07

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

API/cmd/server/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"net/http"
99
"os"
10+
"strings"
1011
"time"
1112

1213
"github.com/ArmchairDevelopers/Kyber/API/api/v1/pbapi"
@@ -30,6 +31,7 @@ import (
3031
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
3132
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
3233
grpc_sentry "github.com/johnbellone/grpc-middleware-sentry"
34+
"github.com/rs/cors"
3335
"go.uber.org/zap"
3436
"golang.org/x/sync/errgroup"
3537
"google.golang.org/grpc"
@@ -130,7 +132,23 @@ func main() {
130132

131133
downloadManager := api.NewDownloadManager(minioClient)
132134
imageManager := api.NewImageManager(store)
133-
httpHandler := sentryHandler.Handle(httpRouter)
135+
136+
allowedOrigins := strings.Split(os.Getenv("CORS_ORIGINS"), ",")
137+
138+
for i, o := range allowedOrigins {
139+
allowedOrigins[i] = strings.TrimSpace(o)
140+
}
141+
142+
corsMW := cors.New(cors.Options{
143+
AllowedOrigins: allowedOrigins,
144+
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodOptions},
145+
AllowedHeaders: []string{"Authorization", "Content-Type", "X-Grpc-Web", "Grpc-Timeout", "X-User-Agent"},
146+
ExposedHeaders: []string{"Grpc-Status", "Grpc-Message", "Grpc-Status-Details-Bin"},
147+
AllowCredentials: true,
148+
MaxAge: 600,
149+
})
150+
151+
httpHandler := corsMW.Handler(sentryHandler.Handle(httpRouter))
134152

135153
httpRouter.HandleFunc("/docker/auth", dockerAuth.AuthHandler).Methods(http.MethodGet)
136154
httpRouter.HandleFunc("/discord/auth", discordAuth.AuthHandler).Methods(http.MethodGet)

API/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ require (
5757
github.com/minio/md5-simd v1.1.2 // indirect
5858
github.com/montanaflynn/stats v0.7.1 // indirect
5959
github.com/philhofer/fwd v1.2.0 // indirect
60+
github.com/rs/cors v1.11.1 // indirect
6061
github.com/rs/xid v1.6.0 // indirect
6162
github.com/tinylib/msgp v1.5.0 // indirect
6263
github.com/xdg-go/pbkdf2 v1.0.0 // indirect

API/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ github.com/redis/go-redis/v9 v9.17.1 h1:7tl732FjYPRT9H9aNfyTwKg9iTETjWjGKEJ2t/5i
130130
github.com/redis/go-redis/v9 v9.17.1/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
131131
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
132132
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
133+
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
134+
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
133135
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
134136
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
135137
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=

0 commit comments

Comments
 (0)