Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
217 lines (188 loc) · 6.89 KB
/
Copy pathcontext.go
File metadata and controls
217 lines (188 loc) · 6.89 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package plugin
import "strings"
// Context is passed to [Plugin.Init] and used to register route handlers,
// event subscriptions, and automation-graph node handlers. It also gives
// access to platform services such as the database, key-value store,
// logger, and configuration.
type Context struct {
routes map[routeKey]RouteHandler
events map[string]EventHandler
conditions map[string]ConditionHandler
actions map[string]ActionHandler
db *DB
kv *KV
cache *Cache
log *Logger
cfg *Config
perm *Permissions
}
// routeKey uniquely identifies a registered route by HTTP method + path.
type routeKey struct {
method string
path string
}
// Route registers a handler for the given HTTP method and path.
// Paths are relative to the plugin's base URL:
//
// /api/v1/plugins/{pluginId}/{path}
//
// Project-scoped routes should include /projects/:projectId in the path
// (e.g. "/projects/:projectId/tasks/:taskId/items").
// Path parameters are available via [Request.PathParam].
func (c *Context) Route(method, path string, handler RouteHandler) {
c.routes[routeKey{strings.ToUpper(method), path}] = handler
}
// On registers an event handler for the given topic.
//
// Example topics: "task.created", "task.deleted", "member.added".
func (c *Context) On(topic string, handler EventHandler) {
c.events[topic] = handler
}
// DB returns a helper for typed SQL operations scoped to the plugin schema.
func (c *Context) DB() *DB { return c.db }
// KV returns a helper for simple key-value persistence.
func (c *Context) KV() *KV { return c.kv }
// Cache returns a helper for the plugin's TTL-based Valkey/Redis-backed
// cache — use it for expensive-to-recompute data that can tolerate being
// briefly stale, as opposed to KV's durable, non-expiring storage.
func (c *Context) Cache() *Cache { return c.cache }
// Log returns a structured logger.
func (c *Context) Log() *Logger { return c.log }
// Config returns a read-only helper for plugin configuration values.
func (c *Context) Config() *Config { return c.cfg }
// Permissions returns a helper for checking the current caller's effective
// permissions, including any custom permission the plugin declared.
func (c *Context) Permissions() *Permissions { return c.perm }
// RouteHandler is the function signature for HTTP route handlers.
type RouteHandler func(req *Request, res *Response)
// EventHandler is the function signature for event subscription handlers.
type EventHandler func(evt *Event)
// newContext constructs a Context backed by the provided implementations.
// Called by the WASM runtime (with host-function backends) and by
// [plugintest] (with in-memory backends).
func newContext(db DBBackend, kv KVBackend, cache CacheBackend, log LogBackend, cfg ConfigBackend, perm PermissionBackend) *Context {
return &Context{
routes: make(map[routeKey]RouteHandler),
events: make(map[string]EventHandler),
conditions: make(map[string]ConditionHandler),
actions: make(map[string]ActionHandler),
db: &DB{backend: db},
kv: &KV{backend: kv},
cache: &Cache{backend: cache},
log: &Logger{backend: log},
cfg: &Config{backend: cfg},
perm: &Permissions{backend: perm},
}
}
// matchRoute finds the registered route for method+path. When more than one
// registered pattern matches the same incoming path — e.g. a plugin
// registers both "/views/:viewId/panels/:panelId" and
// "/views/:viewId/panels/layout" under the same method, and the request is
// for ".../panels/layout" — the most specific pattern wins (the one with
// fewer ":param" segments), so a literal segment like "layout" always beats
// a same-position ":panelId" wildcard. Without this, Go's randomized map
// iteration order would make the match nondeterministic across calls.
func (c *Context) matchRoute(method, path string) (RouteHandler, map[string]string, bool) {
method = strings.ToUpper(method)
if handler, ok := c.routes[routeKey{method, path}]; ok {
return handler, make(map[string]string), true
}
projectID, relativePath, hasProjectScope := splitProjectPath(path)
var (
bestHandler RouteHandler
bestParams map[string]string
bestSpecificity = -1
found bool
)
consider := func(pattern string, handler RouteHandler, tryPath string, withProjectID bool) {
params, ok := matchRoutePattern(pattern, tryPath)
if !ok {
return
}
specificity := routeSpecificity(pattern)
if specificity <= bestSpecificity {
return
}
if withProjectID {
params["projectId"] = projectID
}
bestHandler, bestParams, bestSpecificity, found = handler, params, specificity, true
}
for key, handler := range c.routes {
if key.method != method {
continue
}
consider(key.path, handler, path, false)
if hasProjectScope {
consider(key.path, handler, relativePath, true)
}
}
return bestHandler, bestParams, found
}
// routeSpecificity counts a pattern's literal (non-":param") segments —
// used to break ties when multiple registered patterns match the same
// incoming path. Any leading "/projects/:projectId" scope prefix is
// stripped first, so a fully-qualified pattern like
// "/projects/:projectId/tasks/:taskId" and its relative-style equivalent
// "/tasks/:taskId" (matched via splitProjectPath's implicit projectId
// injection) score the same — spelling the prefix out explicitly is a
// registration-style choice, not extra genuine specificity.
func routeSpecificity(pattern string) int {
segments := splitPathSegments(pattern)
if len(segments) >= 2 && segments[0] == "projects" && strings.HasPrefix(segments[1], ":") {
segments = segments[2:]
}
n := 0
for _, seg := range segments {
if !strings.HasPrefix(seg, ":") {
n++
}
}
return n
}
func splitProjectPath(path string) (projectID, relativePath string, ok bool) {
trimmed := strings.Trim(path, "/")
if trimmed == "" {
return "", "", false
}
segments := strings.Split(trimmed, "/")
if len(segments) < 2 || segments[0] != "projects" {
return "", "", false
}
projectID = segments[1]
if len(segments) == 2 {
return projectID, "/", true
}
return projectID, "/" + strings.Join(segments[2:], "/"), true
}
func matchRoutePattern(pattern, path string) (map[string]string, bool) {
patternSegments := splitPathSegments(pattern)
pathSegments := splitPathSegments(path)
if len(patternSegments) != len(pathSegments) {
return nil, false
}
params := make(map[string]string)
for i := range patternSegments {
patternSegment := patternSegments[i]
pathSegment := pathSegments[i]
if strings.HasPrefix(patternSegment, ":") {
name := strings.TrimPrefix(patternSegment, ":")
if name == "" {
return nil, false
}
params[name] = pathSegment
continue
}
if patternSegment != pathSegment {
return nil, false
}
}
return params, true
}
func splitPathSegments(path string) []string {
trimmed := strings.Trim(path, "/")
if trimmed == "" {
return nil
}
return strings.Split(trimmed, "/")
}