Cloud-native API Gateway uses plug-ins to provide customizable extension capabilities. This topic describes how to use WebIDE to develop and compile custom plug-ins.
Background information
Writing custom plug-ins and setting up a development environment can be difficult. To simplify this process, Cloud-native API Gateway provides a WebIDE runtime with a built-in plug-in development environment. This lets you develop and compile custom plug-ins in one place.
WebIDE is integrated with Alibaba Cloud DevOps repositories. Your code is managed by Git, which supports persistence, standardized pipeline environments, and automated builds.
Procedure
1. Write a gateway plug-in
Log on to the API Gateway console.
In the left-side navigation pane, click Plug-in. In the top navigation bar, select a region.
On the Plug-ins page, click Develop Plug-in With WebIDE. In the page that opens, click New.
In the Create Plug-in panel, enter a plug-in ID and name. Click OK to open WebIDE and start writing the gateway plug-in.
2. Build and publish
After you finish writing the gateway plug-in code, you must build and publish it before you can install it on the gateway.
In the upper-right corner of the WebIDE console, click Build and then click Confirm.
NoteIf you attempt to build with uncommitted files, you are prompted to either commit and build or build directly. A direct build does not include the uncommitted content.
Click Build Log in the upper-right corner to view the real-time build status.
When the build completes, click Publish Version in the upper-right corner.
On the Add Version page, set the parameters and click Publish. The parameters are described as follows:
Parameter
Description
Example
Version number
The version number. The format must be X.Y.Z.
1.0.0
Version description
A description of the version.
Modified the response format.
Execution phase
The processing order of phases is: Authentication > Authorization > Statistics > Default. If the plug-in does not depend on the execution order of other plug-ins, select Default.
Default
Execution priority
Controls the priority within the execution phase. A larger number indicates a higher priority.
100
Compatible gateway version
Any version: The plug-in can be installed on any gateway version.
Specified version: The plug-in can only be installed on the specified gateway version or later.
Any version
Basic structure of plug-in code
Gateway plug-in development involves two main parts: configuration parsing and handler functions. Configuration parsing reads the plug-in's configuration, and handler functions process request headers, request bodies, response headers, and response bodies.
func init() {
wrapper.SetCtx(
// The plug-in name. You can customize this.
"my-plugin",
// Set a user-defined function to parse the plug-in configuration. This method is suitable for scenarios where the configuration rules are consistent for global, routing, domain, and service levels.
wrapper.ParseConfigBy(parseConfig),
// Set a user-defined function to process request headers when the gateway receives them from the client.
wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders),
// Set a user-defined function to process response headers when the gateway receives them from the backend service.
wrapper.ProcessResponseHeadersBy(onHttpResponseHeaders),
// Set a user-defined function to process the request body when the gateway receives it from the client.
wrapper.ProcessRequestBodyBy(onHttpRequestBody),
// Set a user-defined function to process the response body when the gateway receives it from the backend service.
wrapper.ProcessResponseBodyBy(onHttpResponseBody),
// Set a user-defined function to process the streaming request body.
wrapper.ProcessStreamingRequestBodyBy(onHttpStreamingRequestBody),
// Set a user-defined function to process the streaming response body.
wrapper.ProcessStreamingResponseBodyBy(onHttpStreamingResponseBody),
// Set a user-defined function to handle the completion of a stream request.
//wrapper.ProcessStreamDoneBy(onHttpStreamDone),
)
}
// MyConfig is the custom plug-in configuration. You can define it as needed. If the plug-in requires no configuration, define an empty struct (type MyConfig struct {}).
type MyConfig struct {
// Specifies whether to enable the mock feature.
mockEnable bool
}
// The YAML configuration entered in the console is automatically converted to JSON. You can parse the configuration directly from the JSON parameter.
func parseConfig(json gjson.Result, config *MyConfig, log logs.Log) error {
// Parse the configuration and update it to the config.
config.mockEnable = json.Get("mockEnable").Bool()
return nil
}
// Request header handler function.
func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log logs.Log) types.Action {
proxywasm.AddHttpRequestHeader("hello", "world")
if config.mockEnable {
proxywasm.SendHttpResponse(200, nil, []byte("hello world"), -1)
}
return types.ActionContinue
}
// Request body handler function.
func onHttpRequestBody(ctx wrapper.HttpContext, config MyConfig, body []byte, log logs.Log) types.Action {
return types.ActionContinue
}
// Response body handler function.
func onHttpResponseBody(ctx wrapper.HttpContext, config MyConfig, body []byte, log logs.Log) types.Action {
return types.ActionContinue
}
// Response header handler function.
func onHttpResponseHeaders(ctx wrapper.HttpContext, config MyConfig, log logs.Log) types.Action {
return types.ActionContinue
}Generate plug-in code using a large model
WebIDE lets you generate plug-in code from a natural language description. The procedure is as follows:
In the upper-right corner of the WebIDE console, click Generate With One Click.
In the editor that appears, describe your requirements in detail.
Click Confirm and wait for the code to generate.
References
For more information about how to develop a plug-in, see Develop plug-ins.
For more information about how to install, enable, and uninstall plug-ins, see Manage plug-ins.