From cd2079303de4e665b52c23406bf77c822a444682 Mon Sep 17 00:00:00 2001 From: brianheineman Date: Tue, 13 Aug 2024 11:52:04 -0600 Subject: [PATCH] docs: split axum and progress bar examples --- Cargo.lock | 16 +++++++- examples/axum_embedded/Cargo.toml | 2 - examples/axum_embedded/src/main.rs | 13 +----- examples/download_progress_bar/Cargo.toml | 16 ++++++++ examples/download_progress_bar/src/main.rs | 48 ++++++++++++++++++++++ 5 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 examples/download_progress_bar/Cargo.toml create mode 100644 examples/download_progress_bar/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 0ddbdd2..da56139 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -235,13 +235,11 @@ version = "0.16.1" dependencies = [ "anyhow", "axum", - "indicatif", "postgresql_embedded", "postgresql_extensions", "sqlx", "tokio", "tracing", - "tracing-indicatif", "tracing-subscriber", ] @@ -639,6 +637,20 @@ version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +[[package]] +name = "download_progress_bar" +version = "0.16.1" +dependencies = [ + "anyhow", + "indicatif", + "postgresql_embedded", + "postgresql_extensions", + "tokio", + "tracing", + "tracing-indicatif", + "tracing-subscriber", +] + [[package]] name = "either" version = "1.13.0" diff --git a/examples/axum_embedded/Cargo.toml b/examples/axum_embedded/Cargo.toml index cc18a48..eaed6c4 100644 --- a/examples/axum_embedded/Cargo.toml +++ b/examples/axum_embedded/Cargo.toml @@ -8,11 +8,9 @@ version.workspace = true [dependencies] anyhow = { workspace = true } axum = { workspace = true } -indicatif = { workspace = true } postgresql_embedded = { path = "../../postgresql_embedded" } postgresql_extensions = { path = "../../postgresql_extensions" } sqlx = { workspace = true, features = ["runtime-tokio"] } tracing = { workspace = true } -tracing-indicatif = { workspace = true } tracing-subscriber = { workspace = true } tokio = { workspace = true, features = ["full"] } diff --git a/examples/axum_embedded/src/main.rs b/examples/axum_embedded/src/main.rs index ce17632..f2e7243 100644 --- a/examples/axum_embedded/src/main.rs +++ b/examples/axum_embedded/src/main.rs @@ -4,7 +4,6 @@ use anyhow::Result; use axum::extract::State; use axum::{http::StatusCode, routing::get, Json, Router}; -use indicatif::ProgressStyle; use postgresql_embedded::{PostgreSQL, Settings, VersionReq}; use sqlx::postgres::PgPoolOptions; use sqlx::PgPool; @@ -12,21 +11,11 @@ use std::env; use std::time::Duration; use tokio::net::TcpListener; use tracing::info; -use tracing_indicatif::IndicatifLayer; -use tracing_subscriber::filter::LevelFilter; -use tracing_subscriber::prelude::*; -use tracing_subscriber::{fmt, Registry}; /// Example of how to use postgresql embedded with axum. #[tokio::main] async fn main() -> Result<()> { - let progress_style = ProgressStyle::with_template("{span_child_prefix}{spinner} {span_name} [{elapsed_precise}] [{wide_bar:.green.bold}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")? - .progress_chars("=> "); - let indicatif_layer = IndicatifLayer::new().with_progress_style(progress_style); - let subscriber = Registry::default() - .with(fmt::Layer::default().with_filter(LevelFilter::INFO)) - .with(indicatif_layer); - subscriber.init(); + tracing_subscriber::fmt().compact().init(); let db_url = env::var("DATABASE_URL").unwrap_or_else(|_| "postgresql://postgres@localhost".to_string()); diff --git a/examples/download_progress_bar/Cargo.toml b/examples/download_progress_bar/Cargo.toml new file mode 100644 index 0000000..5c95810 --- /dev/null +++ b/examples/download_progress_bar/Cargo.toml @@ -0,0 +1,16 @@ +[package] +edition.workspace = true +name = "download_progress_bar" +publish = false +license.workspace = true +version.workspace = true + +[dependencies] +anyhow = { workspace = true } +indicatif = { workspace = true } +postgresql_embedded = { path = "../../postgresql_embedded" } +postgresql_extensions = { path = "../../postgresql_extensions" } +tracing = { workspace = true } +tracing-indicatif = { workspace = true } +tracing-subscriber = { workspace = true } +tokio = { workspace = true, features = ["full"] } diff --git a/examples/download_progress_bar/src/main.rs b/examples/download_progress_bar/src/main.rs new file mode 100644 index 0000000..b4dab24 --- /dev/null +++ b/examples/download_progress_bar/src/main.rs @@ -0,0 +1,48 @@ +#![forbid(unsafe_code)] +#![deny(clippy::pedantic)] + +use anyhow::Result; +use indicatif::ProgressStyle; +use postgresql_embedded::{PostgreSQL, Settings, VersionReq}; +use tracing_indicatif::IndicatifLayer; +use tracing_subscriber::filter::LevelFilter; +use tracing_subscriber::prelude::*; +use tracing_subscriber::{fmt, Registry}; + +/// Example of how to display a progress bar for the postgresql embedded archive download +#[tokio::main] +async fn main() -> Result<()> { + let progress_style = ProgressStyle::with_template("{span_child_prefix}{spinner} {span_name} [{elapsed_precise}] [{wide_bar:.green.bold}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")? + .progress_chars("=> "); + let indicatif_layer = IndicatifLayer::new().with_progress_style(progress_style); + let subscriber = Registry::default() + .with(fmt::Layer::default().with_filter(LevelFilter::INFO)) + .with(indicatif_layer); + subscriber.init(); + + let settings = Settings { + version: VersionReq::parse("=16.4.0")?, + ..Default::default() + }; + let mut postgresql = PostgreSQL::new(settings); + postgresql.setup().await?; + postgresql.start().await?; + + let database_name = "test"; + postgresql.create_database(database_name).await?; + postgresql.database_exists(database_name).await?; + postgresql.drop_database(database_name).await?; + + postgresql.stop().await?; + Ok(()) +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_main() -> Result<()> { + main() + } +}