From 6ea200bb2b89d05a773ecfab4e3a00e872fc2efd Mon Sep 17 00:00:00 2001 From: Hein Date: Tue, 6 Jan 2026 14:07:56 +0200 Subject: [PATCH] =?UTF-8?q?refactor(cors):=20=F0=9F=9B=A0=EF=B8=8F=20impro?= =?UTF-8?q?ve=20host=20handling=20in=20CORS=20config=20*=20Change=20loop?= =?UTF-8?q?=20to=20use=20index=20for=20server=20instances=20*=20Simplify?= =?UTF-8?q?=20appending=20external=20URLs=20*=20Clean=20up=20commented=20c?= =?UTF-8?q?ode=20for=20clarity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/common/cors.go | 9 ++++----- pkg/config/server.go | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/common/cors.go b/pkg/common/cors.go index 529a806..0325af6 100644 --- a/pkg/common/cors.go +++ b/pkg/common/cors.go @@ -20,17 +20,16 @@ func DefaultCORSConfig() CORSConfig { configManager := config.GetConfigManager() cfg, _ := configManager.GetConfig() hosts := make([]string, 0) - //hosts = append(hosts, "*") + // hosts = append(hosts, "*") _, _, ipsList := config.GetIPs() - for _, server := range cfg.Servers.Instances { + for i := range cfg.Servers.Instances { + server := cfg.Servers.Instances[i] hosts = append(hosts, fmt.Sprintf("http://%s:%d", server.Host, server.Port)) hosts = append(hosts, fmt.Sprintf("https://%s:%d", server.Host, server.Port)) hosts = append(hosts, fmt.Sprintf("http://%s:%d", "localhost", server.Port)) - for _, extURL := range server.ExternalURLs { - hosts = append(hosts, extURL) - } + hosts = append(hosts, server.ExternalURLs...) for _, ip := range ipsList { hosts = append(hosts, fmt.Sprintf("http://%s:%d", ip.String(), server.Port)) hosts = append(hosts, fmt.Sprintf("https://%s:%d", ip.String(), server.Port)) diff --git a/pkg/config/server.go b/pkg/config/server.go index a532f5e..201f053 100644 --- a/pkg/config/server.go +++ b/pkg/config/server.go @@ -110,13 +110,13 @@ func (sc *ServersConfig) GetDefault() (*ServerInstanceConfig, error) { } // GetIPs - GetIP for pc -func GetIPs() (string, string, []net.IP) { +func GetIPs() (hostname string, ipList string, ipNetList []net.IP) { defer func() { if err := recover(); err != nil { fmt.Println("Recovered in GetIPs", err) } }() - hostname, _ := os.Hostname() + hostname, _ = os.Hostname() ipaddrlist := make([]net.IP, 0) iplist := "" addrs, err := net.LookupIP(hostname) @@ -125,7 +125,7 @@ func GetIPs() (string, string, []net.IP) { } for _, a := range addrs { - //cfg.LogInfo("\nFound IP Host Address: %s", a) + // cfg.LogInfo("\nFound IP Host Address: %s", a) if strings.Contains(a.String(), "127.0.0.1") { continue } @@ -135,7 +135,7 @@ func GetIPs() (string, string, []net.IP) { if iplist == "" { iff, _ := net.InterfaceAddrs() for _, a := range iff { - //cfg.LogInfo("\nFound IP Address: %s", a) + // cfg.LogInfo("\nFound IP Address: %s", a) if strings.Contains(a.String(), "127.0.0.1") { continue }