From 1fabba474b3a1362b85da0e088dcd3967f39151b Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Thu, 21 Sep 2023 09:42:16 -0700 Subject: [PATCH] refactor default allow origins this should be less error prone --- server/routes.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/server/routes.go b/server/routes.go index 79d2ee72..c463a1af 100644 --- a/server/routes.go +++ b/server/routes.go @@ -499,23 +499,25 @@ func CopyModelHandler(c *gin.Context) { } } -func Serve(ln net.Listener, origins []string) error { +var defaultAllowOrigins = []string{ + "localhost", + "127.0.0.1", + "0.0.0.0", +} + +func Serve(ln net.Listener, allowOrigins []string) error { config := cors.DefaultConfig() config.AllowWildcard = true - config.AllowOrigins = append(origins, []string{ - "http://localhost", - "http://localhost:*", - "https://localhost", - "https://localhost:*", - "http://127.0.0.1", - "http://127.0.0.1:*", - "https://127.0.0.1", - "https://127.0.0.1:*", - "http://0.0.0.0", - "http://0.0.0.0:*", - "https://0.0.0.0", - "https://0.0.0.0:*", - }...) + + config.AllowOrigins = allowOrigins + for _, allowOrigin := range defaultAllowOrigins { + config.AllowOrigins = append(config.AllowOrigins, + fmt.Sprintf("http://%s", allowOrigin), + fmt.Sprintf("https://%s", allowOrigin), + fmt.Sprintf("http://%s:*", allowOrigin), + fmt.Sprintf("https://%s:*", allowOrigin), + ) + } r := gin.Default() r.Use(cors.New(config))