makes the analytics available for all the routes

This commit is contained in:
Tamo 2021-10-12 14:32:44 +02:00 committed by marin postma
parent 664d09e86a
commit d72c887422
No known key found for this signature in database
GPG key ID: 6088B7721C3E39F9
6 changed files with 69 additions and 21 deletions

View file

@ -151,6 +151,7 @@ mod segment {
impl super::Analytics for SegmentAnalytics {
fn publish(&'static self, event_name: String, send: Value) {
tokio::spawn(async move {
println!("ANALYTICS pushing {} in the batcher", event_name);
let _ = self
.batcher
.lock()
@ -162,6 +163,7 @@ mod segment {
..Default::default()
})
.await;
println!("ANALYTICS {} pushed", event_name);
});
}
}
@ -205,6 +207,6 @@ impl Display for MockAnalytics {
}
#[async_trait::async_trait]
pub trait Analytics: Display {
pub trait Analytics: Display + Sync + Send {
fn publish(&'static self, event_name: String, send: Value);
}

View file

@ -12,6 +12,7 @@ use std::time::Duration;
use crate::error::MeilisearchHttpError;
use crate::extractors::authentication::AuthConfig;
use actix_web::error::JsonPayloadError;
use analytics::Analytics;
use error::PayloadError;
use http::header::CONTENT_TYPE;
pub use option::Opt;
@ -73,10 +74,16 @@ pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<MeiliSearch> {
meilisearch.build(opt.db_path.clone(), opt.indexer_options.clone())
}
pub fn configure_data(config: &mut web::ServiceConfig, data: MeiliSearch, opt: &Opt) {
pub fn configure_data(
config: &mut web::ServiceConfig,
data: MeiliSearch,
opt: &Opt,
analytics: &'static dyn Analytics,
) {
let http_payload_size_limit = opt.http_payload_size_limit.get_bytes() as usize;
config
.app_data(data)
.app_data(web::Data::new(analytics))
.app_data(
web::JsonConfig::default()
.content_type(|mime| mime == mime::APPLICATION_JSON)
@ -167,7 +174,7 @@ pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
#[macro_export]
macro_rules! create_app {
($data:expr, $enable_frontend:expr, $opt:expr) => {{
($data:expr, $enable_frontend:expr, $opt:expr, $analytics:expr) => {{
use actix_cors::Cors;
use actix_web::middleware::TrailingSlash;
use actix_web::App;
@ -177,7 +184,7 @@ macro_rules! create_app {
use meilisearch_http::{configure_auth, configure_data, dashboard};
App::new()
.configure(|s| configure_data(s, $data.clone(), &$opt))
.configure(|s| configure_data(s, $data.clone(), &$opt, $analytics))
.configure(|s| configure_auth(s, &$opt))
.configure(routes::configure)
.configure(|s| dashboard(s, $enable_frontend))

View file

@ -56,17 +56,22 @@ async fn main() -> anyhow::Result<()> {
print_launch_resume(&opt, analytics);
run_http(meilisearch, opt).await?;
run_http(meilisearch, opt, analytics).await?;
Ok(())
}
async fn run_http(data: MeiliSearch, opt: Opt) -> anyhow::Result<()> {
async fn run_http(
data: MeiliSearch,
opt: Opt,
analytics: &'static dyn Analytics,
) -> anyhow::Result<()> {
let _enable_dashboard = &opt.env == "development";
let opt_clone = opt.clone();
let http_server = HttpServer::new(move || create_app!(data, _enable_dashboard, opt_clone))
// Disable signals allows the server to terminate immediately when a user enter CTRL-C
.disable_signals();
let http_server =
HttpServer::new(move || create_app!(data, _enable_dashboard, opt_clone, analytics))
// Disable signals allows the server to terminate immediately when a user enter CTRL-C
.disable_signals();
if let Some(config) = opt.get_ssl_config()? {
http_server