Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: split axum and progress bar examples #126

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions examples/axum_embedded/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
13 changes: 1 addition & 12 deletions examples/axum_embedded/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@
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;
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();

Check warning on line 18 in examples/axum_embedded/src/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/axum_embedded/src/main.rs#L18

Added line #L18 was not covered by tests

let db_url =
env::var("DATABASE_URL").unwrap_or_else(|_| "postgresql://postgres@localhost".to_string());
Expand Down
16 changes: 16 additions & 0 deletions examples/download_progress_bar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
48 changes: 48 additions & 0 deletions examples/download_progress_bar/src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
Loading