Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: generate application URL on backend side #9618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 12, 2023
Merged

Conversation

mtojek
Copy link
Member

@mtojek mtojek commented Sep 11, 2023

Related: #8145

This PR delegates application URL generation from the site to the backend. Once it is merged, the generation will be performed in one place.

Note: The last step of the issue will adjust the URL generation routine to prevent the bug of generating a too long subdomain.

@mtojek mtojek self-assigned this Sep 11, 2023
@mtojek mtojek force-pushed the 8145-url-from-backend branch from f80df13 to 41459dd Compare September 12, 2023 08:32
@mtojek mtojek requested a review from johnstcn September 12, 2023 10:59
@mtojek mtojek marked this pull request as ready for review September 12, 2023 10:59
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this isn't technically a refactor, as we are changing the external-facing logic here.

Looks good to me, this particular endpoint does not appear to be called very often (I can only see it referenced by a few CLI commands), so the additional DB queries should not be an issue.

Comment on lines +67 to +100

resource, err := api.Database.GetWorkspaceResourceByID(ctx, workspaceAgent.ResourceID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching workspace resource.",
Detail: err.Error(),
})
return
}
build, err := api.Database.GetWorkspaceBuildByJobID(ctx, resource.JobID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching workspace build.",
Detail: err.Error(),
})
return
}
workspace, err := api.Database.GetWorkspaceByID(ctx, build.WorkspaceID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching workspace.",
Detail: err.Error(),
})
return
}
owner, err := api.Database.GetUserByID(ctx, workspace.OwnerID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching workspace owner.",
Detail: err.Error(),
})
return
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential follow-up: would it make sense to modify GetWorkspaceAppsByAgentID to return the username and workspace name as well? Although this endpoint does not appear to be used by the UI at all, only by the CLI and unit tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that in mind as potential optimization, but as long it is not used, and could be deprecated, I will focus on deprecation instead 👍

Comment on lines -1284 to +1323
func convertApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp {
// convertProvisionedApps converts applications that are in the middle of provisioning process.
// It means that they may not have an agent or workspace assigned (dry-run job).
func convertProvisionedApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp {
return convertApps(dbApps, database.WorkspaceAgent{}, database.User{}, database.Workspace{})
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why have this extra function at all, you could simply just call convertApps() with the empty structs instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, I just personally find it "dirty" to call functions with multiple nil, nil, nil, {}, {}, {}, or "", "", "". 😅

@mtojek mtojek merged commit 898971b into main Sep 12, 2023
@mtojek mtojek deleted the 8145-url-from-backend branch September 12, 2023 13:25
@github-actions github-actions bot locked and limited conversation to collaborators Sep 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants