diff --git a/.env b/.env index 066c910..edd77db 100644 --- a/.env +++ b/.env @@ -1,2 +1,3 @@ DATABASE_URL=mysql://root:archlinux0311@localhost:3306/likeadmin -UPLOAD_PATH=D:/Downloads/upload_path +#UPLOAD_PATH=/var/www/uploads/caszl +UPLOAD_PATH="/Volumes/iMac Doc/likeadmin-java" diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..bc0032d --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/sshConfigs.xml b/.idea/sshConfigs.xml new file mode 100644 index 0000000..35f49d7 --- /dev/null +++ b/.idea/sshConfigs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/webServers.xml b/.idea/webServers.xml new file mode 100644 index 0000000..2dbeea8 --- /dev/null +++ b/.idea/webServers.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 7b376a2..b3cbed2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -337,10 +337,13 @@ dependencies = [ "axum", "chrono", "dotenvy", + "hyper", "sea-orm", "serde", "serde_json", "tokio", + "tower", + "tower-http", ] [[package]] @@ -713,6 +716,25 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" +[[package]] +name = "h2" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91fc23aa11be92976ef4729127f1a74adf36d8436f7816b185d18df956790833" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap 1.9.3", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -820,6 +842,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" + [[package]] name = "httparse" version = "1.8.0" @@ -842,6 +870,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", + "h2", "http", "http-body", "httparse", @@ -888,6 +917,16 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + [[package]] name = "indexmap" version = "2.1.0" @@ -1015,6 +1054,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1905,7 +1954,7 @@ dependencies = [ "futures-util", "hashlink", "hex", - "indexmap", + "indexmap 2.1.0", "log", "memchr", "native-tls", @@ -2264,6 +2313,20 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + [[package]] name = "toml" version = "0.5.11" @@ -2289,6 +2352,31 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" +dependencies = [ + "bitflags 2.4.1", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-range-header", + "httpdate", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower-layer" version = "0.3.2" @@ -2345,6 +2433,15 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.13" diff --git a/Cargo.toml b/Cargo.toml index d1287f5..822d81d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,10 @@ edition = "2021" axum = "0.6.20" chrono = "0.4.31" dotenvy = "0.15.7" +hyper = { version = "0.14.27", features = ["full"] } sea-orm = { version = "0.12.4", features = ["sqlx-mysql", "runtime-tokio-native-tls", "macros"] } serde = { version = "1.0.192", features = ["derive"] } serde_json = "1.0.108" tokio = { version = "1.34.0", features = ["full"] } +tower = "0.4.13" +tower-http = { version = "0.4.4", features = ["fs"] } diff --git a/src/main.rs b/src/main.rs index 84e0f50..3c77d6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,10 +18,11 @@ use std::sync::Arc; async fn main() { dotenvy::dotenv().ok(); let db_url = env::var("DATABASE_URL").expect(".env 文件中没有设置 DATABASE_URL"); + let upload_path = env::var("UPLOAD_PATH").expect(".env 文件中没有设置 UPLOAD_PATH"); let db_conn = Database::connect(&db_url).await.expect("数据库链接失败"); - let app_state = AppState { db_conn }; + let app_state = AppState { db_conn, upload_path }; let app = routes::create_routes(Arc::new(app_state)); Server::bind(&"0.0.0.0:3000".parse().unwrap()) diff --git a/src/routes.rs b/src/routes.rs index b9140c6..4033f16 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -3,12 +3,14 @@ use crate::state::app::AppState; use axum::routing::{get, IntoMakeService}; use axum::{Router, ServiceExt}; use std::sync::Arc; +use tower_http::services::ServeDir; pub fn create_routes(app_state: Arc) -> IntoMakeService { Router::new() .nest( "/api", Router::new() + .nest_service("/uploads",ServeDir::new(&app_state.upload_path)) .nest("/article", Router::new().route("/list", get(article_list))) .nest( "/pc", diff --git a/src/state/app.rs b/src/state/app.rs index d1eaf4e..439b079 100644 --- a/src/state/app.rs +++ b/src/state/app.rs @@ -2,4 +2,5 @@ use sea_orm::DatabaseConnection; pub struct AppState { pub db_conn: DatabaseConnection, + pub upload_path: String, }