From 94399676a2faec3bdc59010464dd17d2997ceee9 Mon Sep 17 00:00:00 2001 From: jonaro00 <54029719+jonaro00@users.noreply.github.com> Date: Mon, 8 Jan 2024 18:48:48 +0100 Subject: [PATCH] chore: update axum examples to axum 0.7 (#123) --- axum/hello-world/Cargo.toml | 2 +- axum/jwt-authentication/Cargo.toml | 3 ++- axum/jwt-authentication/src/main.rs | 7 +++++-- axum/metadata/Cargo.toml | 2 +- axum/postgres/Cargo.toml | 2 +- axum/static-files/Cargo.toml | 4 ++-- axum/static-next-server/Cargo.toml | 4 ++-- axum/static-next-server/src/main.rs | 10 +++++----- axum/turso/Cargo.toml | 10 +++++----- axum/websocket/Cargo.toml | 7 +++---- axum/websocket/src/main.rs | 7 +------ axum/with-state/Cargo.toml | 2 +- custom-resource/pdo/Cargo.toml | 2 +- custom-service/request-scheduler/Cargo.toml | 4 +--- custom-service/request-scheduler/src/lib.rs | 15 +++++++++++---- fullstack-templates/saas/backend/Cargo.toml | 10 +++++----- fullstack-templates/saas/backend/src/auth.rs | 19 +++++++++---------- fullstack-templates/saas/backend/src/main.rs | 14 +------------- other/standalone-binary/Cargo.toml | 2 +- other/standalone-binary/src/bin/standalone.rs | 4 ++-- 20 files changed, 60 insertions(+), 70 deletions(-) diff --git a/axum/hello-world/Cargo.toml b/axum/hello-world/Cargo.toml index 77d2ebd4..7cca9695 100644 --- a/axum/hello-world/Cargo.toml +++ b/axum/hello-world/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -axum = "0.6.20" +axum = "0.7.3" shuttle-axum = "0.35.0" shuttle-runtime = "0.35.0" tokio = "1.28.2" diff --git a/axum/jwt-authentication/Cargo.toml b/axum/jwt-authentication/Cargo.toml index 9ae088f9..364f8baf 100644 --- a/axum/jwt-authentication/Cargo.toml +++ b/axum/jwt-authentication/Cargo.toml @@ -4,7 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] -axum = { version = "0.6.18", features = ["headers"] } +axum = "0.7.3" +axum-extra = { version = "0.9.1", features = ["typed-header"] } jsonwebtoken = "8.3.0" once_cell = "1.18.0" serde = { version = "1.0.188", features = ["derive"] } diff --git a/axum/jwt-authentication/src/main.rs b/axum/jwt-authentication/src/main.rs index bad2f11d..9413efc6 100644 --- a/axum/jwt-authentication/src/main.rs +++ b/axum/jwt-authentication/src/main.rs @@ -1,11 +1,14 @@ use axum::{ async_trait, extract::FromRequestParts, - headers::{authorization::Bearer, Authorization}, http::{request::Parts, StatusCode}, response::{IntoResponse, Response}, routing::{get, post}, - Json, RequestPartsExt, Router, TypedHeader, + Json, RequestPartsExt, Router, +}; +use axum_extra::{ + headers::{authorization::Bearer, Authorization}, + TypedHeader, }; use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation}; use once_cell::sync::Lazy; diff --git a/axum/metadata/Cargo.toml b/axum/metadata/Cargo.toml index 3434034b..b47ec123 100644 --- a/axum/metadata/Cargo.toml +++ b/axum/metadata/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -axum = "0.6.18" +axum = "0.7.3" shuttle-axum = "0.35.0" shuttle-runtime = "0.35.0" shuttle-metadata = "0.35.0" diff --git a/axum/postgres/Cargo.toml b/axum/postgres/Cargo.toml index 838cd0c0..d1d2fcff 100644 --- a/axum/postgres/Cargo.toml +++ b/axum/postgres/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -axum = "0.6.18" +axum = "0.7.3" serde = { version = "1.0.188", features = ["derive"] } shuttle-axum = "0.35.0" shuttle-runtime = "0.35.0" diff --git a/axum/static-files/Cargo.toml b/axum/static-files/Cargo.toml index c1e717c7..0f2e1190 100644 --- a/axum/static-files/Cargo.toml +++ b/axum/static-files/Cargo.toml @@ -5,8 +5,8 @@ edition = "2021" publish = false [dependencies] -axum = "0.6.18" +axum = "0.7.3" shuttle-axum = "0.35.0" shuttle-runtime = "0.35.0" tokio = "1.28.2" -tower-http = { version = "0.4.0", features = ["fs"] } +tower-http = { version = "0.5.0", features = ["fs"] } diff --git a/axum/static-next-server/Cargo.toml b/axum/static-next-server/Cargo.toml index 7c4421e6..3deba2d9 100644 --- a/axum/static-next-server/Cargo.toml +++ b/axum/static-next-server/Cargo.toml @@ -5,8 +5,8 @@ edition = "2021" publish = false [dependencies] -axum = "0.6.10" -axum-extra = { version = "0.4.2", features = ["spa"] } +axum = "0.7.3" shuttle-axum = "0.35.0" shuttle-runtime = "0.35.0" tokio = "1.26.0" +tower-http = { version = "0.5.0", features = ["fs"] } diff --git a/axum/static-next-server/src/main.rs b/axum/static-next-server/src/main.rs index 97232acd..c18a384d 100644 --- a/axum/static-next-server/src/main.rs +++ b/axum/static-next-server/src/main.rs @@ -1,12 +1,12 @@ -use std::path::PathBuf; - use axum::Router; -use axum_extra::routing::SpaRouter; +use tower_http::services::{ServeDir, ServeFile}; #[shuttle_runtime::main] async fn axum() -> shuttle_axum::ShuttleAxum { - let router = - Router::new().merge(SpaRouter::new("/", PathBuf::from("static")).index_file("index.html")); + let router = Router::new().nest_service( + "/", + ServeDir::new("static").not_found_service(ServeFile::new("static/index.html")), + ); Ok(router.into()) } diff --git a/axum/turso/Cargo.toml b/axum/turso/Cargo.toml index 2b322503..4ef03e6b 100644 --- a/axum/turso/Cargo.toml +++ b/axum/turso/Cargo.toml @@ -4,11 +4,11 @@ version = "0.1.0" edition = "2021" [dependencies] -axum = { version = "0.6.18" } -shuttle-axum = { version = "0.35.0" } -shuttle-runtime = { version = "0.35.0" } -shuttle-turso = { version = "0.35.0" } +axum = "0.7.3" +shuttle-axum = "0.35.0" +shuttle-runtime = "0.35.0" +shuttle-turso = "0.35.0" libsql-client = "0.31.0" -tokio = { version = "1.26.0" } +tokio = "1.26.0" serde = { version = "1.0.164", features = ["derive"] } serde_json = "1.0.99" diff --git a/axum/websocket/Cargo.toml b/axum/websocket/Cargo.toml index e168022e..779d75e2 100644 --- a/axum/websocket/Cargo.toml +++ b/axum/websocket/Cargo.toml @@ -4,14 +4,13 @@ version = "0.1.0" edition = "2021" [dependencies] -axum = { version = "0.6.18", features = ["ws"] } +axum = { version = "0.7.3", features = ["ws"] } chrono = { version = "0.4.26", features = ["serde"] } futures = "0.3.28" -hyper = { version = "0.14.26", features = ["client", "http2"] } -hyper-tls = "0.5.0" +reqwest = "0.11.23" serde = { version = "1.0.163", features = ["derive"] } serde_json = "1.0.96" shuttle-axum = "0.35.0" shuttle-runtime = "0.35.0" tokio = "1.28.2" -tower-http = { version = "0.4.0", features = ["fs"] } +tower-http = { version = "0.5.0", features = ["fs"] } diff --git a/axum/websocket/src/main.rs b/axum/websocket/src/main.rs index 38bbdda7..dac3676f 100644 --- a/axum/websocket/src/main.rs +++ b/axum/websocket/src/main.rs @@ -11,8 +11,6 @@ use axum::{ }; use chrono::{DateTime, Utc}; use futures::{SinkExt, StreamExt}; -use hyper::{Client, Uri}; -use hyper_tls::HttpsConnector; use serde::Serialize; use shuttle_axum::ShuttleAxum; use tokio::{ @@ -50,12 +48,9 @@ async fn axum() -> ShuttleAxum { let state_send = state.clone(); tokio::spawn(async move { let duration = Duration::from_secs(PAUSE_SECS); - let https = HttpsConnector::new(); - let client = Client::builder().build::<_, hyper::Body>(https); - let uri: Uri = STATUS_URI.parse().unwrap(); loop { - let is_up = client.get(uri.clone()).await; + let is_up = reqwest::get(STATUS_URI).await; let is_up = is_up.is_ok(); let response = Response { diff --git a/axum/with-state/Cargo.toml b/axum/with-state/Cargo.toml index 6acc0d02..7480f90a 100644 --- a/axum/with-state/Cargo.toml +++ b/axum/with-state/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -axum = "0.6.10" +axum = "0.7.3" shuttle-axum = "0.35.0" shuttle-runtime = "0.35.0" tokio = "1.26.0" diff --git a/custom-resource/pdo/Cargo.toml b/custom-resource/pdo/Cargo.toml index 901c8968..5b85f730 100644 --- a/custom-resource/pdo/Cargo.toml +++ b/custom-resource/pdo/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [dependencies] async-trait = "0.1.56" -axum = "0.6.18" +axum = "0.7.3" serde = { version = "1.0.148", default-features = false, features = ["derive"] } shuttle-service = "0.35.0" shuttle-axum = "0.35.0" diff --git a/custom-service/request-scheduler/Cargo.toml b/custom-service/request-scheduler/Cargo.toml index b215c9cc..82a9fdce 100644 --- a/custom-service/request-scheduler/Cargo.toml +++ b/custom-service/request-scheduler/Cargo.toml @@ -4,10 +4,8 @@ version = "0.1.0" edition = "2021" publish = false -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -axum = "0.6.18" +axum = "0.7.3" chrono = "0.4.24" cron = "0.12.0" reqwest = "0.11.17" diff --git a/custom-service/request-scheduler/src/lib.rs b/custom-service/request-scheduler/src/lib.rs index ff938ca8..172c4b98 100644 --- a/custom-service/request-scheduler/src/lib.rs +++ b/custom-service/request-scheduler/src/lib.rs @@ -153,10 +153,17 @@ impl shuttle_runtime::Service for CrontabService { let router = self.router; let mut runner = self.runner; - let server = axum::Server::bind(&addr); - - let (_runner_hdl, _axum_hdl) = - tokio::join!(runner.run_jobs(), server.serve(router.into_make_service())); + let server = async move { + axum::serve( + shuttle_runtime::tokio::net::TcpListener::bind(addr) + .await + .unwrap(), + router, + ) + .await + }; + + let (_runner_hdl, _axum_hdl) = tokio::join!(runner.run_jobs(), server); Ok(()) } diff --git a/fullstack-templates/saas/backend/Cargo.toml b/fullstack-templates/saas/backend/Cargo.toml index 44028cee..a273e7df 100644 --- a/fullstack-templates/saas/backend/Cargo.toml +++ b/fullstack-templates/saas/backend/Cargo.toml @@ -7,11 +7,11 @@ publish = false [dependencies] async-stripe = { version = "0.21.0", features = ["runtime-tokio-hyper"] } -axum = "0.6.15" -axum-extra = { version = "0.7.3", features = ["cookie-private"] } -axum-macros = "0.3.7" +axum = "0.7.3" +axum-extra = { version = "0.9.1", features = ["cookie-private"] } +axum-macros = "0.4.0" bcrypt = "0.14.0" -http = "0.2.9" +http = "1.0.0" lettre = "0.10.4" rand = "0.8.5" reqwest = "0.11.16" @@ -24,4 +24,4 @@ sqlx = { version = "0.7.1", features = ["runtime-tokio-native-tls", "postgres", time = { version = "0.3.20", features = ["serde"] } tokio = "1.27.0" tower = "0.4.13" -tower-http = { version = "0.4.0", features = ["cors", "fs"] } +tower-http = { version = "0.5.0", features = ["cors", "fs"] } diff --git a/fullstack-templates/saas/backend/src/auth.rs b/fullstack-templates/saas/backend/src/auth.rs index db3dd674..fe105789 100644 --- a/fullstack-templates/saas/backend/src/auth.rs +++ b/fullstack-templates/saas/backend/src/auth.rs @@ -1,7 +1,7 @@ -use axum::middleware::Next; use axum::{ - extract::State, - http::{Request, StatusCode}, + extract::{Request, State}, + http::StatusCode, + middleware::Next, response::{IntoResponse, Response}, Json, }; @@ -73,13 +73,12 @@ pub async fn login( .await .expect("Couldn't insert session :("); - let cookie = Cookie::build("foo", session_id) + let cookie = Cookie::build(("foo", session_id)) .secure(true) .same_site(SameSite::Strict) .http_only(true) .path("/") - .max_age(Duration::WEEK) - .finish(); + .max_age(Duration::WEEK); Ok((jar.add(cookie), StatusCode::OK)) } @@ -101,16 +100,16 @@ pub async fn logout( .execute(&state.postgres); match query.await { - Ok(_) => Ok(jar.remove(Cookie::named("foo"))), + Ok(_) => Ok(jar.remove(Cookie::build("foo"))), Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR), } } -pub async fn validate_session( +pub async fn validate_session( jar: PrivateCookieJar, State(state): State, - request: Request, - next: Next, + request: Request, + next: Next, ) -> (PrivateCookieJar, Response) { let Some(cookie) = jar.get("foo").map(|cookie| cookie.value().to_owned()) else { println!("Couldn't find a cookie in the jar"); diff --git a/fullstack-templates/saas/backend/src/main.rs b/fullstack-templates/saas/backend/src/main.rs index 030ec99e..36aeb7ce 100644 --- a/fullstack-templates/saas/backend/src/main.rs +++ b/fullstack-templates/saas/backend/src/main.rs @@ -1,11 +1,7 @@ -use axum::body::{boxed, Body}; use axum::extract::FromRef; -use axum::http::{Response, StatusCode}; -use axum::routing::get; use axum::Router; use axum_extra::extract::cookie::Key; use sqlx::PgPool; -use tower::ServiceExt; use tower_http::services::ServeDir; mod auth; @@ -61,15 +57,7 @@ async fn axum( let router = Router::new() .nest("/api", api_router) - .fallback_service(get(|req| async move { - match ServeDir::new("public").oneshot(req).await { - Ok(res) => res.map(boxed), - Err(err) => Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(boxed(Body::from(format!("error: {err}")))) - .expect("error response"), - } - })); + .nest_service("/", ServeDir::new("public")); Ok(router.into()) } diff --git a/other/standalone-binary/Cargo.toml b/other/standalone-binary/Cargo.toml index 9dd5874a..786e605f 100644 --- a/other/standalone-binary/Cargo.toml +++ b/other/standalone-binary/Cargo.toml @@ -13,7 +13,7 @@ name = "standalone" path = "src/bin/standalone.rs" [dependencies] -axum = "0.6.18" +axum = "0.7.3" dotenvy = "0.15.7" shuttle-axum = "0.35.0" shuttle-runtime = "0.35.0" diff --git a/other/standalone-binary/src/bin/standalone.rs b/other/standalone-binary/src/bin/standalone.rs index d0d58641..62c35308 100644 --- a/other/standalone-binary/src/bin/standalone.rs +++ b/other/standalone-binary/src/bin/standalone.rs @@ -10,10 +10,10 @@ async fn main() -> Result<(), Box> { let router = build_router(my_secret); // Do the serving on its own - axum::Server::bind(&"127.0.0.1:8000".parse().unwrap()) - .serve(router.into_make_service()) + let listener = tokio::net::TcpListener::bind("127.0.0.1:8000") .await .unwrap(); + axum::serve(listener, router).await.unwrap(); Ok(()) }