Repository navigation
Expand file tree
/
Copy pathchatprojectmemoryparam.go
More file actions
52 lines (46 loc) · 1.68 KB
/
Copy pathchatprojectmemoryparam.go
File metadata and controls
52 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package httpmw
import (
"context"
"net/http"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/codersdk"
)
type chatProjectMemoryParamContextKey struct{}
// ChatProjectMemoryParam returns the chat project memory from the extractor.
func ChatProjectMemoryParam(r *http.Request) database.GetChatProjectMemoryByIDRow {
memory, ok := r.Context().Value(chatProjectMemoryParamContextKey{}).(database.GetChatProjectMemoryByIDRow)
if !ok {
panic("developer error: chat project memory param middleware not provided")
}
return memory
}
// ExtractChatProjectMemoryParam resolves a chat project memory from "memory".
func ExtractChatProjectMemoryParam(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()
memoryID, parsed := ParseUUIDParam(rw, r, "memory")
if !parsed {
return
}
// Route extraction resolves identity before the handler authorizes its action.
//nolint:gocritic // Restrict system access to the identity lookup.
memory, err := db.GetChatProjectMemoryByID(dbauthz.AsSystemRestricted(ctx), memoryID)
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching chat project memory.",
Detail: err.Error(),
})
return
}
ctx = context.WithValue(ctx, chatProjectMemoryParamContextKey{}, memory)
next.ServeHTTP(rw, r.WithContext(ctx))
})
}
}