feat: get_match_config returns with rule_id for stateful plugins (#406)

This commit is contained in:
纪卓志
2023-07-04 19:59:43 +08:00
committed by GitHub
parent b65446fa25
commit d718870b65
2 changed files with 16 additions and 13 deletions

View File

@@ -13,7 +13,7 @@
// limitations under the License.
use higress_wasm_rust::log::Log;
use higress_wasm_rust::rule_matcher::{on_configure, RuleMatcher};
use higress_wasm_rust::rule_matcher::{on_configure, RuleMatcher, SharedRuleMatcher};
use proxy_wasm::traits::{Context, HttpContext, RootContext};
use proxy_wasm::types::{Action, ContextType, LogLevel};
use serde::Deserialize;
@@ -28,11 +28,11 @@ proxy_wasm::main! {{
struct SayHelloRoot {
log: Log,
rule_matcher: Rc<RefCell<RuleMatcher<SayHelloConfig>>>,
rule_matcher: SharedRuleMatcher<SayHelloConfig>,
}
struct SayHello {
rule_matcher: Rc<RefCell<RuleMatcher<SayHelloConfig>>>,
rule_matcher: SharedRuleMatcher<SayHelloConfig>,
}
#[derive(Default, Debug, Deserialize)]
@@ -82,7 +82,7 @@ impl HttpContext for SayHello {
self.send_http_response(200, vec![], Some("Hello, World!".as_bytes()));
return Action::Continue;
}
Some(config) => config,
Some(config) => config.1,
};
self.send_http_response(

View File

@@ -20,7 +20,9 @@ use proxy_wasm::traits::RootContext;
use proxy_wasm::types::LogLevel;
use serde::de::DeserializeOwned;
use serde_json::{from_slice, Map, Value};
use std::cell::RefCell;
use std::collections::HashSet;
use std::rc::Rc;
enum Category {
Route,
@@ -37,6 +39,8 @@ const RULES_KEY: &str = "_rules_";
const MATCH_ROUTE_KEY: &str = "_match_route_";
const MATCH_DOMAIN_KEY: &str = "_match_domain_";
pub type SharedRuleMatcher<PluginConfig> = Rc<RefCell<RuleMatcher<PluginConfig>>>;
struct HostMatcher {
match_type: MatchType,
host: String,
@@ -137,15 +141,15 @@ where
Ok(())
}
pub fn get_match_config(&self) -> Option<&PluginConfig> {
pub fn get_match_config(&self) -> Option<(i64, &PluginConfig)> {
let host = get_http_request_header(":authority").unwrap_or_default();
let route_name = get_property(vec!["route_name"]).unwrap_or_default();
for rule in &self.rule_config {
for (i, rule) in self.rule_config.iter().enumerate() {
match rule.category {
Category::Host => {
if self.host_match(rule, host.as_str()) {
return Some(&rule.config);
return Some((i as i64, &rule.config));
}
}
Category::Route => {
@@ -154,20 +158,19 @@ where
.unwrap_or_else(|_| "".to_string())
.as_str(),
) {
return Some(&rule.config);
return Some((i as i64, &rule.config));
}
}
}
}
return self.global_config.as_ref();
self.global_config
.as_ref()
.map(|config| (usize::MAX as i64, config))
}
pub fn rewrite_config(&mut self, rewrite: fn(config: &PluginConfig) -> PluginConfig) {
self.global_config = match self.global_config.as_ref() {
None => None,
Some(config) => Some(rewrite(config))
};
self.global_config = self.global_config.as_ref().map(rewrite);
for rule_config in &mut self.rule_config {
rule_config.config = rewrite(&rule_config.config);