diff --git a/Cargo.lock b/Cargo.lock index 7556d89..e7bbc9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,6 +65,17 @@ version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" +[[package]] +name = "async-trait" +version = "0.1.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6dde6e4ed435a4c1ee4e73592f5ba9da2151af10076cc04858746af9352d09" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.23", +] + [[package]] name = "atoi" version = "2.0.0" @@ -91,6 +102,55 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "axum" +version = "0.6.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" +dependencies = [ + "async-trait", + "axum-core", + "bitflags 1.3.2", + "bytes", + "futures-util", + "http", + "http-body", + "hyper", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "mime", + "rustversion", + "tower-layer", + "tower-service", +] + [[package]] name = "backtrace" version = "0.3.68" @@ -106,12 +166,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - [[package]] name = "base64" version = "0.21.2" @@ -581,31 +635,6 @@ dependencies = [ "hashbrown 0.14.0", ] -[[package]] -name = "headers" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584" -dependencies = [ - "base64 0.13.1", - "bitflags 1.3.2", - "bytes", - "headers-core", - "http", - "httpdate", - "mime", - "sha1", -] - -[[package]] -name = "headers-core" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" -dependencies = [ - "http", -] - [[package]] name = "heck" version = "0.3.3" @@ -914,6 +943,12 @@ version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" +[[package]] +name = "matchit" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed1202b2a6f884ae56f04cff409ab315c5ce26b5e58d7412e484f01fd52f52ef" + [[package]] name = "md-5" version = "0.10.5" @@ -935,16 +970,6 @@ 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" @@ -971,24 +996,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "multer" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01acbdc23469fd8fe07ab135923371d5f5a422fbf9c522158677c8eb15bc51c2" -dependencies = [ - "bytes", - "encoding_rs", - "futures-util", - "http", - "httparse", - "log", - "memchr", - "mime", - "spin 0.9.8", - "version_check", -] - [[package]] name = "native-tls" version = "0.2.11" @@ -1297,6 +1304,7 @@ name = "qr-backend" version = "0.0.1" dependencies = [ "anyhow", + "axum", "chrono", "reqwest", "serde", @@ -1309,7 +1317,6 @@ dependencies = [ "tracing-subscriber", "url", "uuid", - "warp", ] [[package]] @@ -1366,7 +1373,7 @@ version = "0.11.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cde824a14b7c14f85caff81225f411faacc04a2013f41670f41443742b1c1c55" dependencies = [ - "base64 0.21.2", + "base64", "bytes", "encoding_rs", "futures-core", @@ -1477,7 +1484,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2" dependencies = [ - "base64 0.21.2", + "base64", ] [[package]] @@ -1500,6 +1507,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustversion" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" + [[package]] name = "ryu" version = "1.0.14" @@ -1515,12 +1528,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "scoped-tls" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" - [[package]] name = "scopeguard" version = "1.1.0" @@ -1591,6 +1598,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4beec8bce849d58d06238cb50db2e1c417cfeafa4c63f692b15c82b7c80f8335" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1818,7 +1835,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8264c59b28b6858796acfcedc660aa4c9075cc6e4ec8eb03cdca2a3e725726db" dependencies = [ "atoi", - "base64 0.21.2", + "base64", "bitflags 2.3.3", "byteorder", "bytes", @@ -1862,7 +1879,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cab6147b81ca9213a7578f1b4c9d24c449a53953cd2222a7b5d7cd29a5c3139" dependencies = [ "atoi", - "base64 0.21.2", + "base64", "bitflags 2.3.3", "byteorder", "chrono", @@ -1988,6 +2005,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "tempfile" version = "3.6.0" @@ -2130,31 +2153,41 @@ dependencies = [ ] [[package]] -name = "tokio-tungstenite" -version = "0.18.0" +name = "tokio-util" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54319c93411147bced34cb5609a80e0a8e44c5999c93903a81cd866630ec0bfd" +checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" dependencies = [ - "futures-util", - "log", + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", "tokio", - "tungstenite", + "tracing", ] [[package]] -name = "tokio-util" -version = "0.7.8" +name = "tower" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" dependencies = [ - "bytes", "futures-core", - "futures-sink", + "futures-util", + "pin-project", "pin-project-lite", "tokio", + "tower-layer", + "tower-service", "tracing", ] +[[package]] +name = "tower-layer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" + [[package]] name = "tower-service" version = "0.3.2" @@ -2226,40 +2259,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" -[[package]] -name = "tungstenite" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30ee6ab729cd4cf0fd55218530c4522ed30b7b6081752839b68fcec8d0960788" -dependencies = [ - "base64 0.13.1", - "byteorder", - "bytes", - "http", - "httparse", - "log", - "rand", - "sha1", - "thiserror", - "url", - "utf-8", -] - [[package]] name = "typenum" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" -[[package]] -name = "unicase" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" -dependencies = [ - "version_check", -] - [[package]] name = "unicode-bidi" version = "0.3.13" @@ -2316,12 +2321,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "utf-8" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" - [[package]] name = "uuid" version = "1.4.0" @@ -2365,37 +2364,6 @@ dependencies = [ "try-lock", ] -[[package]] -name = "warp" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba431ef570df1287f7f8b07e376491ad54f84d26ac473489427231e1718e1f69" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "headers", - "http", - "hyper", - "log", - "mime", - "mime_guess", - "multer", - "percent-encoding", - "pin-project", - "rustls-pemfile", - "scoped-tls", - "serde", - "serde_json", - "serde_urlencoded", - "tokio", - "tokio-stream", - "tokio-tungstenite", - "tokio-util", - "tower-service", - "tracing", -] - [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 52d8620..3ea48fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] anyhow = "1.0.71" +axum = "0.6.20" chrono = { version = "0.4.26", features = ["serde"] } reqwest = { version = "0.11.18", features = ["json", "rustls-tls"] } serde = { version = "1.0.166", features = ["derive"] } @@ -20,21 +21,3 @@ tracing = "0.1.37" tracing-subscriber = "0.3.17" url = "2.4.0" uuid = { version = "1.4.0", features = ["serde", "v4"] } -warp = "0.3.5" -#tokio = { version = "1.25.0", features = ["full"] } -#serde = { version = "1.0.152", features = ["derive"] } -#warp = { version = "0.3.3", default-features = false } -#tracing = "0.1.37" -#tracing-futures = "0.2.5" -#tracing-subscriber = "0.3.16" -#futures = "0.3.26" -#anyhow = "1.0.69" -#structopt = "0.3.26" -#async-trait = "0.1.64" -#thiserror = "1.0.38" -#chrono = "0.4.23" -#sqlx = { git = "https://github.com/launchbadge/sqlx", features = ["postgres", "runtime-tokio-rustls"] } -#jsonwebtoken = "8.2.0" -#reqwest = { version = "0.11.14", default-features = false, features = ["json", "rustls-tls"] } -#url = "2.3.1" -#serde_json = "1.0.93" diff --git a/src/app.rs b/src/app.rs index cd5a7cd..a021155 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,13 @@ use anyhow::{Context, Result}; -use sqlx::postgres::PgPool; +use axum::{ + extract::Json, + http::StatusCode, + routing::{get, post}, + Router, +}; +use sqlx::{pool::Pool, postgres::PgPool, postgres::Postgres}; use std::net::SocketAddr; -use warp::Filter; +use std::sync::Arc; /// サーバーの実体 /// データベースを起動してエントリポイントに応じて関数を呼び出す @@ -10,51 +16,39 @@ pub async fn app(bind: SocketAddr) -> Result<()> { std::env::var("DATABASE_URL").context("Environment variable not set: DATABASE_URL")?; // migrateファイルを適用 - let conn = PgPool::connect(&database_url).await?; + let conn = Arc::new(PgPool::connect(&database_url).await?); crate::database::migrate(&mut conn.acquire().await?).await?; - warp::serve(ping().or(insert_equipment(&conn))) - .run(bind) - .await; + + // pathと関数の実体の紐づけ + let app = Router::new().route("/ping", get(ping)).route( + "/insert_equipment", + post({ + let conn = Arc::clone(&conn); + move |body| insert_equipment(body, conn) + }), + ); + + // サーバーの実行 + axum::Server::bind(&bind) + .serve(app.into_make_service()) + .await?; + Ok(()) } /// ダミー -pub fn ping() -> impl Filter + Clone { - warp::path("ping") - .and(warp::get()) - .map(|| String::from("ping")) +pub async fn ping() -> &'static str { + "pong" } /// 備品情報の登録を行うエンドポイント /// - https://github.com/sohosai/qr-backend/issues/11 -pub fn insert_equipment<'a, E>( - conn: E, -) -> impl Filter + Clone + 'a -where - E: sqlx::Executor<'a, Database = sqlx::Postgres> + Clone, -{ - warp::path("insert_equipment") - .and(warp::post()) - .and(warp::body::json::()) - .and_then( - |equipment: crate::Equipment| async { - if true { - Ok(String::new()) - } else { - Err(warp::reject::reject()) - } - }, - // クロージャの外部にある`conn`を参照してしまっているので - // ``` - // expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce` - // ``` - // というエラーが出てダメ - // どうすれば良いのかわからず停止 - /*async { - match crate::database::insert_equipment::insert_equipment(conn, equipment).await { - Ok(()) => Ok(String::new()),//Ok(warp::http::StatusCode::ACCEPTED), - _ => Err(warp::reject::reject()), - } - }*/ - ) +pub async fn insert_equipment( + Json(equipment): Json, + conn: Arc>, +) -> StatusCode { + match crate::database::insert_equipment::insert_equipment(&*conn, equipment).await { + Ok(()) => StatusCode::ACCEPTED, + _ => StatusCode::BAD_REQUEST, + } }