ExtractWorkspaceAgentAndWorkspaceParam grabs a workspace agent and its associated workspace from the "workspaceagent" URL parameter.
(db database.Store)
| 26 | |
| 27 | // ExtractWorkspaceAgentAndWorkspaceParam grabs a workspace agent and its associated workspace from the "workspaceagent" URL parameter. |
| 28 | func 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 | } |