Skip to content

Commit

Permalink
feat: output the VIRTUAL_HOST if specified in environment variables o…
Browse files Browse the repository at this point in the history
…f a service (ddev#6136) [skip ci]

Co-authored-by: Stanislav Zhuk <[email protected]>
  • Loading branch information
kevinquillen and stasadev authored May 22, 2024
1 parent 9fd5323 commit c41e903
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 28 deletions.
13 changes: 9 additions & 4 deletions cmd/ddev/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ running 'ddev describe <projectname>'.`,
// renderAppDescribe takes the map describing the app and renders it for plain-text output
func renderAppDescribe(app *ddevapp.DdevApp, desc map[string]interface{}) (string, error) {
status := desc["status"]

var out bytes.Buffer

t := table.NewWriter()
Expand Down Expand Up @@ -123,9 +124,9 @@ func renderAppDescribe(app *ddevapp.DdevApp, desc map[string]interface{}) (strin

for _, k := range serviceNames {
v := serviceMap[k]

httpURL := ""
urlPortParts := []string{}
extraInfo := []string{}

switch {
// Normal case, using ddev-router based URLs
Expand All @@ -135,6 +136,7 @@ func renderAppDescribe(app *ddevapp.DdevApp, desc map[string]interface{}) (strin
} else if httpURL, ok = v["http_url"]; ok {
urlPortParts = append(urlPortParts, httpURL)
}

// Gitpod, web container only, using port proxied by Gitpod
case (nodeps.IsGitpod() || nodeps.IsCodespaces()) && k == "web":
urlPortParts = append(urlPortParts, app.GetPrimaryURL())
Expand All @@ -148,14 +150,17 @@ func renderAppDescribe(app *ddevapp.DdevApp, desc map[string]interface{}) (strin
}

if p, ok := v["exposed_ports"]; ok {
urlPortParts = append(urlPortParts, "InDocker: "+v["short_name"]+":"+p)
if p != "" {
urlPortParts = append(urlPortParts, "InDocker: "+v["short_name"]+":"+p)
} else {
urlPortParts = append(urlPortParts, "InDocker: "+v["short_name"])
}
}

if p, ok := v["host_ports"]; ok && p != "" {
urlPortParts = append(urlPortParts, "Host: 127.0.0.1:"+p)
}

extraInfo := []string{}

// Get extra info for web container
if k == "web" {
extraInfo = append(extraInfo, fmt.Sprintf("%s PHP%s\n%s\ndocroot:'%s'", desc["type"], desc["php_version"], desc["webserver_type"], desc["docroot"]))
Expand Down
84 changes: 60 additions & 24 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,34 +312,70 @@ func (app *DdevApp) Describe(short bool) (map[string]interface{}, error) {
}
services[shortName]["host_ports"] = strings.Join(hostPorts, ",")

// Extract HTTP_EXPOSE and HTTPS_EXPOSE for additional info
// Extract VIRTUAL_HOST, HTTP_EXPOSE and HTTPS_EXPOSE for additional info
if !IsRouterDisabled(app) {
envMap := make(map[string]string)

for _, e := range c.Config.Env {
split := strings.SplitN(e, "=", 2)
envName := split[0]
if len(split) == 2 && (envName == "HTTP_EXPOSE" || envName == "HTTPS_EXPOSE") {
envVal := split[1]

envValStr := fmt.Sprintf("%s", envVal)
portSpecs := strings.Split(envValStr, ",")
// There might be more than one exposed UI port, but this only handles the first listed,
// most often there's only one.
if len(portSpecs) > 0 {
// HTTPS portSpecs typically look like <exposed>:<containerPort>, for example - HTTPS_EXPOSE=1359:1358
ports := strings.Split(portSpecs[0], ":")
//services[shortName][envName.(string)] = ports[0]
switch envName {
case "HTTP_EXPOSE":
services[shortName]["http_url"] = "http://" + appDesc["hostname"].(string)
if ports[0] != "80" {
services[shortName]["http_url"] = services[shortName]["http_url"] + ":" + ports[0]
}
case "HTTPS_EXPOSE":
services[shortName]["https_url"] = "https://" + appDesc["hostname"].(string)
if ports[0] != "443" {
services[shortName]["https_url"] = services[shortName]["https_url"] + ":" + ports[0]
}
}

// Store the values first, so we can have them all before assigning
if len(split) == 2 && (envName == "VIRTUAL_HOST" || envName == "HTTP_EXPOSE" || envName == "HTTPS_EXPOSE") {
envMap[envName] = split[1]
}
}

if virtualHost, ok := envMap["VIRTUAL_HOST"]; ok {
vhostVal := virtualHost
vhostValStr := fmt.Sprintf("%s", vhostVal)
vhostsList := strings.Split(vhostValStr, ",")

// There might be more than one VIRTUAL_HOST value, but this only handles the first listed,
// most often there's only one.
if len(vhostsList) > 0 {
// VIRTUAL_HOSTS typically look like subdomain.domain.tld, for example - VIRTUAL_HOSTS=vhost1.myproject.ddev.site,vhost2.myproject.ddev.site
vhost := strings.Split(vhostsList[0], ",")
services[shortName]["virtual_host"] = vhost[0]
}
}

hostname, ok := services[shortName]["virtual_host"]
appHostname := ""

if ok {
appHostname = hostname
} else {
appHostname = appDesc["hostname"].(string)
}

for name, portMapping := range envMap {
if name != "HTTP_EXPOSE" && name != "HTTPS_EXPOSE" {
continue
}

portDefault := "80"
attributeName := "http_url"
protocol := "http://"

if name == "HTTPS_EXPOSE" {
portDefault = "443"
attributeName = "https_url"
protocol = "https://"
}

portValStr := fmt.Sprintf("%s", portMapping)
portSpecs := strings.Split(portValStr, ",")
// There might be more than one exposed UI port, but this only handles the first listed,
// most often there's only one.
if len(portSpecs) > 0 {
// HTTP(S) portSpecs typically look like <exposed>:<containerPort>, for example - HTTP_EXPOSE=1359:1358
ports := strings.Split(portSpecs[0], ":")

services[shortName][attributeName] = protocol + appHostname

if ports[0] != portDefault {
services[shortName][attributeName] = services[shortName][attributeName] + ":" + ports[0]
}
}
}
Expand Down

0 comments on commit c41e903

Please sign in to comment.