Compare commits

..

2 commits

Author SHA1 Message Date
Rob Ede
d4d0db2e1e
refactor: migrate web server to axum (lite) 2024-05-27 03:52:53 +01:00
Rob Ede
85a077e80d
refactor: migrate to tracing for logging 2024-05-26 23:22:19 +01:00
21 changed files with 96 additions and 78 deletions

3
.rustfmt.toml Normal file
View file

@ -0,0 +1,3 @@
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
use_field_init_shorthand = true

8
Cargo.lock generated
View file

@ -497,6 +497,12 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "error_reporter"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31ae425815400e5ed474178a7a22e275a9687086a12ca63ec793ff292d8fdae8"
[[package]] [[package]]
name = "faster-hex" name = "faster-hex"
version = "0.9.0" version = "0.9.0"
@ -2628,6 +2634,7 @@ dependencies = [
"crates-index", "crates-index",
"derive_more", "derive_more",
"dotenvy", "dotenvy",
"error_reporter",
"font-awesome-as-a-crate", "font-awesome-as-a-crate",
"futures-util", "futures-util",
"gix", "gix",
@ -2636,6 +2643,7 @@ dependencies = [
"lru_time_cache", "lru_time_cache",
"maud", "maud",
"once_cell", "once_cell",
"parking_lot",
"pulldown-cmark", "pulldown-cmark",
"relative-path", "relative-path",
"reqwest", "reqwest",

View file

@ -22,10 +22,12 @@ derive_more = "0.99"
dotenvy = "0.15" dotenvy = "0.15"
font-awesome-as-a-crate = "0.3" font-awesome-as-a-crate = "0.3"
futures-util = { version = "0.3", default-features = false, features = ["std"] } futures-util = { version = "0.3", default-features = false, features = ["std"] }
error_reporter = "1"
indexmap = { version = "2", features = ["serde"] } indexmap = { version = "2", features = ["serde"] }
lru_time_cache = "0.11" lru_time_cache = "0.11"
maud = "0.26" maud = "0.26"
once_cell = "1" once_cell = "1"
parking_lot = "0.12"
pulldown-cmark = "0.11" pulldown-cmark = "0.11"
relative-path = { version = "1", features = ["serde"] } relative-path = { version = "1", features = ["serde"] }
reqwest = { version = "0.12", features = ["json"] } reqwest = { version = "0.12", features = ["json"] }

View file

@ -1,6 +1,4 @@
use std::env; use std::{env, fs, path::Path};
use std::fs;
use std::path::Path;
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};

View file

@ -235,8 +235,8 @@ mod tests {
#[test] #[test]
#[ignore] #[ignore]
fn test_to_svg() { fn test_to_svg() {
use std::fs::File; use std::{fs::File, io::Write as _};
use std::io::Write;
let mut file = File::create("test.svg").unwrap(); let mut file = File::create("test.svg").unwrap();
let options = BadgeOptions { let options = BadgeOptions {
subject: "latest".to_owned(), subject: "latest".to_owned(),

View file

@ -2,11 +2,12 @@ use anyhow::Error;
use futures_util::{future::BoxFuture, stream::FuturesOrdered, FutureExt as _, StreamExt as _}; use futures_util::{future::BoxFuture, stream::FuturesOrdered, FutureExt as _, StreamExt as _};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::models::repo::RepoPath; use crate::{
engine::{
use crate::engine::{
machines::crawler::{ManifestCrawler, ManifestCrawlerOutput}, machines::crawler::{ManifestCrawler, ManifestCrawlerOutput},
Engine, Engine,
},
models::repo::RepoPath,
}; };
pub async fn crawl_manifest( pub async fn crawl_manifest(

View file

@ -1,5 +1,4 @@
mod analyze; mod analyze;
mod crawl; mod crawl;
pub use self::analyze::analyze_dependencies; pub use self::{analyze::analyze_dependencies, crawl::crawl_manifest};
pub use self::crawl::crawl_manifest;

View file

@ -101,9 +101,8 @@ impl DependencyAnalyzer {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::models::crates::{CrateDep, CrateDeps, CrateRelease};
use super::*; use super::*;
use crate::models::crates::{CrateDep, CrateDeps, CrateRelease};
#[test] #[test]
fn tracks_latest_without_matching() { fn tracks_latest_without_matching() {

View file

@ -4,8 +4,10 @@ use anyhow::Error;
use indexmap::IndexMap; use indexmap::IndexMap;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::models::crates::{CrateDep, CrateDeps, CrateManifest, CrateName}; use crate::{
use crate::parsers::manifest::parse_manifest_toml; models::crates::{CrateDep, CrateDeps, CrateManifest, CrateName},
parsers::manifest::parse_manifest_toml,
};
pub struct ManifestCrawlerOutput { pub struct ManifestCrawlerOutput {
pub crates: IndexMap<CrateName, CrateDeps>, pub crates: IndexMap<CrateName, CrateDeps>,
@ -118,9 +120,8 @@ mod tests {
use relative_path::RelativePath; use relative_path::RelativePath;
use semver::VersionReq; use semver::VersionReq;
use crate::models::crates::CrateDep;
use super::*; use super::*;
use crate::models::crates::CrateDep;
#[test] #[test]
fn simple_package_manifest() { fn simple_package_manifest() {

View file

@ -7,7 +7,6 @@ use std::{
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use cadence::{MetricSink, NopMetricSink, StatsdClient}; use cadence::{MetricSink, NopMetricSink, StatsdClient};
use futures_util::{ use futures_util::{
future::try_join_all, future::try_join_all,
stream::{self, BoxStream}, stream::{self, BoxStream},
@ -19,14 +18,20 @@ use rustsec::database::Database;
use semver::VersionReq; use semver::VersionReq;
use tower::Service; use tower::Service;
use crate::interactors::crates::{GetPopularCrates, QueryCrate}; use crate::{
use crate::interactors::github::GetPopularRepos; interactors::{
use crate::interactors::rustsec::FetchAdvisoryDatabase; crates::{GetPopularCrates, QueryCrate},
use crate::interactors::RetrieveFileAtPath; github::GetPopularRepos,
use crate::models::crates::{AnalyzedDependencies, CrateName, CratePath, CrateRelease}; rustsec::FetchAdvisoryDatabase,
use crate::models::repo::{RepoPath, Repository}; RetrieveFileAtPath,
use crate::utils::cache::Cache; },
use crate::ManagedIndex; models::{
crates::{AnalyzedDependencies, CrateName, CratePath, CrateRelease},
repo::{RepoPath, Repository},
},
utils::cache::Cache,
ManagedIndex,
};
mod fut; mod fut;
mod machines; mod machines;

View file

@ -1,13 +1,15 @@
use std::{fmt, str, task::Context, task::Poll}; use std::{
fmt, str,
task::{Context, Poll},
};
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use crates_index::{Crate, DependencyKind}; use crates_index::{Crate, DependencyKind};
use futures_util::FutureExt as _; use futures_util::FutureExt as _;
use semver::{Version, VersionReq}; use semver::{Version, VersionReq};
use serde::Deserialize; use serde::Deserialize;
use tower::Service;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
use tower::Service;
use crate::{ use crate::{
models::crates::{CrateDep, CrateDeps, CrateName, CratePath, CrateRelease}, models::crates::{CrateDep, CrateDeps, CrateName, CratePath, CrateRelease},

View file

@ -4,7 +4,6 @@ use std::{
}; };
use anyhow::Error; use anyhow::Error;
use futures_util::FutureExt as _; use futures_util::FutureExt as _;
use serde::Deserialize; use serde::Deserialize;
use tower::Service; use tower::Service;

View file

@ -1,4 +1,8 @@
use std::{fmt, sync::Arc, task::Context, task::Poll}; use std::{
fmt,
sync::Arc,
task::{Context, Poll},
};
use anyhow::Error; use anyhow::Error;
use futures_util::FutureExt as _; use futures_util::FutureExt as _;

View file

@ -21,9 +21,7 @@ mod parsers;
mod server; mod server;
mod utils; mod utils;
use self::engine::Engine; use self::{engine::Engine, server::App, utils::index::ManagedIndex};
use self::server::App;
use self::utils::index::ManagedIndex;
/// Future crate's BoxFuture without the explicit lifetime parameter. /// Future crate's BoxFuture without the explicit lifetime parameter.
pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>; pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;

View file

@ -135,9 +135,8 @@ pub fn parse_manifest_toml(input: &str) -> Result<CrateManifest, Error> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::models::crates::CrateManifest;
use super::*; use super::*;
use crate::models::crates::CrateManifest;
#[test] #[test]
fn parse_workspace_without_members_declaration() { fn parse_workspace_without_members_declaration() {

View file

@ -22,10 +22,14 @@ mod views;
use self::assets::{ use self::assets::{
STATIC_LINKS_JS_ETAG, STATIC_LINKS_JS_PATH, STATIC_STYLE_CSS_ETAG, STATIC_STYLE_CSS_PATH, STATIC_LINKS_JS_ETAG, STATIC_LINKS_JS_PATH, STATIC_STYLE_CSS_ETAG, STATIC_STYLE_CSS_PATH,
}; };
use crate::engine::{AnalyzeDependenciesOutcome, Engine}; use crate::{
use crate::models::crates::{CrateName, CratePath}; engine::{AnalyzeDependenciesOutcome, Engine},
use crate::models::repo::RepoPath; models::{
use crate::models::SubjectPath; crates::{CrateName, CratePath},
repo::RepoPath,
SubjectPath,
},
};
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
enum StatusFormat { enum StatusFormat {

View file

@ -1,10 +1,7 @@
use axum::body::Body; use axum::{body::Body, http::header::CONTENT_TYPE, response::Response};
use axum::http::header::CONTENT_TYPE;
use axum::response::Response;
use badge::{Badge, BadgeOptions}; use badge::{Badge, BadgeOptions};
use crate::engine::AnalyzeDependenciesOutcome; use crate::{engine::AnalyzeDependenciesOutcome, server::ExtraConfig};
use crate::server::ExtraConfig;
pub fn badge( pub fn badge(
analysis_outcome: Option<&AnalyzeDependenciesOutcome>, analysis_outcome: Option<&AnalyzeDependenciesOutcome>,

View file

@ -1,10 +1,10 @@
use axum::{body::Body, response::Response}; use axum::{body::Body, response::Response};
use maud::{html, Markup}; use maud::{html, Markup};
use crate::models::crates::CratePath; use crate::{
use crate::models::repo::Repository; models::{crates::CratePath, repo::Repository},
server::assets::STATIC_LINKS_JS_PATH,
use crate::server::assets::STATIC_LINKS_JS_PATH; };
fn link_forms() -> Markup { fn link_forms() -> Markup {
html! { html! {

View file

@ -1,15 +1,13 @@
use std::time::Duration; use std::time::Duration;
use axum::http::header::CONTENT_TYPE; use axum::{body::Body, http::header::CONTENT_TYPE, response::Response};
use axum::{body::Body, response::Response};
use maud::{html, Markup, Render, DOCTYPE}; use maud::{html, Markup, Render, DOCTYPE};
pub mod error; pub mod error;
pub mod index; pub mod index;
pub mod status; pub mod status;
use crate::server::assets::STATIC_STYLE_CSS_PATH; use crate::server::{assets::STATIC_STYLE_CSS_PATH, SELF_BASE_URL};
use crate::server::SELF_BASE_URL;
fn render_html<B: Render>(title: &str, body: B) -> Response<Body> { fn render_html<B: Render>(title: &str, body: B) -> Response<Body> {
let rendered = html! { let rendered = html! {

View file

@ -6,12 +6,15 @@ use pulldown_cmark::{html, Parser};
use rustsec::advisory::Advisory; use rustsec::advisory::Advisory;
use semver::Version; use semver::Version;
use crate::engine::AnalyzeDependenciesOutcome; use crate::{
use crate::models::crates::{AnalyzedDependencies, AnalyzedDependency, CrateName}; engine::AnalyzeDependenciesOutcome,
use crate::models::repo::RepoSite; models::{
use crate::models::SubjectPath; crates::{AnalyzedDependencies, AnalyzedDependency, CrateName},
use crate::server::views::badge; repo::RepoSite,
use crate::server::ExtraConfig; SubjectPath,
},
server::{views::badge, ExtraConfig},
};
fn get_crates_url(name: impl AsRef<str>) -> String { fn get_crates_url(name: impl AsRef<str>) -> String {
format!("https://crates.io/crates/{}", name.as_ref()) format!("https://crates.io/crates/{}", name.as_ref())

View file

@ -1,13 +1,14 @@
use std::sync::Arc; use std::{sync::Arc, time::Duration};
use std::sync::Mutex;
use std::time::Duration; use anyhow::Result;
use crates_index::{Crate, GitIndex};
use parking_lot::Mutex;
use tokio::{
task::spawn_blocking,
time::{self, MissedTickBehavior},
};
use crate::models::crates::CrateName; use crate::models::crates::CrateName;
use anyhow::Result;
use crates_index::Crate;
use crates_index::GitIndex;
use tokio::task::spawn_blocking;
use tokio::time::{self, MissedTickBehavior};
#[derive(Clone)] #[derive(Clone)]
pub struct ManagedIndex { pub struct ManagedIndex {
@ -23,9 +24,7 @@ impl ManagedIndex {
} }
pub fn crate_(&self, crate_name: CrateName) -> Option<Crate> { pub fn crate_(&self, crate_name: CrateName) -> Option<Crate> {
let index = self.index.lock().unwrap(); self.index.lock().crate_(crate_name.as_ref())
index.crate_(crate_name.as_ref())
} }
pub async fn refresh_at_interval(&self, update_interval: Duration) { pub async fn refresh_at_interval(&self, update_interval: Duration) {
@ -35,22 +34,21 @@ impl ManagedIndex {
loop { loop {
if let Err(err) = self.refresh().await { if let Err(err) = self.refresh().await {
tracing::error!( tracing::error!(
"failed refreshing the crates.io-index, the operation will be retried: {err}" "failed refreshing the crates.io-index, the operation will be retried: {}",
error_reporter::Report::new(err),
); );
} }
update_interval.tick().await; update_interval.tick().await;
} }
} }
async fn refresh(&self) -> Result<()> { async fn refresh(&self) -> Result<(), crates_index::Error> {
let index = Arc::clone(&self.index); let index = Arc::clone(&self.index);
spawn_blocking(move || { spawn_blocking(move || index.lock().update())
let mut index = index.lock().unwrap(); .await
.expect("blocking index update task should never panic")?;
index.update()
})
.await??;
Ok(()) Ok(())
} }
} }