From 06a1c7b46632e3bf7144c21e5241b53b3d75d8cf Mon Sep 17 00:00:00 2001 From: Syfaro Date: Wed, 17 Feb 2021 12:28:58 -0500 Subject: [PATCH] Minor formatting changes. --- src/filters.rs | 2 +- src/handlers.rs | 8 ++-- src/main.rs | 112 ++++++++++++++++++++++++------------------------ src/models.rs | 4 +- src/types.rs | 2 +- src/utils.rs | 4 +- 6 files changed, 65 insertions(+), 67 deletions(-) diff --git a/src/filters.rs b/src/filters.rs index 4cfb4a0..4d734ef 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -61,7 +61,7 @@ pub fn search_image_by_url( ) -> impl Filter + Clone { warp::path("url") .and(warp::get()) - .and(warp::query::()) + .and(warp::query::()) .and(with_pool(db)) .and(with_tree(tree)) .and(with_api_key()) diff --git a/src/handlers.rs b/src/handlers.rs index d45e73a..f326a51 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -8,7 +8,7 @@ use warp::{Rejection, Reply}; #[derive(Debug)] enum Error { - BB8(bb8::RunError), + Bb8(bb8::RunError), Postgres(tokio_postgres::Error), Reqwest(reqwest::Error), InvalidData, @@ -20,7 +20,7 @@ enum Error { impl warp::Reply for Error { fn into_response(self) -> warp::reply::Response { let msg = match self { - Error::BB8(_) | Error::Postgres(_) | Error::Reqwest(_) => ErrorMessage { + Error::Bb8(_) | Error::Postgres(_) | Error::Reqwest(_) => ErrorMessage { code: 500, message: "Internal server error".to_string(), }, @@ -53,7 +53,7 @@ impl warp::Reply for Error { impl From> for Error { fn from(err: bb8::RunError) -> Self { - Error::BB8(err) + Error::Bb8(err) } } @@ -369,7 +369,7 @@ pub async fn check_handle(opts: HandleOpts, db: Pool) -> Result, } pub async fn search_image_by_url( - opts: URLSearchOpts, + opts: UrlSearchOpts, pool: Pool, tree: Tree, api_key: String, diff --git a/src/main.rs b/src/main.rs index 6ccbf30..a774b13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use std::sync::Arc; use tokio::sync::RwLock; +use warp::Filter; mod filters; mod handlers; @@ -10,60 +11,8 @@ mod models; mod types; mod utils; -use warp::Filter; - -fn configure_tracing() { - use opentelemetry::{ - api::{KeyValue, Provider}, - sdk::{Config, Sampler}, - }; - use tracing_subscriber::{layer::SubscriberExt, prelude::*}; - - let env = if cfg!(debug_assertions) { - "debug" - } else { - "release" - }; - - let fmt_layer = tracing_subscriber::fmt::layer(); - let filter_layer = tracing_subscriber::EnvFilter::try_from_default_env() - .or_else(|_| tracing_subscriber::EnvFilter::try_new("info")) - .unwrap(); - tracing_subscriber::fmt() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .finish(); - let registry = tracing_subscriber::registry() - .with(filter_layer) - .with(fmt_layer); - - let exporter = opentelemetry_jaeger::Exporter::builder() - .with_agent_endpoint(std::env::var("JAEGER_COLLECTOR").unwrap().parse().unwrap()) - .with_process(opentelemetry_jaeger::Process { - service_name: "fuzzysearch".to_string(), - tags: vec![ - KeyValue::new("environment", env), - KeyValue::new("version", env!("CARGO_PKG_VERSION")), - ], - }) - .init() - .expect("unable to create jaeger exporter"); - - let provider = opentelemetry::sdk::Provider::builder() - .with_simple_exporter(exporter) - .with_config(Config { - default_sampler: Box::new(Sampler::Always), - ..Default::default() - }) - .build(); - - opentelemetry::global::set_provider(provider); - - let tracer = opentelemetry::global::trace_provider().get_tracer("fuzzysearch"); - let telem_layer = tracing_opentelemetry::layer().with_tracer(tracer); - let registry = registry.with(telem_layer); - - registry.init(); -} +type Tree = Arc>>; +type Pool = bb8::Pool>; #[derive(Debug)] pub struct Node { @@ -77,8 +26,6 @@ impl Node { } } -type Tree = Arc>>; - pub struct Hamming; impl bk_tree::Metric for Hamming { @@ -198,7 +145,58 @@ async fn main() { warp::serve(routes).run(([0, 0, 0, 0], 8080)).await; } -type Pool = bb8::Pool>; +fn configure_tracing() { + use opentelemetry::{ + api::{KeyValue, Provider}, + sdk::{Config, Sampler}, + }; + use tracing_subscriber::{layer::SubscriberExt, prelude::*}; + + let env = if cfg!(debug_assertions) { + "debug" + } else { + "release" + }; + + let fmt_layer = tracing_subscriber::fmt::layer(); + let filter_layer = tracing_subscriber::EnvFilter::try_from_default_env() + .or_else(|_| tracing_subscriber::EnvFilter::try_new("info")) + .unwrap(); + tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .finish(); + let registry = tracing_subscriber::registry() + .with(filter_layer) + .with(fmt_layer); + + let exporter = opentelemetry_jaeger::Exporter::builder() + .with_agent_endpoint(std::env::var("JAEGER_COLLECTOR").unwrap().parse().unwrap()) + .with_process(opentelemetry_jaeger::Process { + service_name: "fuzzysearch".to_string(), + tags: vec![ + KeyValue::new("environment", env), + KeyValue::new("version", env!("CARGO_PKG_VERSION")), + ], + }) + .init() + .expect("unable to create jaeger exporter"); + + let provider = opentelemetry::sdk::Provider::builder() + .with_simple_exporter(exporter) + .with_config(Config { + default_sampler: Box::new(Sampler::Always), + ..Default::default() + }) + .build(); + + opentelemetry::global::set_provider(provider); + + let tracer = opentelemetry::global::trace_provider().get_tracer("fuzzysearch"); + let telem_layer = tracing_opentelemetry::layer().with_tracer(tracer); + let registry = registry.with(telem_layer); + + registry.init(); +} fn get_hasher() -> img_hash::Hasher<[u8; 8]> { use img_hash::{HashAlg::Gradient, HasherConfig}; diff --git a/src/models.rs b/src/models.rs index 2bd4fb0..7fc7a58 100644 --- a/src/models.rs +++ b/src/models.rs @@ -3,11 +3,11 @@ use crate::utils::extract_rows; use crate::{Pool, Tree}; use tracing_futures::Instrument; -pub type DB<'a> = +pub type Db<'a> = &'a bb8::PooledConnection<'a, bb8_postgres::PostgresConnectionManager>; #[tracing::instrument(skip(db))] -pub async fn lookup_api_key(key: &str, db: DB<'_>) -> Option { +pub async fn lookup_api_key(key: &str, db: Db<'_>) -> Option { let rows = db .query( "SELECT diff --git a/src/types.rs b/src/types.rs index a4b76c8..55d6187 100644 --- a/src/types.rs +++ b/src/types.rs @@ -114,6 +114,6 @@ pub struct HandleOpts { } #[derive(Debug, Deserialize)] -pub struct URLSearchOpts { +pub struct UrlSearchOpts { pub url: String, } diff --git a/src/utils.rs b/src/utils.rs index 404483d..d0e940e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use crate::models::DB; +use crate::models::Db; use crate::types::*; #[macro_export] @@ -51,7 +51,7 @@ macro_rules! early_return { /// joined requests. #[tracing::instrument(skip(db))] pub async fn update_rate_limit( - db: DB<'_>, + db: Db<'_>, key_id: i32, key_group_limit: i16, group_name: &'static str,