validops-east-1 commited on
Commit
ab3d262
·
1 Parent(s): 698f782
whatsapp-service/pkg/config/config.go CHANGED
@@ -3,6 +3,7 @@ package config
3
  import (
4
  "database/sql"
5
  "fmt"
 
6
  "net/url"
7
  "os"
8
  "strconv"
@@ -79,7 +80,9 @@ func (c *Config) CreateSupabaseDB() (*sql.DB, error) {
79
  return nil, fmt.Errorf("SUPABASE_DB_URL is required for the whatsmeow session store")
80
  }
81
 
82
- db, err := sql.Open("postgres", c.SupabaseDBURL)
 
 
83
  if err != nil {
84
  return nil, err
85
  }
@@ -97,6 +100,46 @@ func (c *Config) CreateSupabaseDB() (*sql.DB, error) {
97
  return db, nil
98
  }
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  func Load() *Config {
101
  supabaseURL := os.Getenv(config_env.SUPABASE_URL)
102
  supabaseServiceKey := os.Getenv(config_env.SUPABASE_SERVICE_KEY)
 
3
  import (
4
  "database/sql"
5
  "fmt"
6
+ "net"
7
  "net/url"
8
  "os"
9
  "strconv"
 
80
  return nil, fmt.Errorf("SUPABASE_DB_URL is required for the whatsmeow session store")
81
  }
82
 
83
+ dsn := forceIPv4DSN(c.SupabaseDBURL)
84
+
85
+ db, err := sql.Open("postgres", dsn)
86
  if err != nil {
87
  return nil, err
88
  }
 
100
  return db, nil
101
  }
102
 
103
+ // forceIPv4DSN rewrites the DSN host to its resolved IPv4 address. Container
104
+ // runtimes (HF Spaces, many CI/cloud networks) frequently have no IPv6 route;
105
+ // a hostname whose DNS returns only an AAAA record first then fails to connect
106
+ // ("network is unreachable"). lib/pq dials whatever family the URL names, so
107
+ // pin the address to IPv4 to keep the connection working on IPv6-less hosts.
108
+ func forceIPv4DSN(dsn string) string {
109
+ if dsn == "" {
110
+ return dsn
111
+ }
112
+ u, err := url.Parse(dsn)
113
+ if err != nil {
114
+ return dsn
115
+ }
116
+ host := u.Hostname()
117
+ if host == "" || net.ParseIP(host) != nil {
118
+ return dsn
119
+ }
120
+ port := u.Port()
121
+ ips, err := net.LookupIP(host)
122
+ if err != nil {
123
+ return dsn
124
+ }
125
+ var v4 string
126
+ for _, ip := range ips {
127
+ if ip4 := ip.To4(); ip4 != nil {
128
+ v4 = ip4.String()
129
+ break
130
+ }
131
+ }
132
+ if v4 == "" {
133
+ return dsn
134
+ }
135
+ if port != "" {
136
+ u.Host = net.JoinHostPort(v4, port)
137
+ } else {
138
+ u.Host = v4
139
+ }
140
+ return u.String()
141
+ }
142
+
143
  func Load() *Config {
144
  supabaseURL := os.Getenv(config_env.SUPABASE_URL)
145
  supabaseServiceKey := os.Getenv(config_env.SUPABASE_SERVICE_KEY)