Skip to content

Commit 6ea200b

Browse files
author
Hein
committed
refactor(cors): 🛠️ improve host handling in CORS config
* Change loop to use index for server instances * Simplify appending external URLs * Clean up commented code for clarity
1 parent 9872440 commit 6ea200b

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

pkg/common/cors.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ func DefaultCORSConfig() CORSConfig {
2020
configManager := config.GetConfigManager()
2121
cfg, _ := configManager.GetConfig()
2222
hosts := make([]string, 0)
23-
//hosts = append(hosts, "*")
23+
// hosts = append(hosts, "*")
2424

2525
_, _, ipsList := config.GetIPs()
2626

27-
for _, server := range cfg.Servers.Instances {
27+
for i := range cfg.Servers.Instances {
28+
server := cfg.Servers.Instances[i]
2829
hosts = append(hosts, fmt.Sprintf("http://%s:%d", server.Host, server.Port))
2930
hosts = append(hosts, fmt.Sprintf("https://%s:%d", server.Host, server.Port))
3031
hosts = append(hosts, fmt.Sprintf("http://%s:%d", "localhost", server.Port))
31-
for _, extURL := range server.ExternalURLs {
32-
hosts = append(hosts, extURL)
33-
}
32+
hosts = append(hosts, server.ExternalURLs...)
3433
for _, ip := range ipsList {
3534
hosts = append(hosts, fmt.Sprintf("http://%s:%d", ip.String(), server.Port))
3635
hosts = append(hosts, fmt.Sprintf("https://%s:%d", ip.String(), server.Port))

pkg/config/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ func (sc *ServersConfig) GetDefault() (*ServerInstanceConfig, error) {
110110
}
111111

112112
// GetIPs - GetIP for pc
113-
func GetIPs() (string, string, []net.IP) {
113+
func GetIPs() (hostname string, ipList string, ipNetList []net.IP) {
114114
defer func() {
115115
if err := recover(); err != nil {
116116
fmt.Println("Recovered in GetIPs", err)
117117
}
118118
}()
119-
hostname, _ := os.Hostname()
119+
hostname, _ = os.Hostname()
120120
ipaddrlist := make([]net.IP, 0)
121121
iplist := ""
122122
addrs, err := net.LookupIP(hostname)
@@ -125,7 +125,7 @@ func GetIPs() (string, string, []net.IP) {
125125
}
126126

127127
for _, a := range addrs {
128-
//cfg.LogInfo("\nFound IP Host Address: %s", a)
128+
// cfg.LogInfo("\nFound IP Host Address: %s", a)
129129
if strings.Contains(a.String(), "127.0.0.1") {
130130
continue
131131
}
@@ -135,7 +135,7 @@ func GetIPs() (string, string, []net.IP) {
135135
if iplist == "" {
136136
iff, _ := net.InterfaceAddrs()
137137
for _, a := range iff {
138-
//cfg.LogInfo("\nFound IP Address: %s", a)
138+
// cfg.LogInfo("\nFound IP Address: %s", a)
139139
if strings.Contains(a.String(), "127.0.0.1") {
140140
continue
141141
}

0 commit comments

Comments
 (0)