diff --git a/go_to_rust/easy_note/Cargo.toml b/go_to_rust/easy_note/Cargo.toml new file mode 100644 index 00000000..a94bb2cd --- /dev/null +++ b/go_to_rust/easy_note/Cargo.toml @@ -0,0 +1,29 @@ + +[package] +name = "test_rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +# we recommend to use the latest framework version for new features and bug fixes +volo = "*" +volo-http = { version = "*", features = ["default_server"] } + +tokio = { version = "1", features = ["full"] } +serde = "*" +tracing = "*" +motore = "*" +serde_json = "*" + +[profile.release] +opt-level = 3 +debug = true +debug-assertions = false +overflow-checks = false +lto = true +panic = 'unwind' +incremental = false +codegen-units = 1 +rpath = false diff --git a/go_to_rust/easy_note/README.md b/go_to_rust/easy_note/README.md new file mode 100644 index 00000000..e69de29b diff --git a/go_to_rust/easy_note/mod.rs b/go_to_rust/easy_note/mod.rs new file mode 100644 index 00000000..aa416dfa --- /dev/null +++ b/go_to_rust/easy_note/mod.rs @@ -0,0 +1 @@ +pub mod src; diff --git a/go_to_rust/easy_note/src/bin/main.rs b/go_to_rust/easy_note/src/bin/main.rs new file mode 100644 index 00000000..f8995633 --- /dev/null +++ b/go_to_rust/easy_note/src/bin/main.rs @@ -0,0 +1,105 @@ +use test_rust::cmd::api::hertz_router::generated_register; +use test_rust::cmd::api::hertz_handler::demoapi; +use test_rust::cmd::api::hertz_handler; +use test_rust::cmd::api::rpc; +use test_rust::cmd::api::mw; +use test_rust::pkg::errno; +use test_rust::pkg::consts::API_SERVICE_NAME; +use test_rust::pkg::consts::EXPORT_ENDPOINT; +use volo_http::server::route::Router; +use volo_http::server::route::get_service; +use volo_http::server::response::IntoResponse; +use volo_http::server::Server; +use volo_http::http::StatusCode; +use volo_http::Address; +use motore::service::service_fn; +use std::net::SocketAddr; + +// register函数用于在Hertz服务器上注册路由和处理器。 +pub fn register() -> Router { + // 1. 调用GeneratedRegister函数,为Hertz服务器配置路由和中间件 + let router = generated_register(); + + // 2. 调用customizedRegister函数,在Hertz服务器上注册自定义路由和处理器 + router.merge(customized_register()) +} + +// customized_register registers custom routers. +pub fn customized_register() -> Router { + Router::new() + // 1. 注册 GET 请求的 '/ping' 路由,并指定处理函数 + .route("/ping", get_service(service_fn(hertz_handler::ping))) + // 2. 为未匹配到任何路由的请求注册处理函数 + .fallback(handle_uri_not_found) + // 3. 为使用了不被允许的方法的请求注册处理函数 + .fallback(handle_method_not_allowed) +} + +// fallback handler for METHOD NOT ALLOWED +async fn handle_method_not_allowed() -> (StatusCode, &'static str) { + demoapi::send_response(errno::SERVICE_ERR, "Method Not Allowed") +} + +// fallback handler for NOT FOUND +async fn handle_uri_not_found() -> (StatusCode, &'static str) { + demoapi::send_response(errno::SERVICE_ERR, "404 Not Found") +} + +pub fn init() { + // 1. 初始化系统中的用户服务客户端和笔记服务客户端 + rpc::init(); + + // 2. 初始化JWT中间件,配置其参数和回调函数 + mw::init_jwt(); + + // 3. 创建并设置新的日志记录器 + // 使用 tracing_subscriber 创建一个新的日志记录器 + // let subscriber = FmtSubscriber::builder() + // .with_max_level(Level::INFO) + // .finish(); + + // // 设置全局日志记录器 + // tracing::subscriber::set_global_default(subscriber) + // .expect("Failed to set global default subscriber"); + + // // // 4. 设置日志记录级别为信息级别 + // info!("System initialized successfully."); +} + +#[volo::main] +async fn main() { + // 1. 创建OpenTelemetry提供者,配置服务名称、导出端点和安全设置 + // let tracer = opentelemetry_otlp::new_pipeline() + // .tracing() + // .with_endpoint(EXPORT_ENDPOINT) + // .with_trace_config( + // sdktrace::config().with_resource(Resource::new(vec![ + // KeyValue::new("service.name", API_SERVICE_NAME), + // ])), + // ) + // .install_simple() + // .expect("Failed to install OpenTelemetry tracer"); + + // let opentelemetry = OpenTelemetryLayer::new(tracer); + // let subscriber = Registry::default().with(opentelemetry); + // tracing::subscriber::set_global_default(subscriber).expect("Failed to set tracing subscriber"); + + // 2. 注册一个延迟关闭操作以确保OpenTelemetry提供者在程序结束时正确关闭 + // (Note: This is a pseudo-code to indicate the intention) + // let _guard = opentelemetry::global::shutdown_tracer_provider(); + + // 3. 初始化系统组件 + init(); + + // 4. 创建一个新的服务器实例,配置端口、处理方法和追踪器 + let app = Router::new() + .merge(register()); // 6. 在服务器上注册路由和处理器 + + let addr = "[::]:8080".parse::().unwrap(); + let addr = Address::from(addr); + + println!("Listening on {addr}"); + + // 7. 启动服务器 + Server::new(app).run(addr).await.unwrap(); +} diff --git a/go_to_rust/easy_note/src/cmd/api/hertz_handler/demoapi/mod.rs b/go_to_rust/easy_note/src/cmd/api/hertz_handler/demoapi/mod.rs new file mode 100644 index 00000000..6c737af0 --- /dev/null +++ b/go_to_rust/easy_note/src/cmd/api/hertz_handler/demoapi/mod.rs @@ -0,0 +1,6 @@ +use volo_http::http; + +// mock for implement stage +pub fn send_response(_err: &'static str, _msg: &'static str) -> (http::StatusCode, &'static str) { + (http::StatusCode::INTERNAL_SERVER_ERROR, _msg) +} diff --git a/go_to_rust/easy_note/src/cmd/api/hertz_handler/mod.rs b/go_to_rust/easy_note/src/cmd/api/hertz_handler/mod.rs new file mode 100644 index 00000000..0e416abb --- /dev/null +++ b/go_to_rust/easy_note/src/cmd/api/hertz_handler/mod.rs @@ -0,0 +1,19 @@ +pub mod demoapi; + +use std::convert::Infallible; +use volo_http::context::ServerContext; +use volo_http::http::StatusCode; +use volo_http::request::ServerRequest; +use volo_http::response::ServerResponse; +use volo_http::server::response::IntoResponse; +use volo_http::json::Json; + +// Ping函数用于处理传入的HTTP请求,并返回一个JSON响应。 +pub async fn ping( + _ctx: &mut ServerContext, // 上下文信息 + _req: ServerRequest, // 请求对象 +) -> Result { + // 1. 调用 RequestContext 的 JSON 方法,传入状态码 200 和包含键值对 {"message": "pong"} 的对象,来生成并发送 JSON 响应。 + let response = Json(serde_json::json!({ "message": "pong" })).into_response(); + Ok(response) +} diff --git a/go_to_rust/easy_note/src/cmd/api/hertz_router/mod.rs b/go_to_rust/easy_note/src/cmd/api/hertz_router/mod.rs new file mode 100644 index 00000000..5e0cbb08 --- /dev/null +++ b/go_to_rust/easy_note/src/cmd/api/hertz_router/mod.rs @@ -0,0 +1,6 @@ +use volo_http::server::route::Router; + +// mock for implement stage +pub fn generated_register() -> Router { + Router::new() +} diff --git a/go_to_rust/easy_note/src/cmd/api/mod.rs b/go_to_rust/easy_note/src/cmd/api/mod.rs new file mode 100644 index 00000000..d0c271d4 --- /dev/null +++ b/go_to_rust/easy_note/src/cmd/api/mod.rs @@ -0,0 +1,4 @@ +pub mod hertz_handler; +pub mod hertz_router; +pub mod mw; +pub mod rpc; diff --git a/go_to_rust/easy_note/src/cmd/api/mw/mod.rs b/go_to_rust/easy_note/src/cmd/api/mw/mod.rs new file mode 100644 index 00000000..6a8090aa --- /dev/null +++ b/go_to_rust/easy_note/src/cmd/api/mw/mod.rs @@ -0,0 +1,7 @@ +use crate::cmd::api::mw; + +// mock for implement stage +pub fn init_jwt() { + // Initialize JWT middleware + todo!("implement it"); +} diff --git a/go_to_rust/easy_note/src/cmd/api/rpc/mod.rs b/go_to_rust/easy_note/src/cmd/api/rpc/mod.rs new file mode 100644 index 00000000..1281a9bc --- /dev/null +++ b/go_to_rust/easy_note/src/cmd/api/rpc/mod.rs @@ -0,0 +1,7 @@ +use crate::cmd::api::rpc; + +// mock for implement stage +pub fn init() { + // Initialize user and note service clients + todo!("implement it"); +} diff --git a/go_to_rust/easy_note/src/cmd/mod.rs b/go_to_rust/easy_note/src/cmd/mod.rs new file mode 100644 index 00000000..e5fdf85e --- /dev/null +++ b/go_to_rust/easy_note/src/cmd/mod.rs @@ -0,0 +1 @@ +pub mod api; diff --git a/go_to_rust/easy_note/src/lib.rs b/go_to_rust/easy_note/src/lib.rs new file mode 100644 index 00000000..e41807c3 --- /dev/null +++ b/go_to_rust/easy_note/src/lib.rs @@ -0,0 +1,2 @@ +pub mod cmd; +pub mod pkg; diff --git a/go_to_rust/easy_note/src/pkg/consts/mod.rs b/go_to_rust/easy_note/src/pkg/consts/mod.rs new file mode 100644 index 00000000..f625a3f8 --- /dev/null +++ b/go_to_rust/easy_note/src/pkg/consts/mod.rs @@ -0,0 +1,6 @@ + +// mock for implement stage +pub const API_SERVICE_NAME: &str = "api-service"; + +// mock for implement stage +pub const EXPORT_ENDPOINT: &str = "http://localhost:4317"; diff --git a/go_to_rust/easy_note/src/pkg/errno/mod.rs b/go_to_rust/easy_note/src/pkg/errno/mod.rs new file mode 100644 index 00000000..95b890c1 --- /dev/null +++ b/go_to_rust/easy_note/src/pkg/errno/mod.rs @@ -0,0 +1,3 @@ + +// mock for implement stage +pub static SERVICE_ERR: &'static str = "Service error"; diff --git a/go_to_rust/easy_note/src/pkg/mod.rs b/go_to_rust/easy_note/src/pkg/mod.rs new file mode 100644 index 00000000..6285c098 --- /dev/null +++ b/go_to_rust/easy_note/src/pkg/mod.rs @@ -0,0 +1,2 @@ +pub mod consts; +pub mod errno;