detached d8b24be

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() {
5252
5353 // Set the logger level
5454 logger.SetLevel(logLevel)
55+ log.SetLevel(logLevel) // Set the global logger level too
5556 logger.Debug("Log level set from environment variable", "level", logLevelEnv)
5657 } else if os.Getenv("ENV") == "dev" {
5758 // 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 level53 // 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 set58 // 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
425425 }
426426 }
427427
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 {
430440 return fmt.Errorf("failed to add proxy route: %w", err)
431441 }
432442
@@ -425,8 +425,18 @@ func addProxyRoute(a *server.App, containerID, containerIP, containerPort, targe
425 }425 }
426 }426 }
427 427
428- // Add the route428+ // 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
13561356 if err != nil {
13571357 return fmt.Errorf("failed to insert domain: %w", err)
13581358 }
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+ }
13591394 } else {
13601395 return fmt.Errorf("failed to query domain: %w", err)
13611396 }
@@ -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 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close