快速上手

在这个页面,我会介绍spring-rs如何快速上手spring-rs

准备环境

  • rust ≥ 1.75

添加依赖

在你的Cargo.toml文件中添加下面的依赖

[dependencies]
# spring提供了核心的插件系统和有用的过程宏
spring = "0.1.1"
# 如果你准备写web应用就添加spring-web
spring-web = "0.1.1"
# 如果应用需要和数据库交互就添加spring-sqlx
spring-sqlx = { version="0.1.1", features = ["mysql"] }
# spring-rs项目默认使用tokio异步运行时
tokio = "1"

编写代码

use anyhow::Context;
use spring::{auto_config, App};
use spring_sqlx::{
    sqlx::{self, Row},
    ConnectPool, SqlxPlugin,
};
use spring_web::{
    axum::response::IntoResponse,
    error::Result,
    extractor::{Component, Path},
    WebConfigurator, WebPlugin,
};
use spring_web::{get, route};

// 主函数入口
#[auto_config(WebConfigurator)]   // 自动扫描web router
#[tokio::main]
async fn main() {
    App::new()
        .add_plugin(SqlxPlugin)  // 添加插件
        .add_plugin(WebPlugin)
        .run()
        .await
}

// get宏指定Http Method和请求路径。spring-rs还提供了post、delete、patch等其他标准http method宏
#[get("/")]
async fn hello_world() -> impl IntoResponse {
    "hello world"
}

// 也可以使用route宏指定Http Method和请求路径。Path从HTTP请求中提取请求路径中的参数
#[route("/hello/:name", method = "GET", method = "POST")]
async fn hello(Path(name): Path<String>) -> impl IntoResponse {
    format!("hello {name}")
}

// Component可以抽取由Sqlx插件在AppState中的注册的连接池
#[get("/version")]
async fn sqlx_request_handler(Component(pool): Component<ConnectPool>) -> Result<String> {
    let version = sqlx::query("select version() as version")
        .fetch_one(&pool)
        .await
        .context("sqlx query failed")?
        .get("version");
    Ok(version)
}

对应用进行配置

在项目的根路径下创建一个config目录,这里会存储spring-rs的配置文件。

你可以在该目录下先创建一个app.toml文件,内容如下:

[web]
port = 8000                  # 配置web服务端口,如果不配置默认就是8080端口

[sqlx]                       # 配置sqlx的数据库连接信息
uri = "mysql://user:password@127.0.0.1:3306"

spring-rs支持多环境配置:dev(开发)、test(测试)、prod(生产),分别对应着app-dev.tomlapp-dev.tomlapp-prod.toml三个配置文件。环境配置文件中的配置会覆盖app.toml主配置文件的配置项。

spring-rs会根据SPRING_ENV环境变量激活对应环境的配置文件。

运行

编码完成,请确保你的数据库能正常连接,然后就让我们开始运行起来吧。

cargo run