Skip to content

Commit

Permalink
chore: update axum examples to axum 0.7 (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaro00 authored Jan 8, 2024
1 parent e3bec67 commit 9439967
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 70 deletions.
2 changes: 1 addition & 1 deletion axum/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion axum/jwt-authentication/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
7 changes: 5 additions & 2 deletions axum/jwt-authentication/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion axum/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion axum/postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions axum/static-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
4 changes: 2 additions & 2 deletions axum/static-next-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
10 changes: 5 additions & 5 deletions axum/static-next-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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())
}
10 changes: 5 additions & 5 deletions axum/turso/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 3 additions & 4 deletions axum/websocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
7 changes: 1 addition & 6 deletions axum/websocket/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion axum/with-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion custom-resource/pdo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 1 addition & 3 deletions custom-service/request-scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 11 additions & 4 deletions custom-service/request-scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
10 changes: 5 additions & 5 deletions fullstack-templates/saas/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"] }
19 changes: 9 additions & 10 deletions fullstack-templates/saas/backend/src/auth.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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))
}
Expand All @@ -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<B>(
pub async fn validate_session(
jar: PrivateCookieJar,
State(state): State<AppState>,
request: Request<B>,
next: Next<B>,
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");
Expand Down
14 changes: 1 addition & 13 deletions fullstack-templates/saas/backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion other/standalone-binary/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions other/standalone-binary/src/bin/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}

0 comments on commit 9439967

Please sign in to comment.