Crates.io | spring |
lib.rs | spring |
version | |
source | src |
created_at | 2024-08-07 04:43:51.684699+00 |
updated_at | 2024-12-14 09:08:07.485917+00 |
description | Rust microservice framework like spring boot in java |
homepage | |
repository | https://github.com/spring-rs/spring-rs |
max_upload_size | |
id | 1328063 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
spring-rs is an application framework that emphasizes convention over configuration, inspired by Java's SpringBoot. spring-rs provides an easily extensible plug-in system for integrating excellent projects in the Rust community, such as axum, sqlx, sea-orm, etc.
Compared with SpringBoot in java, spring-rs has higher performance and lower memory usage, allowing you to completely get rid of the bloated JVM and travel light.
web
use spring::{auto_config, App};
use spring_sqlx::{
sqlx::{self, Row},
ConnectPool, SqlxPlugin
};
use spring_web::{get, route};
use spring_web::{
error::Result, extractor::{Path, Component}, handler::TypeRouter, axum::response::IntoResponse, Router,
WebConfigurator, WebPlugin,
};
use anyhow::Context;
#[auto_config(WebConfigurator)]
#[tokio::main]
async fn main() {
App::new()
.add_plugin(SqlxPlugin)
.add_plugin(WebPlugin)
.run()
.await
}
#[get("/")]
async fn hello_world() -> impl IntoResponse {
"hello world"
}
#[route("/hello/:name", method = "GET", method = "POST")]
async fn hello(Path(name): Path<String>) -> impl IntoResponse {
format!("hello {name}")
}
#[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)
}
job
use anyhow::Context;
use spring::{auto_config, App};
use spring_job::{cron, fix_delay, fix_rate};
use spring_job::{extractor::Component, JobConfigurator, JobPlugin};
use spring_sqlx::{
sqlx::{self, Row},
ConnectPool, SqlxPlugin,
};
use std::time::{Duration, SystemTime};
#[auto_config(JobConfigurator)]
#[tokio::main]
async fn main() {
App::new()
.add_plugin(JobPlugin)
.add_plugin(SqlxPlugin)
.run()
.await;
tokio::time::sleep(Duration::from_secs(100)).await;
}
#[cron("1/10 * * * * *")]
async fn cron_job(Component(db): Component<ConnectPool>) {
let time: String = sqlx::query("select TO_CHAR(now(),'YYYY-MM-DD HH24:MI:SS') as time")
.fetch_one(&db)
.await
.context("query failed")
.unwrap()
.get("time");
println!("cron scheduled: {:?}", time)
}
#[fix_delay(5)]
async fn fix_delay_job() {
let now = SystemTime::now();
let datetime: sqlx::types::chrono::DateTime<sqlx::types::chrono::Local> = now.into();
let formatted_time = datetime.format("%Y-%m-%d %H:%M:%S");
println!("fix delay scheduled: {}", formatted_time)
}
#[fix_rate(5)]
async fn fix_rate_job() {
tokio::time::sleep(Duration::from_secs(10)).await;
let now = SystemTime::now();
let datetime: sqlx::types::chrono::DateTime<sqlx::types::chrono::Local> = now.into();
let formatted_time = datetime.format("%Y-%m-%d %H:%M:%S");
println!("fix rate scheduled: {}", formatted_time)
}
spring-web
: Based on axum
spring-sqlx
: Integrated with sqlx
spring-postgres
: Integrated with rust-postgres
spring-sea-orm
: Integrated with sea-orm
spring-redis
: Integrated with redis
spring-mail
: Integrated with lettre
spring-job
: Integrated with tokio-cron-scheduler
spring-stream
: Integrate sea-streamer
to implement message processing such as redis-stream and kafkaspring-opentelemetry
: Integrate with opentelemetry
to implement full observability of logging, metrics, tracingspring-tarpc
: Integratetarpc
to implement RPC callsWe also welcome community experts to contribute their own plugins. Contributing →
Click here to view common problems encountered when using spring-rs
Help →