5 个版本 (3 个稳定版)
1.0.3 | 2024 年 5 月 7 日 |
---|---|
1.0.1 | 2023 年 11 月 27 日 |
1.0.0 | 2023 年 4 月 26 日 |
0.3.0 | 2023 年 4 月 21 日 |
0.2.0 | 2023 年 4 月 12 日 |
#471 in 网页编程
每月下载量 77
在 2 crate 中使用
140KB
2.5K SLoC
Springtime
基于 springtime-di 依赖注入的应用框架。受 Java 中的 Spring 框架 的启发,Springtime 通过确保所有应用组件之间都正确解耦,并由依赖注入系统管理,提供了一种创建高级模块化 Rust 应用程序的方法。
核心概念围绕提供基本应用服务,例如日志记录,并运行有序的 ApplicationRunner
。一个 ApplicationRunner
代表根应用服务,它启动应用逻辑。运行者的例子包括 HTTP 服务器、消息系统消费者,甚至是命令行应用程序。此 crate 为那些希望利用 Springtime 提供额外功能(例如 Web 服务器运行者)的更专业化的 crate 提供构建块。
功能
- 自动应用逻辑发现和运行(基于 DI)
- 运行者优先级
- 可配置的日志实现(基于跟踪)
- 异步 + 同步支持(运行时无关)
基本用法
Springtime 可高度配置,但最基本的使用示例非常简单,包括创建一个 Application
实例并调用 run()
。有关 教程、高级功能和模式,请参阅 示例,它构成了一个逐步指南。
以下示例假设您熟悉 springtime-di 并使用 async
功能。
// the following example shows how to inject an example HTTP server and run it
// this is an application runner, which will run when the application starts; the framework will
// automatically discover it using dependency injection
#[derive(Component)]
struct HttpRunner {
// let the framework inject the example server
http_server: ComponentInstancePtr<HttpServer>,
}
#[component_alias]
impl ApplicationRunner for HttpRunner {
// note: BoxFuture is only needed when using the "async" feature
fn run(&self) -> BoxFuture<'_, Result<(), ErrorPtr>> {
// run the example server (run() is assumed to return a Future)
self.http_server.run().boxed()
}
}
// note: for the sake of simplicity, errors are unwrapped, rather than gracefully handled
#[tokio::main]
async fn main() {
// create our application, which will detect all runners
let mut application =
application::create_default().expect("unable to create default application");
// runs all ApplicationRunners, which means our HttpServer
application.run().await.expect("error running application");
}
依赖项
~7–16MB
~191K SLoC