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 )
0 commit comments