5个版本
0.2.5 | 2024年8月5日 |
---|---|
0.2.4 | 2024年8月2日 |
0.2.1 | 2024年8月1日 |
0.2.0 | 2024年7月30日 |
#336 in HTTP服务器
每月479次下载
用于 argentum_user_account_inf…
190KB
5K SLoC
Argentum 用户账户组件 API 的 REST 库
用户账户 API
概述
该服务器由以下链接生成的 OpenAPI 规范生成:https://gitlab.com/argentum-toolkit/argentum(argentum-rest-generator).
- API版本:0.2.0-dev
- 包名:argentum_user_account_rest
如何使用
创建服务器应用程序
克隆此存储库或将其发布为crate
[source,bash]
....
$ git clone https://github.com/argentum-toolkit/argentum rest
....
链接:如何发布
创建应用程序
[source,bash] .... $ cargo new app --name argentum_user_account_rest_app $ cd app ....
添加依赖项
.Cargo.toml 示例 [source,toml] .... [package] name = "argentum_user_account_rest_app" description = "Argentum 用户账户组件 API" version = "0.2.0-dev" edition = "2021"
[dependencies] argentum_log_business = { path = "../../argentum_log/business", version = ">=0.2.0-dev,<0.3.0" } argentum_log_infrastructure = { path = "../../argentum_log/infrastructure", version = ">=0.2.0-dev,<0.3.0" } argentum_rest_infrastructure = { path = "../../argentum_rest/infrastructure", version = ">=0.2.0-dev,<0.3.0" } argentum_standard_business = { path = "../../argentum_standard/business", version = ">=0.2.0-dev,<0.3.0" } argentum_user_business = { path = "../../argentum_user/business", version = ">=0.2.0-dev,<0.3.0" }
argentum_user_account_rest = { path = "../rest" } tokio = { version = "1.34.*", features = ["full"] } ....
.main.rs [source,rust] .... mod di;
use crate::di::di_factory;
tokio::main 异步函数 main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let di = di_factory().await; di.server.serve().await } ....
.di.rs [source,rust] .... 使用 argentum_log_business::{DefaultLogger, Level}; 使用 argentum_log_infrastructure::stdout::PrettyWriter; 使用 argentum_rest_infrastructure::service::{RouterCombinator, Server}; 使用 std::net::SocketAddr;
使用 argentum_rest_infrastructure::RestDiC; 使用 argentum_user_business::use_case::user_authenticates_with_token::UserAuthenticatesWithTokenUc; 使用 argentum_user_account_rest::ApiDiC; 使用 std::sync::Arc;
pub struct DiC { // 公共服务 pub server: Arc, }
impl DiC { pub fn new(server: Arc) -> DiC { DiC { server } } }
pub async fn di_factory() -> DiC { let log_writer = Arc::new(PrettyWriter::new()); let logger = Arc::new(DefaultLogger::new(Level::Trace, log_writer));
//TODO: create instance of
let user_authenticates_with_token_uc: Arc<UserAuthenticatesWithTokenUc>;
// ...
let rest_di = RestDiC::new(logger.clone(), user_authenticates_with_token_uc);
let u_api_di = ApiDiC::new(
"/api/v1".to_string(),
rest_di.request_transformer.clone(),
rest_di.bearer_authenticator.clone(),
// Implementations of handler traits (`use argentum_user_account_rest::server::handler::*`)
//...
rest_di.error_pre_handler.clone(),
);
let listen = "0.0.0.0:8080";
// or something like that:
// let listen = "172.18.0.1:8080";
// let listen = "127.0.0.1:8080";
let addr: SocketAddr = listen.parse().expect("Unable to parse socket address");
let router = Arc::new(RouterCombinator::new(
vec![
u_api_di.router,
//other routers
],
rest_di.error_pre_handler,
));
let server = Arc::new(Server::new(
addr,
router, // or just u_api_di.router
rest_di.response_transformer,
rest_di.error_handler,
logger,
));
DiC::new(server)
}
....
依赖项
~42–59MB
~1M SLoC