mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-08 20:54:24 +00:00
feat(cors): ✨ enhance CORS configuration with dynamic origins
* Update CORSConfig to allow dynamic origins based on server instances. * Add ExternalURLs field to ServerInstanceConfig for additional CORS support. * Implement GetIPs function to retrieve non-local IP addresses for CORS.
This commit is contained in:
@@ -3,6 +3,8 @@ package common
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/bitechdev/ResolveSpec/pkg/config"
|
||||
)
|
||||
|
||||
// CORSConfig holds CORS configuration
|
||||
@@ -15,8 +17,28 @@ type CORSConfig struct {
|
||||
|
||||
// DefaultCORSConfig returns a default CORS configuration suitable for HeadSpec
|
||||
func DefaultCORSConfig() CORSConfig {
|
||||
configManager := config.GetConfigManager()
|
||||
cfg, _ := configManager.GetConfig()
|
||||
hosts := make([]string, 0)
|
||||
//hosts = append(hosts, "*")
|
||||
|
||||
_, _, ipsList := config.GetIPs()
|
||||
|
||||
for _, server := range cfg.Servers.Instances {
|
||||
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)
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
return CORSConfig{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedOrigins: hosts,
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: GetHeadSpecHeaders(),
|
||||
MaxAge: 86400, // 24 hours
|
||||
|
||||
@@ -73,6 +73,9 @@ type ServerInstanceConfig struct {
|
||||
|
||||
// Tags for organization and filtering
|
||||
Tags map[string]string `mapstructure:"tags"`
|
||||
|
||||
// ExternalURLs are additional URLs that this server instance is accessible from (for CORS) for proxy setups
|
||||
ExternalURLs []string `mapstructure:"external_urls"`
|
||||
}
|
||||
|
||||
// TracingConfig holds OpenTelemetry tracing configuration
|
||||
|
||||
@@ -12,6 +12,16 @@ type Manager struct {
|
||||
v *viper.Viper
|
||||
}
|
||||
|
||||
var configInstance *Manager
|
||||
|
||||
// GetConfigManager returns a singleton configuration manager instance
|
||||
func GetConfigManager() *Manager {
|
||||
if configInstance == nil {
|
||||
configInstance = NewManager()
|
||||
}
|
||||
return configInstance
|
||||
}
|
||||
|
||||
// NewManager creates a new configuration manager with defaults
|
||||
func NewManager() *Manager {
|
||||
v := viper.New()
|
||||
@@ -32,7 +42,8 @@ func NewManager() *Manager {
|
||||
// Set default values
|
||||
setDefaults(v)
|
||||
|
||||
return &Manager{v: v}
|
||||
configInstance = &Manager{v: v}
|
||||
return configInstance
|
||||
}
|
||||
|
||||
// NewManagerWithOptions creates a new configuration manager with custom options
|
||||
|
||||
@@ -2,6 +2,9 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ApplyGlobalDefaults applies global server defaults to this instance
|
||||
@@ -105,3 +108,42 @@ func (sc *ServersConfig) GetDefault() (*ServerInstanceConfig, error) {
|
||||
|
||||
return &instance, nil
|
||||
}
|
||||
|
||||
// GetIPs - GetIP for pc
|
||||
func GetIPs() (string, string, []net.IP) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
fmt.Println("Recovered in GetIPs", err)
|
||||
}
|
||||
}()
|
||||
hostname, _ := os.Hostname()
|
||||
ipaddrlist := make([]net.IP, 0)
|
||||
iplist := ""
|
||||
addrs, err := net.LookupIP(hostname)
|
||||
if err != nil {
|
||||
return hostname, iplist, ipaddrlist
|
||||
}
|
||||
|
||||
for _, a := range addrs {
|
||||
//cfg.LogInfo("\nFound IP Host Address: %s", a)
|
||||
if strings.Contains(a.String(), "127.0.0.1") {
|
||||
continue
|
||||
}
|
||||
iplist = fmt.Sprintf("%s,%s", iplist, a)
|
||||
ipaddrlist = append(ipaddrlist, a)
|
||||
}
|
||||
if iplist == "" {
|
||||
iff, _ := net.InterfaceAddrs()
|
||||
for _, a := range iff {
|
||||
//cfg.LogInfo("\nFound IP Address: %s", a)
|
||||
if strings.Contains(a.String(), "127.0.0.1") {
|
||||
continue
|
||||
}
|
||||
iplist = fmt.Sprintf("%s,%s", iplist, a)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
iplist = strings.TrimLeft(iplist, ",")
|
||||
return hostname, iplist, ipaddrlist
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user