mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-11 21:44:25 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ea200bb2b | ||
|
|
987244019c |
@@ -3,6 +3,8 @@ package common
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bitechdev/ResolveSpec/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CORSConfig holds CORS configuration
|
// CORSConfig holds CORS configuration
|
||||||
@@ -15,8 +17,27 @@ type CORSConfig struct {
|
|||||||
|
|
||||||
// DefaultCORSConfig returns a default CORS configuration suitable for HeadSpec
|
// DefaultCORSConfig returns a default CORS configuration suitable for HeadSpec
|
||||||
func DefaultCORSConfig() CORSConfig {
|
func DefaultCORSConfig() CORSConfig {
|
||||||
|
configManager := config.GetConfigManager()
|
||||||
|
cfg, _ := configManager.GetConfig()
|
||||||
|
hosts := make([]string, 0)
|
||||||
|
// hosts = append(hosts, "*")
|
||||||
|
|
||||||
|
_, _, ipsList := config.GetIPs()
|
||||||
|
|
||||||
|
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))
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return CORSConfig{
|
return CORSConfig{
|
||||||
AllowedOrigins: []string{"*"},
|
AllowedOrigins: hosts,
|
||||||
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
||||||
AllowedHeaders: GetHeadSpecHeaders(),
|
AllowedHeaders: GetHeadSpecHeaders(),
|
||||||
MaxAge: 86400, // 24 hours
|
MaxAge: 86400, // 24 hours
|
||||||
|
|||||||
@@ -73,6 +73,9 @@ type ServerInstanceConfig struct {
|
|||||||
|
|
||||||
// Tags for organization and filtering
|
// Tags for organization and filtering
|
||||||
Tags map[string]string `mapstructure:"tags"`
|
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
|
// TracingConfig holds OpenTelemetry tracing configuration
|
||||||
|
|||||||
@@ -12,6 +12,16 @@ type Manager struct {
|
|||||||
v *viper.Viper
|
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
|
// NewManager creates a new configuration manager with defaults
|
||||||
func NewManager() *Manager {
|
func NewManager() *Manager {
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
@@ -32,7 +42,8 @@ func NewManager() *Manager {
|
|||||||
// Set default values
|
// Set default values
|
||||||
setDefaults(v)
|
setDefaults(v)
|
||||||
|
|
||||||
return &Manager{v: v}
|
configInstance = &Manager{v: v}
|
||||||
|
return configInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewManagerWithOptions creates a new configuration manager with custom options
|
// NewManagerWithOptions creates a new configuration manager with custom options
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ApplyGlobalDefaults applies global server defaults to this instance
|
// ApplyGlobalDefaults applies global server defaults to this instance
|
||||||
@@ -105,3 +108,42 @@ func (sc *ServersConfig) GetDefault() (*ServerInstanceConfig, error) {
|
|||||||
|
|
||||||
return &instance, nil
|
return &instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetIPs - GetIP for pc
|
||||||
|
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()
|
||||||
|
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