History

feat(pluginhost): introduce browser-navigable plugin resources in Management API

- Added `resources` field in `management.register` for defining browser-accessible resources. - Updated examples and documentation to reflect resource-based paths under `/v0/resource/plugins/<pluginID>/...`. - Replaced legacy `GET` menu routes with resource-based implementations for consistent plugin behavior. - Enhanced request handling for resource paths, including proper response headers and streamlined test coverage.

Luis Pater committed 5 months agov7.1.59

Commit 44ea9ab1 parent 2aeb41c22 files · +341 −69Browse files

Showing the changes to examples/plugin/simple (5 of 22 files). Show all

Unified · Split

5 files
  1. examples/plugin/simple/README.md +3 −1
  2. examples/plugin/simple/README_CN.md +3 −1
  3. examples/plugin/simple/c/src/plugin.c +3 −3
  4. examples/plugin/simple/go/main.go +11 −6
  5. examples/plugin/simple/rust/src/lib.rs +2 −2
examples/plugin/simple/README.md+3−1
@@ -187,7 +187,9 @@ PUT /v0/management/plugins/{pluginID}/config · 186 unmodified lines
187PATCH /v0/management/plugins/{pluginID}/config187PATCH /v0/management/plugins/{pluginID}/config
188```188```
189 189
190Plugin-owned Management API routes are registered through `management.register` and handled through `management.handle`.190Plugin-owned Management API routes are registered through the `routes` field of `management.register` and handled through `management.handle`.
191
192Browser-navigable menu resources are registered through the `resources` field of `management.register`. CPA exposes those resources under `/v0/resource/plugins/<pluginID>/...`; for example, a plugin with ID `example` and resource path `/status` is served as `/v0/resource/plugins/example/status`.
191 193
192## Trust Boundary194## Trust Boundary
193 195
examples/plugin/simple/README_CN.md+3−1
@@ -185,7 +185,9 @@ PUT /v0/management/plugins/{pluginID}/config · 184 unmodified lines
185PATCH /v0/management/plugins/{pluginID}/config185PATCH /v0/management/plugins/{pluginID}/config
186```186```
187 187
188插件自有 Management API 路由通过 `management.register` 注册,通过 `management.handle` 处理。188插件自有 Management API 路由通过 `management.register` 的 `routes` 字段注册,并通过 `management.handle` 处理。
189
190可由浏览器直接访问的菜单资源通过 `management.register` 的 `resources` 字段注册。CPA 会将这些资源暴露在 `/v0/resource/plugins/<pluginID>/...` 下;例如插件 ID 为 `example` 且资源路径为 `/status` 时,最终路径是 `/v0/resource/plugins/example/status`。
189 191
190## 信任边界192## 信任边界
191 193
examples/plugin/simple/c/src/plugin.c+3−3
@@ -84,8 +84,8 @@ static const char* CLI_REGISTER_RESPONSE = · 83 unmodified lines
84static const char* CLI_EXECUTE_RESPONSE =84static const char* CLI_EXECUTE_RESPONSE =
85 "{\"ok\":true,\"result\":{\"Stdout\":\"cGx1Z2luIGV4YW1wbGUgYyBjb21tYW5kCg==\",\"ExitCode\":0}}";85 "{\"ok\":true,\"result\":{\"Stdout\":\"cGx1Z2luIGV4YW1wbGUgYyBjb21tYW5kCg==\",\"ExitCode\":0}}";
86static const char* MANAGEMENT_REGISTER_RESPONSE =86static const char* MANAGEMENT_REGISTER_RESPONSE =
87 "{\"ok\":true,\"result\":{\"Routes\":[{\"Method\":\"GET\",\"Path\":\"/plugins/example-c/status\","87 "{\"ok\":true,\"result\":{\"Resources\":[{\"Path\":\"/status\","
88 "\"Menu\":\"Example C Plugin\",\"Description\":\"Shows example C plugin status.\"}]}}";88 "\"Menu\":\"Example C Plugin\",\"Description\":\"CPA exposes this menu resource under /v0/resource/plugins/example-c/status.\"}]}}";
89static const char* UNKNOWN_METHOD_RESPONSE =89static const char* UNKNOWN_METHOD_RESPONSE =
90 "{\"ok\":false,\"error\":{\"code\":\"unknown_method\",\"message\":\"unknown method\"}}";90 "{\"ok\":false,\"error\":{\"code\":\"unknown_method\",\"message\":\"unknown method\"}}";
91static const char* INVALID_METHOD_RESPONSE =91static const char* INVALID_METHOD_RESPONSE =
@@ -421,7 +421,7 @@ static char* make_http_response(const uint8_t* request, size_t request_len) { · 329 unmodified lines
421 char* url = extract_json_string(json, "URL");421 char* url = extract_json_string(json, "URL");
422 char* path = extract_json_string(json, "Path");422 char* path = extract_json_string(json, "Path");
423 char* method_escaped = json_escape(method == NULL ? "GET" : method);423 char* method_escaped = json_escape(method == NULL ? "GET" : method);
424 char* target_escaped = json_escape(url != NULL ? url : (path == NULL ? "/plugins/example-c/status" : path));424 char* target_escaped = json_escape(url != NULL ? url : (path == NULL ? "/v0/resource/plugins/example-c/status" : path));
425 char* body_json = format_string(425 char* body_json = format_string(
426 "{\"plugin\":\"example-c\",\"method\":\"%s\",\"target\":\"%s\"}",426 "{\"plugin\":\"example-c\",\"method\":\"%s\",\"target\":\"%s\"}",
427 method_escaped == NULL ? "" : method_escaped,427 method_escaped == NULL ? "" : method_escaped,
examples/plugin/simple/go/main.go+11−6
@@ -100,7 +100,8 @@ type streamResponse struct { · 99 unmodified lines
100}100}
101 101
102type managementRegistrationResponse struct {102type managementRegistrationResponse struct {
103 Routes []pluginapi.ManagementRoute `json:"routes,omitempty"`103 Routes []pluginapi.ManagementRoute `json:"routes,omitempty"`
104 Resources []pluginapi.ResourceRoute `json:"resources,omitempty"`
104}105}
105 106
106func main() {}107func main() {}
@@ -207,14 +208,18 @@ func handleMethod(method string, request []byte) ([]byte, error) { · 100 unmodified lines
207 case pluginabi.MethodCommandLineExecute:208 case pluginabi.MethodCommandLineExecute:
208 return okEnvelope(pluginapi.CommandLineExecutionResponse{Stdout: []byte("plugin example command\n")})209 return okEnvelope(pluginapi.CommandLineExecutionResponse{Stdout: []byte("plugin example command\n")})
209 case pluginabi.MethodManagementRegister:210 case pluginabi.MethodManagementRegister:
210 return okEnvelope(managementRegistrationResponse{Routes: []pluginapi.ManagementRoute{{211 // CPA exposes menu resources under /v0/resource/plugins/<plugin-id>/.
211 Method: http.MethodGet,212 return okEnvelope(managementRegistrationResponse{Resources: []pluginapi.ResourceRoute{{
212 Path: "/plugins/example/status",213 Path: "/status",
213 Menu: "Example Plugin",214 Menu: "Example Plugin",
214 Description: "Shows example plugin status.",215 Description: "Shows example plugin status as a browser-navigable resource.",
215 }}})216 }}})
216 case pluginabi.MethodManagementHandle:217 case pluginabi.MethodManagementHandle:
217 return okEnvelope(pluginapi.ManagementResponse{StatusCode: http.StatusOK, Body: []byte(`{"plugin":"example"}`)})218 return okEnvelope(pluginapi.ManagementResponse{
219 StatusCode: http.StatusOK,
220 Headers: http.Header{"Content-Type": []string{"text/html; charset=utf-8"}},
221 Body: []byte(`<!doctype html><title>Example Plugin</title><main>Example Plugin</main>`),
222 })
218 default:223 default:
219 return errorEnvelope("unknown_method", "unknown method: "+method), nil224 return errorEnvelope("unknown_method", "unknown method: "+method), nil
220 }225 }
examples/plugin/simple/rust/src/lib.rs+2−2
@@ -18,7 +18,7 @@ const FRONTEND_AUTH_RESPONSE: &str = r#"{"ok":true,"result":{"Authenticated":tru · 17 unmodified lines
18const STREAM_RESPONSE: &str = r#"{"ok":true,"result":{"headers":{"content-type":["text/event-stream"]},"chunks":[{"Payload":"cGx1Z2luLWV4YW1wbGUtcnVzdAo="}]}}"#;18const STREAM_RESPONSE: &str = r#"{"ok":true,"result":{"headers":{"content-type":["text/event-stream"]},"chunks":[{"Payload":"cGx1Z2luLWV4YW1wbGUtcnVzdAo="}]}}"#;
19const CLI_REGISTER_RESPONSE: &str = r#"{"ok":true,"result":{"Flags":[{"Name":"plugin-example-rust-command","Usage":"Run the example Rust ABI plugin command","Type":"bool"}]}}"#;19const CLI_REGISTER_RESPONSE: &str = r#"{"ok":true,"result":{"Flags":[{"Name":"plugin-example-rust-command","Usage":"Run the example Rust ABI plugin command","Type":"bool"}]}}"#;
20const CLI_EXECUTE_RESPONSE: &str = r#"{"ok":true,"result":{"Stdout":"cGx1Z2luIGV4YW1wbGUgcnVzdCBjb21tYW5kCg==","ExitCode":0}}"#;20const CLI_EXECUTE_RESPONSE: &str = r#"{"ok":true,"result":{"Stdout":"cGx1Z2luIGV4YW1wbGUgcnVzdCBjb21tYW5kCg==","ExitCode":0}}"#;
21const MANAGEMENT_REGISTER_RESPONSE: &str = r#"{"ok":true,"result":{"Routes":[{"Method":"GET","Path":"/plugins/example-rust/status","Menu":"Example Rust Plugin","Description":"Shows example Rust plugin status."}]}}"#;21const MANAGEMENT_REGISTER_RESPONSE: &str = r#"{"ok":true,"result":{"Resources":[{"Path":"/status","Menu":"Example Rust Plugin","Description":"CPA exposes this menu resource under /v0/resource/plugins/example-rust/status."}]}}"#;
22const UNKNOWN_METHOD_RESPONSE: &str = r#"{"ok":false,"error":{"code":"unknown_method","message":"unknown method"}}"#;22const UNKNOWN_METHOD_RESPONSE: &str = r#"{"ok":false,"error":{"code":"unknown_method","message":"unknown method"}}"#;
23const INVALID_METHOD_RESPONSE: &str = r#"{"ok":false,"error":{"code":"invalid_method","message":"method is required"}}"#;23const INVALID_METHOD_RESPONSE: &str = r#"{"ok":false,"error":{"code":"invalid_method","message":"method is required"}}"#;
24 24
@@ -170,7 +170,7 @@ fn make_http_response(request: &[u8]) -> String { · 145 unmodified lines
170 let method = extract_json_string(&json, "Method").unwrap_or_else(|| "GET".to_string());170 let method = extract_json_string(&json, "Method").unwrap_or_else(|| "GET".to_string());
171 let target = extract_json_string(&json, "URL")171 let target = extract_json_string(&json, "URL")
172 .or_else(|| extract_json_string(&json, "Path"))172 .or_else(|| extract_json_string(&json, "Path"))
173 .unwrap_or_else(|| "/plugins/example-rust/status".to_string());173 .unwrap_or_else(|| "/v0/resource/plugins/example-rust/status".to_string());
174 let body = format!(174 let body = format!(
175 r#"{{"plugin":"example-rust","method":"{}","target":"{}"}}"#,175 r#"{{"plugin":"example-rust","method":"{}","target":"{}"}}"#,
176 json_escape(&method),176 json_escape(&method),