This commit is contained in:
胡天 2024-02-04 16:35:12 +08:00
parent 84458b3d3e
commit 46dedb4306
10 changed files with 3239 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
.vscode
.idea

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="46" name="Rust" />
</Languages>
</inspection_tool>
</profile>
</component>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/axum-with-seaorm.iml" filepath="$PROJECT_DIR$/.idea/axum-with-seaorm.iml" />
</modules>
</component>
</project>

7
.idea/sqldialects.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/app.sql" dialect="GenericSQL" />
<file url="PROJECT" dialect="MySQL" />
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

3094
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

19
Cargo.toml Normal file
View File

@ -0,0 +1,19 @@
[package]
name = "axum-with-seaorm"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
askama = "0.12.1"
axum = "0.7.4"
sea-orm = { version = "0.12.12", features = ["sqlx-mysql", "runtime-tokio-rustls"] }
tokio = { version = "1.35.1", features = ["full"] }
serde = { version = "1.0.196", features = ["derive"] }
config = "0.13.4"
dotenvy = "0.15.7"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "time", "local-time"] }
serde_json = "1.0.113"
chrono = "0.4.33"

73
src/main.rs Normal file
View File

@ -0,0 +1,73 @@
use std::sync::Arc;
use sea_orm::{ConnectOptions, Database};
use tokio::signal;
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
use tracing_subscriber::fmt::time::LocalTime;
use axum_with_seaorm::{config::AppConfig, router, state::AppState};
fn init_trace_logger() {
// 设置日志级别 根据RUST_LOG
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let timer = LocalTime::rfc_3339();
// 构建layer 控制台打印
let console_layer = fmt::layer().with_timer(timer);
tracing_subscriber::registry()
.with(env_filter)
.with(console_layer)
.init();
}
#[tokio::main]
async fn main() {
// 解析 .env 文件
dotenvy::dotenv().expect("解析.env文件失败");
let _guard = init_trace_logger();
let cfg = AppConfig::new()
.map_err(|e| tracing::error!("初始化配置失败:{}", e.to_string()))
.unwrap();
let mut opt = ConnectOptions::new(&cfg.mysql.dsn);
opt.max_connections(cfg.mysql.max_cons);
let app = router::init()
.with_state(Arc::new(AppState {
conn: Database::connect(opt).await.unwrap(),
}));
let listener = tokio::net::TcpListener::bind(&cfg.web.addr).await.unwrap();
tracing::info!("服务器运行于: {}", listener.local_addr().unwrap());
axum::serve(listener, app).with_graceful_shutdown(shutdown_signal()).await.unwrap()
}
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
tracing::info!("signal received, starting graceful shutdown");
}