diff --git a/Cargo.lock b/Cargo.lock index 9721623..5dbeebb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,6 +72,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "async-trait" +version = "0.1.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -84,6 +95,61 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "axum" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf" +dependencies = [ + "async-trait", + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper 1.0.1", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper 0.1.2", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "backtrace" version = "0.3.71" @@ -1390,25 +1456,6 @@ dependencies = [ "phf", ] -[[package]] -name = "h2" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http 0.2.12", - "indexmap", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "h2" version = "0.4.5" @@ -1420,7 +1467,7 @@ dependencies = [ "fnv", "futures-core", "futures-sink", - "http 1.1.0", + "http", "indexmap", "slab", "tokio", @@ -1471,17 +1518,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "http" -version = "0.2.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - [[package]] name = "http" version = "1.1.0" @@ -1493,17 +1529,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "http-body" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" -dependencies = [ - "bytes", - "http 0.2.12", - "pin-project-lite", -] - [[package]] name = "http-body" version = "1.0.0" @@ -1511,7 +1536,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http 1.1.0", + "http", ] [[package]] @@ -1522,8 +1547,8 @@ checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" dependencies = [ "bytes", "futures-core", - "http 1.1.0", - "http-body 1.0.0", + "http", + "http-body", "pin-project-lite", ] @@ -1539,30 +1564,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "hyper" -version = "0.14.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" -dependencies = [ - "bytes", - "futures-channel", - "futures-core", - "futures-util", - "h2 0.3.26", - "http 0.2.12", - "http-body 0.4.6", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "socket2", - "tokio", - "tower-service", - "tracing", - "want", -] - [[package]] name = "hyper" version = "1.3.1" @@ -1572,10 +1573,11 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.5", - "http 1.1.0", - "http-body 1.0.0", + "h2", + "http", + "http-body", "httparse", + "httpdate", "itoa", "pin-project-lite", "smallvec", @@ -1590,8 +1592,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c" dependencies = [ "futures-util", - "http 1.1.0", - "hyper 1.3.1", + "http", + "hyper", "hyper-util", "rustls", "rustls-pki-types", @@ -1608,7 +1610,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ "bytes", "http-body-util", - "hyper 1.3.1", + "hyper", "hyper-util", "native-tls", "tokio", @@ -1625,9 +1627,9 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "http 1.1.0", - "http-body 1.0.0", - "hyper 1.3.1", + "http", + "http-body", + "hyper", "pin-project-lite", "socket2", "tokio", @@ -1777,6 +1779,12 @@ version = "0.11.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9106e1d747ffd48e6be5bb2d97fa706ed25b144fbee4d5c02eae110cd8d6badd" +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + [[package]] name = "maud" version = "0.26.0" @@ -2244,11 +2252,11 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.4.5", - "http 1.1.0", - "http-body 1.0.0", + "h2", + "http", + "http-body", "http-body-util", - "hyper 1.3.1", + "hyper", "hyper-rustls", "hyper-tls", "hyper-util", @@ -2267,7 +2275,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "sync_wrapper", + "sync_wrapper 0.1.2", "system-configuration", "tokio", "tokio-native-tls", @@ -2521,6 +2529,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_spanned" version = "0.6.6" @@ -2570,6 +2588,7 @@ name = "shiny-robots" version = "0.1.0" dependencies = [ "anyhow", + "axum", "badge", "cadence", "crates-index", @@ -2578,7 +2597,6 @@ dependencies = [ "futures-util", "gix", "grass", - "hyper 0.14.28", "indexmap", "lru_time_cache", "maud", @@ -2597,6 +2615,7 @@ dependencies = [ "slog-term", "tokio", "toml 0.8.13", + "tower", ] [[package]] @@ -2716,6 +2735,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "sync_wrapper" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" + [[package]] name = "system-configuration" version = "0.5.1" @@ -2753,7 +2778,7 @@ dependencies = [ "crossbeam-channel", "gix", "home", - "http 1.1.0", + "http", "libc", "memchr", "rayon", @@ -3012,6 +3037,7 @@ dependencies = [ "tokio", "tower-layer", "tower-service", + "tracing", ] [[package]] @@ -3032,6 +3058,7 @@ version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ + "log", "pin-project-lite", "tracing-core", ] diff --git a/Cargo.toml b/Cargo.toml index 746daaf..ca0a8c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,12 @@ edition = "2021" badge = { path = "./libs/badge" } anyhow = "1" +axum = "0.7" cadence = "1" crates-index = { version = "2", default-features = false, features = ["git"] } derive_more = "0.99" font-awesome-as-a-crate = "0.3" futures-util = { version = "0.3", default-features = false, features = ["std"] } -hyper = { version = "0.14.10", features = ["full"] } indexmap = { version = "2", features = ["serde"] } lru_time_cache = "0.11" maud = "0.26" @@ -38,6 +38,7 @@ slog-async = "2" slog-term = "2" tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros", "sync", "time"] } toml = "0.8" +tower = "0.4" [target.'cfg(any())'.dependencies] gix = { version = "0.63", default-features = false, features = ["blocking-http-transport-reqwest-rust-tls"] } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 2603ab3..eed921f 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -13,12 +13,12 @@ use futures_util::{ stream::{self, BoxStream}, StreamExt as _, }; -use hyper::service::Service; use once_cell::sync::Lazy; use relative_path::{RelativePath, RelativePathBuf}; use rustsec::database::Database; use semver::VersionReq; use slog::Logger; +use tower::Service; use crate::interactors::crates::{GetPopularCrates, QueryCrate}; use crate::interactors::github::GetPopularRepos; diff --git a/src/interactors/crates.rs b/src/interactors/crates.rs index e490a64..971fbdf 100644 --- a/src/interactors/crates.rs +++ b/src/interactors/crates.rs @@ -3,9 +3,9 @@ use std::{fmt, str, task::Context, task::Poll}; use anyhow::{anyhow, Error}; use crates_index::{Crate, DependencyKind}; use futures_util::FutureExt as _; -use hyper::service::Service; use semver::{Version, VersionReq}; use serde::Deserialize; +use tower::Service; use tokio::task::spawn_blocking; diff --git a/src/interactors/github.rs b/src/interactors/github.rs index 32d5c86..35cd0b2 100644 --- a/src/interactors/github.rs +++ b/src/interactors/github.rs @@ -6,8 +6,8 @@ use std::{ use anyhow::Error; use futures_util::FutureExt as _; -use hyper::service::Service; use serde::Deserialize; +use tower::Service; use crate::{ models::repo::{RepoPath, Repository}, diff --git a/src/interactors/mod.rs b/src/interactors/mod.rs index 030174f..b6291cf 100644 --- a/src/interactors/mod.rs +++ b/src/interactors/mod.rs @@ -5,8 +5,8 @@ use std::{ use anyhow::{anyhow, Error}; use futures_util::FutureExt as _; -use hyper::service::Service; use relative_path::RelativePathBuf; +use tower::Service; use crate::{models::repo::RepoPath, BoxFuture}; diff --git a/src/interactors/rustsec.rs b/src/interactors/rustsec.rs index e28488e..0513179 100644 --- a/src/interactors/rustsec.rs +++ b/src/interactors/rustsec.rs @@ -2,8 +2,8 @@ use std::{fmt, sync::Arc, task::Context, task::Poll}; use anyhow::Error; use futures_util::FutureExt as _; -use hyper::service::Service; use rustsec::database::Database; +use tower::Service; use crate::BoxFuture; diff --git a/src/main.rs b/src/main.rs index c568f0b..d3e3dfa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,13 +9,8 @@ use std::{ time::Duration, }; +use axum::{extract::Request, Router}; use cadence::{QueuingMetricSink, UdpMetricSink}; -use hyper::{ - server::conn::AddrStream, - service::{make_service_fn, service_fn}, - Server, -}; - use reqwest::redirect::Policy as RedirectPolicy; use slog::{error, info, o, Drain, Logger}; @@ -85,23 +80,19 @@ async fn main() { engine.set_metrics(metrics); let svc_logger = logger.new(o!()); - let make_svc = make_service_fn(move |_socket: &AddrStream| { - let engine = engine.clone(); - let logger = svc_logger.clone(); - - async move { - let server = App::new(logger.clone(), engine.clone()); - Ok::<_, hyper::Error>(service_fn(move |req| { - let server = server.clone(); - async move { server.handle(req).await } - })) - } - }); - let server = Server::bind(&addr).serve(make_svc); + + let app = App::new(svc_logger.clone(), engine.clone()); + + let lst = tokio::net::TcpListener::bind(addr).await.unwrap(); + + let router = + Router::new().fallback(|req: Request| async move { app.handle(req).await.unwrap() }); + + let server = axum::serve(lst, router); info!(logger, "Server running on port {}", port); - if let Err(e) = server.await { - error!(logger, "server error: {}", e); + if let Err(err) = server.await { + error!(logger, "server error: {err}"); } } diff --git a/src/server/mod.rs b/src/server/mod.rs index ea54c19..733e54e 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,11 +1,16 @@ use std::{env, sync::Arc, time::Instant}; +use axum::{ + body::Body, + extract::Request, + http::{ + header::{CACHE_CONTROL, CONTENT_TYPE, ETAG, LOCATION}, + Method, StatusCode, + }, + response::Response, +}; use badge::BadgeStyle; use futures_util::future; -use hyper::{ - header::{CACHE_CONTROL, CONTENT_TYPE, ETAG, LOCATION}, - Body, Error as HyperError, Method, Request, Response, StatusCode, -}; use once_cell::sync::Lazy; use route_recognizer::{Params, Router}; use semver::VersionReq; @@ -89,7 +94,7 @@ impl App { } } - pub async fn handle(&self, req: Request
) -> Result