feat: Improve domain handling and Let's Encrypt support in proxy routing
- Add domain cleaning to remove protocol prefixes when adding proxy routes - Enhance domain database insertion to support subdomain and domain part tracking - Add debug logging for domain and subdomain database operations - Improve log level configuration by setting global logger level - Implement more robust domain parsing for Let's Encrypt certificate management
- commit
- d8b24be50ccb2906f3ee2442d2430527f55e6458
- author
- bamen · · 2025-03-09 16:12 UTC
- parent
- b5e5ea3
- signature
- unsigned
3 files · +48 -2
M internal/common/init_config.go +1 -0
| @@ -52,6 +52,7 @@ func init() { | |||
| 52 | 52 | ||
| 53 | 53 | // Set the logger level | |
| 54 | 54 | logger.SetLevel(logLevel) | |
| 55 | + | log.SetLevel(logLevel) // Set the global logger level too | |
| 55 | 56 | logger.Debug("Log level set from environment variable", "level", logLevelEnv) | |
| 56 | 57 | } else if os.Getenv("ENV") == "dev" { | |
| 57 | 58 | // Fall back to ENV=dev behavior if GORDON_LOG_LEVEL is not set | |
| @@ -52,6 +52,7 @@ func init() { | |||||
| 52 | 52 | ||||
| 53 | // Set the logger level | 53 | // Set the logger level | ||
| 54 | logger.SetLevel(logLevel) | 54 | logger.SetLevel(logLevel) | ||
| 55 | + | log.SetLevel(logLevel) // Set the global logger level too | |||
| 55 | logger.Debug("Log level set from environment variable", "level", logLevelEnv) | 56 | logger.Debug("Log level set from environment variable", "level", logLevelEnv) | ||
| 56 | } else if os.Getenv("ENV") == "dev" { | 57 | } else if os.Getenv("ENV") == "dev" { | ||
| 57 | // Fall back to ENV=dev behavior if GORDON_LOG_LEVEL is not set | 58 | // Fall back to ENV=dev behavior if GORDON_LOG_LEVEL is not set | ||
M internal/httpserve/handlers/endpoint_deploy.go +12 -2
| @@ -425,8 +425,18 @@ func addProxyRoute(a *server.App, containerID, containerIP, containerPort, targe | |||
| 425 | 425 | } | |
| 426 | 426 | } | |
| 427 | 427 | ||
| 428 | - | // Add the route | |
| 429 | - | if err := p.AddRoute(targetDomain, containerID, containerIP, containerPort, protocol, "/"); err != nil { | |
| 428 | + | // Clean domain if it includes protocol | |
| 429 | + | cleanDomain := targetDomain | |
| 430 | + | if strings.Contains(cleanDomain, "://") { | |
| 431 | + | cleanDomain = strings.Split(cleanDomain, "://")[1] | |
| 432 | + | } | |
| 433 | + | ||
| 434 | + | // Log that we're adding the domain for Let's Encrypt | |
| 435 | + | log.Debug("Adding domain for Let's Encrypt verification", | |
| 436 | + | "domain", cleanDomain) | |
| 437 | + | ||
| 438 | + | // Add the route - this will also add to the domain database for Let's Encrypt | |
| 439 | + | if err := p.AddRoute(cleanDomain, containerID, containerIP, containerPort, protocol, "/"); err != nil { | |
| 430 | 440 | return fmt.Errorf("failed to add proxy route: %w", err) | |
| 431 | 441 | } | |
| 432 | 442 | ||
| @@ -425,8 +425,18 @@ func addProxyRoute(a *server.App, containerID, containerIP, containerPort, targe | |||||
| 425 | } | 425 | } | ||
| 426 | } | 426 | } | ||
| 427 | 427 | ||||
| 428 | - | // Add the route | 428 | + | // Clean domain if it includes protocol |
| 429 | - | if err := p.AddRoute(targetDomain, containerID, containerIP, containerPort, protocol, "/"); err != nil { | 429 | + | cleanDomain := targetDomain |
| 430 | + | if strings.Contains(cleanDomain, "://") { | |||
| 431 | + | cleanDomain = strings.Split(cleanDomain, "://")[1] | |||
| 432 | + | } | |||
| 433 | + | ||||
| 434 | + | // Log that we're adding the domain for Let's Encrypt | |||
| 435 | + | log.Debug("Adding domain for Let's Encrypt verification", | |||
| 436 | + | "domain", cleanDomain) | |||
| 437 | + | ||||
| 438 | + | // Add the route - this will also add to the domain database for Let's Encrypt | |||
| 439 | + | if err := p.AddRoute(cleanDomain, containerID, containerIP, containerPort, protocol, "/"); err != nil { | |||
| 430 | return fmt.Errorf("failed to add proxy route: %w", err) | 440 | return fmt.Errorf("failed to add proxy route: %w", err) | ||
| 431 | } | 441 | } | ||
| 432 | 442 | ||||
M internal/proxy/proxy.go +35 -0
| @@ -1356,6 +1356,41 @@ func (p *Proxy) AddRoute(domainName, containerID, containerIP, containerPort, pr | |||
| 1356 | 1356 | if err != nil { | |
| 1357 | 1357 | return fmt.Errorf("failed to insert domain: %w", err) | |
| 1358 | 1358 | } | |
| 1359 | + | ||
| 1360 | + | log.Debug("Added domain to database for Let's Encrypt", | |
| 1361 | + | "domain", hostname) | |
| 1362 | + | ||
| 1363 | + | // Also add the subdomain.domain format to the database if this is a domain with components | |
| 1364 | + | hostParts := strings.Split(hostname, ".") | |
| 1365 | + | if len(hostParts) >= 3 { | |
| 1366 | + | // This appears to be in format subdomain.domain.tld | |
| 1367 | + | // Extract subdomain and domain parts | |
| 1368 | + | subdomain := hostParts[0] | |
| 1369 | + | domainPart := strings.Join(hostParts[1:], ".") | |
| 1370 | + | ||
| 1371 | + | // Check if the domain part already exists | |
| 1372 | + | var domainPartID string | |
| 1373 | + | err = tx.QueryRow("SELECT id FROM domain WHERE name = ?", domainPart).Scan(&domainPartID) | |
| 1374 | + | if err != nil { | |
| 1375 | + | if err == sql.ErrNoRows { | |
| 1376 | + | // Domain part doesn't exist, create it | |
| 1377 | + | domainPartID = generateUUID() | |
| 1378 | + | _, err = tx.Exec( | |
| 1379 | + | "INSERT INTO domain (id, name, created_at, updated_at) VALUES (?, ?, ?, ?)", | |
| 1380 | + | domainPartID, domainPart, now, now, | |
| 1381 | + | ) | |
| 1382 | + | if err != nil { | |
| 1383 | + | log.Warn("Failed to insert domain part, continuing anyway", | |
| 1384 | + | "domain_part", domainPart, | |
| 1385 | + | "error", err) | |
| 1386 | + | } else { | |
| 1387 | + | log.Debug("Added domain part to database for Let's Encrypt", | |
| 1388 | + | "domain_part", domainPart, | |
| 1389 | + | "subdomain", subdomain) | |
| 1390 | + | } | |
| 1391 | + | } | |
| 1392 | + | } | |
| 1393 | + | } | |
| 1359 | 1394 | } else { | |
| 1360 | 1395 | return fmt.Errorf("failed to query domain: %w", err) | |
| 1361 | 1396 | } | |
| @@ -1356,6 +1356,41 @@ func (p *Proxy) AddRoute(domainName, containerID, containerIP, containerPort, pr | |||||
| 1356 | if err != nil { | 1356 | if err != nil { | ||
| 1357 | return fmt.Errorf("failed to insert domain: %w", err) | 1357 | return fmt.Errorf("failed to insert domain: %w", err) | ||
| 1358 | } | 1358 | } | ||
| 1359 | + | ||||
| 1360 | + | log.Debug("Added domain to database for Let's Encrypt", | |||
| 1361 | + | "domain", hostname) | |||
| 1362 | + | ||||
| 1363 | + | // Also add the subdomain.domain format to the database if this is a domain with components | |||
| 1364 | + | hostParts := strings.Split(hostname, ".") | |||
| 1365 | + | if len(hostParts) >= 3 { | |||
| 1366 | + | // This appears to be in format subdomain.domain.tld | |||
| 1367 | + | // Extract subdomain and domain parts | |||
| 1368 | + | subdomain := hostParts[0] | |||
| 1369 | + | domainPart := strings.Join(hostParts[1:], ".") | |||
| 1370 | + | ||||
| 1371 | + | // Check if the domain part already exists | |||
| 1372 | + | var domainPartID string | |||
| 1373 | + | err = tx.QueryRow("SELECT id FROM domain WHERE name = ?", domainPart).Scan(&domainPartID) | |||
| 1374 | + | if err != nil { | |||
| 1375 | + | if err == sql.ErrNoRows { | |||
| 1376 | + | // Domain part doesn't exist, create it | |||
| 1377 | + | domainPartID = generateUUID() | |||
| 1378 | + | _, err = tx.Exec( | |||
| 1379 | + | "INSERT INTO domain (id, name, created_at, updated_at) VALUES (?, ?, ?, ?)", | |||
| 1380 | + | domainPartID, domainPart, now, now, | |||
| 1381 | + | ) | |||
| 1382 | + | if err != nil { | |||
| 1383 | + | log.Warn("Failed to insert domain part, continuing anyway", | |||
| 1384 | + | "domain_part", domainPart, | |||
| 1385 | + | "error", err) | |||
| 1386 | + | } else { | |||
| 1387 | + | log.Debug("Added domain part to database for Let's Encrypt", | |||
| 1388 | + | "domain_part", domainPart, | |||
| 1389 | + | "subdomain", subdomain) | |||
| 1390 | + | } | |||
| 1391 | + | } | |||
| 1392 | + | } | |||
| 1393 | + | } | |||
| 1359 | } else { | 1394 | } else { | ||
| 1360 | return fmt.Errorf("failed to query domain: %w", err) | 1395 | return fmt.Errorf("failed to query domain: %w", err) | ||
| 1361 | } | 1396 | } | ||