buildAppLinkURL returns the URL to open the app in the browser. It follows similar logic to the TypeScript implementation in site/src/utils/app.ts except that all URLs returned are absolute and based on the provided base URL.
(baseURL *url.URL, workspace codersdk.Workspace, agent codersdk.WorkspaceAgent, app codersdk.WorkspaceApp, appsHost, preferredPathBase string)
| 650 | // It follows similar logic to the TypeScript implementation in site/src/utils/app.ts |
| 651 | // except that all URLs returned are absolute and based on the provided base URL. |
| 652 | func buildAppLinkURL(baseURL *url.URL, workspace codersdk.Workspace, agent codersdk.WorkspaceAgent, app codersdk.WorkspaceApp, appsHost, preferredPathBase string) string { |
| 653 | // If app is external, return the URL directly |
| 654 | if app.External { |
| 655 | return app.URL |
| 656 | } |
| 657 | |
| 658 | var u url.URL |
| 659 | u.Scheme = baseURL.Scheme |
| 660 | u.Host = baseURL.Host |
| 661 | // We redirect if we don't include a trailing slash, so we always include one to avoid extra roundtrips. |
| 662 | u.Path = fmt.Sprintf( |
| 663 | "%s/@%s/%s.%s/apps/%s/", |
| 664 | preferredPathBase, |
| 665 | workspace.OwnerName, |
| 666 | workspace.Name, |
| 667 | agent.Name, |
| 668 | url.PathEscape(app.Slug), |
| 669 | ) |
| 670 | if app.Command != "" { |
| 671 | u.Path = fmt.Sprintf( |
| 672 | "%s/@%s/%s.%s/terminal", |
| 673 | preferredPathBase, |
| 674 | workspace.OwnerName, |
| 675 | workspace.Name, |
| 676 | agent.Name, |
| 677 | ) |
| 678 | q := u.Query() |
| 679 | q.Set("app", app.Slug) |
| 680 | u.RawQuery = q.Encode() |
| 681 | } |
| 682 | |
| 683 | if appsHost != "" && app.Subdomain && app.SubdomainName != "" { |
| 684 | u.Host = strings.Replace(appsHost, "*", app.SubdomainName, 1) |
| 685 | u.Path = "/" |
| 686 | } |
| 687 | return u.String() |
| 688 | } |