shenyu_client_rust/lib.rs
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Rust shenyu-client-rust sdk of Apache ShenYu.
use crate::model::UriInfo;
pub mod config;
pub mod core;
pub mod error;
pub mod macros;
pub mod model;
pub trait IRouter {
fn app_name(&self) -> &str;
fn uri_infos(&self) -> &Vec<UriInfo>;
}
#[cfg(feature = "axum")]
pub mod axum_impl {
use super::model::UriInfo;
use crate::IRouter;
use axum::extract::Request;
use axum::response::IntoResponse;
use axum::routing::MethodRouter;
use axum::Router;
use std::convert::Infallible;
use tower_service::Service;
/// A router that can be used to register routes.
///
/// This is a wrapper around `Router` that provides a more ergonomic API.
/// It allows you to define routes and nest other routers or services.
///
/// # Fields
///
/// * `app_name` - The name of the application.
/// * `uri_infos` - A vector of URI information.
///
/// # Examples
/// ```rust
///
/// use axum::routing::{get, post};
/// use shenyu_client_rust::axum_impl::ShenYuRouter;
///
/// async fn health_handler() -> &'static str {
/// "OK"
/// }
///
/// async fn create_user_handler() -> &'static str {
/// "User created"
/// }
///
/// async fn not_found_handler() -> &'static str {
/// "Not found"
/// }
///
/// let app = ShenYuRouter::<()>::new("shenyu_client_app")
/// .nest("/api", ShenYuRouter::new("api"))
/// .route("/health", "get", get(health_handler))
/// .route("/users", "post", post(create_user_handler));
///
/// ```
///
#[derive(Debug, Clone)]
pub struct ShenYuRouter<S = ()> {
app_name: String,
inner: Router<S>,
uri_infos: Vec<UriInfo>,
}
impl<S> ShenYuRouter<S>
where
S: Clone + Send + Sync + 'static,
{
pub fn new(app_name: &str) -> Self {
Self {
app_name: app_name.to_string(),
inner: Router::new(),
uri_infos: Vec::new(),
}
}
pub fn uri_info(mut self, uri_info: UriInfo) -> Self {
self.uri_infos.push(uri_info);
self
}
pub fn route(mut self, path: &str, method: &str, method_router: MethodRouter<S>) -> Self {
self.inner = self.inner.route(path, method_router);
self.uri_infos.push(UriInfo {
path: path.to_string(),
rule_name: path.to_string(),
service_name: None,
method_name: method.to_string(),
});
self
}
pub fn route_service<T>(mut self, path: &str, method: &str, service: T) -> Self
where
T: Service<Request, Error = Infallible> + Clone + Send + 'static,
T::Response: IntoResponse,
T::Future: Send + 'static,
{
self.inner = self.inner.route_service(path, service);
self.uri_infos.push(UriInfo {
path: path.to_string(),
rule_name: path.to_string(),
service_name: None,
method_name: method.to_string(),
});
self
}
#[track_caller]
pub fn nest(mut self, path: &str, route: ShenYuRouter<S>) -> Self {
self.inner = self.inner.nest(path, route.inner);
self.uri_infos.extend(route.uri_infos);
self
}
#[track_caller]
pub fn nest_service<T>(mut self, path: &str, method: &str, service: T) -> Self
where
T: Service<Request, Error = Infallible> + Clone + Send + 'static,
T::Response: IntoResponse,
T::Future: Send + 'static,
{
self.inner = self.inner.nest_service(path, service);
self.uri_infos.push(UriInfo {
path: path.to_string(),
rule_name: path.to_string(),
service_name: None,
method_name: method.to_string(),
});
self
}
pub fn uri_infos(&self) -> &Vec<UriInfo> {
&self.uri_infos
}
#[track_caller]
pub fn merge<R>(mut self, other: ShenYuRouter<R>) -> Self
where
R: Into<Router<S>>,
S: Clone + Send + Sync + 'static,
Router<S>: From<Router<R>>,
{
self.inner = self.inner.merge(other.inner);
self.uri_infos.extend(other.uri_infos);
self
}
}
impl<S> From<ShenYuRouter<S>> for Router<S>
where
S: Clone + Send + Sync + 'static,
{
fn from(val: ShenYuRouter<S>) -> Self {
val.inner
}
}
impl<S> IRouter for ShenYuRouter<S> {
fn app_name(&self) -> &str {
&self.app_name
}
fn uri_infos(&self) -> &Vec<UriInfo> {
&self.uri_infos
}
}
}
impl ShenyuClient {
pub fn parse(path: &str, router: Box<dyn IRouter>, port: u16) -> Result<Self, String> {
let config = ShenYuConfig::from_yaml_file(path).unwrap();
Self::from(config, router.app_name(), router.uri_infos(), port)
}
pub fn from(
config: ShenYuConfig,
app_name: &str,
uri_infos: &[UriInfo],
port: u16,
) -> Result<Self, String> {
Self::new(config, app_name, uri_infos, port)
}
}
#[cfg(feature = "actix-web")]
pub mod actix_web_impl {
use super::model::UriInfo;
use crate::IRouter;
/// A router that can be used to register routes.
///
/// This is a wrapper around `Router` that provides a more ergonomic API.
/// It allows you to define routes and nest other routers or services.
///
/// # Fields
///
/// * `app_name` - The name of the application.
/// * `uri_infos` - A vector of URI information.
#[derive(Debug, Clone)]
pub struct ShenYuRouter {
app_name: String,
uri_infos: Vec<UriInfo>,
}
impl ShenYuRouter {
pub fn new(app_name: &str) -> Self {
Self {
app_name: app_name.to_string(),
uri_infos: Vec::new(),
}
}
pub fn route(&mut self, path: &str, method: &str) {
self.uri_infos.push(UriInfo {
path: path.to_string().clone(),
rule_name: path.to_string().clone(),
service_name: None,
method_name: method.to_string(),
});
}
}
impl IRouter for ShenYuRouter {
fn app_name(&self) -> &str {
&self.app_name
}
fn uri_infos(&self) -> &Vec<UriInfo> {
&self.uri_infos
}
}
/// Macro to register the ShenYu client once.
///
/// This macro ensures that the ShenYu client is registered only once using a `OnceLock`.
/// It initializes the client with the provided configuration, router, and port, and sets up
/// a shutdown hook to deregister the client upon receiving a `ctrl_c` signal.
///
/// # Arguments
///
/// * `$config` - The configuration for the ShenYu client.
/// * `$router` - The router instance.
/// * `$port` - The port number.
#[macro_export]
macro_rules! register_once {
($config:expr, $router:expr, $port:literal) => {
use std::sync::OnceLock;
use $crate::IRouter;
static ONCE: OnceLock<()> = OnceLock::new();
ONCE.get_or_init(|| {
let client = {
let res = $crate::core::ShenyuClient::from(
$config,
$router.app_name(),
$router.uri_infos(),
$port,
);
let client = res.unwrap();
client
};
client.register().expect("Failed to register");
actix_web::rt::spawn(async move {
// Add shutdown hook
tokio::select! {
_ = actix_web::rt::signal::ctrl_c() => {
client.offline_register();
}
}
});
});
};
}
/// Macro to define routes for the ShenYu router.
///
/// This macro allows you to define routes for the ShenYu router in a concise manner.
/// It supports both regular routes and nested routes.
///
/// # Arguments
///
/// * `$router` - The router instance.
/// * `$app` - The Actix web application instance.
/// * `$path` - The path for the route.
/// * `$method` - The HTTP method for the route (e.g., `get`, `post`).
/// * `$handler` - The handler function for the route.
///
#[macro_export]
macro_rules! shenyu_router {
($router:expr, $app:expr, $($path:expr => $method:ident($handler:expr))*) => {
$(
$router.route($path, stringify!($method));
$app = $app.service(actix_web::web::resource($path).route(actix_web::web::$method().to($handler)));
)*
}
}
}
use crate::config::ShenYuConfig;
use crate::core::ShenyuClient;
#[cfg(test)]
#[cfg(feature = "axum")]
mod tests_axum {
use super::axum_impl::ShenYuRouter;
use crate::config::ShenYuConfig;
use crate::core::ShenyuClient;
use crate::IRouter;
use axum::routing::{get, post};
use serde_json::Value;
use std::collections::HashMap;
async fn health_handler() -> &'static str {
"OK"
}
async fn create_user_handler() -> &'static str {
"User created"
}
#[tokio::test]
async fn test_login() {
let mut hashmap = HashMap::new();
hashmap.insert("username", "admin");
hashmap.insert("password", "123456");
let params = [
("userName", hashmap.get("username").copied().unwrap()),
("password", hashmap.get("password").copied().unwrap()),
];
// Fix the URL to include the scheme
let res = ureq::get("http://127.0.0.1:9095/platform/login")
.query_pairs(params)
.call()
.unwrap();
let res_data: Value = res.into_json().unwrap();
print!("res_data: {:?}", res_data);
print!("res_data:token {:?}", res_data["data"]["token"]);
}
#[tokio::test]
async fn build_client() {
let app = ShenYuRouter::<()>::new("shenyu_client_app")
.nest("/api", ShenYuRouter::new("api"))
.route("/health", "get", get(health_handler))
.route("/users", "post", post(create_user_handler));
let config = ShenYuConfig::from_yaml_file("config.yml").unwrap();
let res = ShenyuClient::from(config, app.app_name(), app.uri_infos(), 9527);
assert!(&res.is_ok());
let client = &mut res.unwrap();
println!(
"shenyu-client-rust.token: {:?}",
client
.headers
.get("X-Access-Token")
.map(|r| r.clone())
.unwrap_or("None".to_string())
);
if let Ok(token) = client.get_register_token() {
client
.headers
.insert("X-Access-Token".to_string(), token.to_string());
} else {
panic!("Can't get register token");
}
let res = client.register_all_metadata(true);
assert!(res.is_ok());
let res = client.register_uri();
assert!(res.is_ok());
let res = client.register_discovery_config();
assert!(res.is_ok());
client.offline_register();
}
#[test]
fn it_works() {
let binding = ShenYuRouter::<()>::new("shenyu_client_app");
let app = binding
.nest("/api", ShenYuRouter::new("api"))
.route("/health", "get", get(health_handler))
.route("/users", "post", post(create_user_handler));
let uri_infos = app.uri_infos();
assert_eq!(uri_infos.len(), 2);
assert_eq!(uri_infos[0].path, "/health");
assert_eq!(uri_infos[1].path, "/users");
}
}
#[cfg(test)]
#[cfg(feature = "actix-web")]
mod tests_actix_web {
use super::actix_web_impl::ShenYuRouter;
use crate::config::ShenYuConfig;
use crate::core::ShenyuClient;
use crate::IRouter;
#[tokio::test]
async fn build_client() {
let app = ShenYuRouter::new("shenyu_client_app");
let config = ShenYuConfig::from_yaml_file("config.yml").unwrap();
let res = ShenyuClient::from(config, app.app_name(), app.uri_infos(), 9527);
assert!(&res.is_ok());
let client = &mut res.unwrap();
println!(
"shenyu-client-rust.token: {:?}",
client
.headers
.get("X-Access-Token")
.map(|r| r.clone())
.unwrap_or("None".to_string())
);
if let Ok(token) = client.get_register_token() {
client
.headers
.insert("X-Access-Token".to_string(), token.to_string());
} else {
panic!("Can't get register token");
}
let res = client.register_all_metadata(true);
assert!(res.is_ok());
let res = client.register_uri();
assert!(res.is_ok());
let res = client.register_discovery_config();
assert!(res.is_ok());
client.offline_register();
}
}