MCPcopy Index your code
hub / github.com/coder/coder / ExtractWorkspaceAgentAndWorkspaceParam

Function ExtractWorkspaceAgentAndWorkspaceParam

coderd/httpmw/workspaceagentparam.go:28–58  ·  view source on GitHub ↗

ExtractWorkspaceAgentAndWorkspaceParam grabs a workspace agent and its associated workspace from the "workspaceagent" URL parameter.

(db database.Store)

Source from the content-addressed store, hash-verified

26
27// ExtractWorkspaceAgentAndWorkspaceParam grabs a workspace agent and its associated workspace from the "workspaceagent" URL parameter.
28func ExtractWorkspaceAgentAndWorkspaceParam(db database.Store) func(http.Handler) http.Handler {
29 return func(next http.Handler) http.Handler {
30 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
31 ctx := r.Context()
32 agentUUID, parsed := ParseUUIDParam(rw, r, "workspaceagent")
33 if !parsed {
34 return
35 }
36
37 agentWithWorkspace, err := db.GetWorkspaceAgentAndWorkspaceByID(ctx, agentUUID)
38 if httpapi.Is404Error(err) {
39 httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
40 Message: "Agent doesn't exist with that id, or you do not have access to it.",
41 })
42 return
43 }
44
45 ctx = context.WithValue(ctx, workspaceAgentAndWorkspaceParamContextKey{}, agentWithWorkspace)
46 chi.RouteContext(ctx).URLParams.Add("workspace", agentWithWorkspace.WorkspaceTable.ID.String())
47
48 if rlogger := loggermw.RequestLoggerFromContext(ctx); rlogger != nil {
49 rlogger.WithFields(
50 slog.F("workspace_name", agentWithWorkspace.WorkspaceTable.Name),
51 slog.F("agent_name", agentWithWorkspace.WorkspaceAgent.Name),
52 )
53 }
54
55 next.ServeHTTP(rw, r.WithContext(ctx))
56 })
57 }
58}

Callers 2

TestWatchAgentContainersFunction · 0.92
TestWorkspaceAgentParamFunction · 0.92

Calls 11

Is404ErrorFunction · 0.92
WriteFunction · 0.92
RequestLoggerFromContextFunction · 0.92
ParseUUIDParamFunction · 0.85
WithContextMethod · 0.80
ContextMethod · 0.65
AddMethod · 0.65
WithFieldsMethod · 0.65
StringMethod · 0.45
ServeHTTPMethod · 0.45

Tested by 2

TestWatchAgentContainersFunction · 0.74
TestWorkspaceAgentParamFunction · 0.74