Minor formatting changes.

This commit is contained in:
Syfaro 2021-02-17 12:28:58 -05:00
parent 3ade5aeba9
commit 06a1c7b466
6 changed files with 65 additions and 67 deletions

View File

@ -61,7 +61,7 @@ pub fn search_image_by_url(
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { ) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("url") warp::path("url")
.and(warp::get()) .and(warp::get())
.and(warp::query::<URLSearchOpts>()) .and(warp::query::<UrlSearchOpts>())
.and(with_pool(db)) .and(with_pool(db))
.and(with_tree(tree)) .and(with_tree(tree))
.and(with_api_key()) .and(with_api_key())

View File

@ -8,7 +8,7 @@ use warp::{Rejection, Reply};
#[derive(Debug)] #[derive(Debug)]
enum Error { enum Error {
BB8(bb8::RunError<tokio_postgres::Error>), Bb8(bb8::RunError<tokio_postgres::Error>),
Postgres(tokio_postgres::Error), Postgres(tokio_postgres::Error),
Reqwest(reqwest::Error), Reqwest(reqwest::Error),
InvalidData, InvalidData,
@ -20,7 +20,7 @@ enum Error {
impl warp::Reply for Error { impl warp::Reply for Error {
fn into_response(self) -> warp::reply::Response { fn into_response(self) -> warp::reply::Response {
let msg = match self { let msg = match self {
Error::BB8(_) | Error::Postgres(_) | Error::Reqwest(_) => ErrorMessage { Error::Bb8(_) | Error::Postgres(_) | Error::Reqwest(_) => ErrorMessage {
code: 500, code: 500,
message: "Internal server error".to_string(), message: "Internal server error".to_string(),
}, },
@ -53,7 +53,7 @@ impl warp::Reply for Error {
impl From<bb8::RunError<tokio_postgres::Error>> for Error { impl From<bb8::RunError<tokio_postgres::Error>> for Error {
fn from(err: bb8::RunError<tokio_postgres::Error>) -> Self { fn from(err: bb8::RunError<tokio_postgres::Error>) -> Self {
Error::BB8(err) Error::Bb8(err)
} }
} }
@ -369,7 +369,7 @@ pub async fn check_handle(opts: HandleOpts, db: Pool) -> Result<Box<dyn Reply>,
} }
pub async fn search_image_by_url( pub async fn search_image_by_url(
opts: URLSearchOpts, opts: UrlSearchOpts,
pool: Pool, pool: Pool,
tree: Tree, tree: Tree,
api_key: String, api_key: String,

View File

@ -3,6 +3,7 @@
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use warp::Filter;
mod filters; mod filters;
mod handlers; mod handlers;
@ -10,60 +11,8 @@ mod models;
mod types; mod types;
mod utils; mod utils;
use warp::Filter; type Tree = Arc<RwLock<bk_tree::BKTree<Node, Hamming>>>;
type Pool = bb8::Pool<bb8_postgres::PostgresConnectionManager<tokio_postgres::NoTls>>;
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();
}
#[derive(Debug)] #[derive(Debug)]
pub struct Node { pub struct Node {
@ -77,8 +26,6 @@ impl Node {
} }
} }
type Tree = Arc<RwLock<bk_tree::BKTree<Node, Hamming>>>;
pub struct Hamming; pub struct Hamming;
impl bk_tree::Metric<Node> for Hamming { impl bk_tree::Metric<Node> for Hamming {
@ -198,7 +145,58 @@ async fn main() {
warp::serve(routes).run(([0, 0, 0, 0], 8080)).await; warp::serve(routes).run(([0, 0, 0, 0], 8080)).await;
} }
type Pool = bb8::Pool<bb8_postgres::PostgresConnectionManager<tokio_postgres::NoTls>>; 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]> { fn get_hasher() -> img_hash::Hasher<[u8; 8]> {
use img_hash::{HashAlg::Gradient, HasherConfig}; use img_hash::{HashAlg::Gradient, HasherConfig};

View File

@ -3,11 +3,11 @@ use crate::utils::extract_rows;
use crate::{Pool, Tree}; use crate::{Pool, Tree};
use tracing_futures::Instrument; use tracing_futures::Instrument;
pub type DB<'a> = pub type Db<'a> =
&'a bb8::PooledConnection<'a, bb8_postgres::PostgresConnectionManager<tokio_postgres::NoTls>>; &'a bb8::PooledConnection<'a, bb8_postgres::PostgresConnectionManager<tokio_postgres::NoTls>>;
#[tracing::instrument(skip(db))] #[tracing::instrument(skip(db))]
pub async fn lookup_api_key(key: &str, db: DB<'_>) -> Option<ApiKey> { pub async fn lookup_api_key(key: &str, db: Db<'_>) -> Option<ApiKey> {
let rows = db let rows = db
.query( .query(
"SELECT "SELECT

View File

@ -114,6 +114,6 @@ pub struct HandleOpts {
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct URLSearchOpts { pub struct UrlSearchOpts {
pub url: String, pub url: String,
} }

View File

@ -1,4 +1,4 @@
use crate::models::DB; use crate::models::Db;
use crate::types::*; use crate::types::*;
#[macro_export] #[macro_export]
@ -51,7 +51,7 @@ macro_rules! early_return {
/// joined requests. /// joined requests.
#[tracing::instrument(skip(db))] #[tracing::instrument(skip(db))]
pub async fn update_rate_limit( pub async fn update_rate_limit(
db: DB<'_>, db: Db<'_>,
key_id: i32, key_id: i32,
key_group_limit: i16, key_group_limit: i16,
group_name: &'static str, group_name: &'static str,