Files
coder/coderd/httpmw/workspaceparam.go
T
Spike Curtis 8dc4d76890 chore: add agent-connection-watch for workspaces (#24507)
<!--

If you have used AI to produce some or all of this PR, please ensure you have read our [AI Contribution guidelines](https://coder.com/docs/about/contributing/AI_CONTRIBUTING) before submitting.

-->

relates to GRU-18  
  
Adds basic implementation for Workspace Agent Connection Watch and tests.  
  
Missing are handling of logs.
2026-05-20 13:09:11 -04:00

61 lines
1.7 KiB
Go

package httpmw
import (
"context"
"net/http"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw/loggermw"
"github.com/coder/coder/v2/codersdk"
)
type workspaceParamContextKey struct{}
// WorkspaceParam returns the workspace from the ExtractWorkspaceParam handler.
func WorkspaceParam(r *http.Request) database.Workspace {
workspace, ok := r.Context().Value(workspaceParamContextKey{}).(database.Workspace)
if !ok {
panic("developer error: workspace param middleware not provided")
}
return workspace
}
// ExtractWorkspaceParam grabs a workspace from the "workspace" URL parameter.
func ExtractWorkspaceParam(db database.Store) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
workspaceID, parsed := ParseUUIDParam(rw, r, "workspace")
if !parsed {
return
}
workspace, err := db.GetWorkspaceByID(ctx, workspaceID)
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching workspace.",
Detail: err.Error(),
})
return
}
ctx = context.WithValue(ctx, workspaceParamContextKey{}, workspace)
if rlogger := loggermw.RequestLoggerFromContext(ctx); rlogger != nil {
rlogger.WithFields(slog.F("workspace_name", workspace.Name))
}
next.ServeHTTP(rw, r.WithContext(ctx))
})
}
}
func WithWorkspaceParam(ctx context.Context, workspace database.Workspace) context.Context {
return context.WithValue(ctx, workspaceParamContextKey{}, workspace)
}