diff --git a/meilisearch-http/Cargo.toml b/meilisearch-http/Cargo.toml index 64d6e6f2b..54ee2b6a9 100644 --- a/meilisearch-http/Cargo.toml +++ b/meilisearch-http/Cargo.toml @@ -23,8 +23,8 @@ zip = { version = "0.6.2", optional = true } [dependencies] actix-cors = "0.6.3" +actix-http = { version = "3.2.2", default-features = false, features = ["compress-brotli", "compress-gzip", "rustls"] } actix-web = { version = "4.2.1", default-features = false, features = ["macros", "compress-brotli", "compress-gzip", "cookies", "rustls"] } -actix-http = "3.2.2" actix-web-static-files = { git = "https://github.com/kilork/actix-web-static-files.git", rev = "2d3b6160", optional = true } anyhow = { version = "1.0.65", features = ["backtrace"] } async-stream = "0.3.3" diff --git a/meilisearch-http/src/lib.rs b/meilisearch-http/src/lib.rs index 5b2d9b89d..f8dc8f9bb 100644 --- a/meilisearch-http/src/lib.rs +++ b/meilisearch-http/src/lib.rs @@ -21,8 +21,10 @@ use std::{ }; use crate::error::MeilisearchHttpError; -use actix_web::error::JsonPayloadError; -use actix_web::web::Data; +use actix_cors::Cors; +use actix_http::body::MessageBody; +use actix_web::{dev::ServiceFactory, error::JsonPayloadError, middleware}; +use actix_web::{dev::ServiceResponse, web::Data}; use analytics::Analytics; use anyhow::bail; use error::PayloadError; @@ -61,6 +63,57 @@ fn is_empty_db(db_path: impl AsRef) -> bool { } } +pub fn create_app( + index_scheduler: Data, + auth_controller: AuthController, + opt: Opt, + analytics: Arc, + enable_dashboard: bool, +) -> actix_web::App< + impl ServiceFactory< + actix_web::dev::ServiceRequest, + Config = (), + Response = ServiceResponse, + Error = actix_web::Error, + InitError = (), + >, +> { + let app = actix_web::App::new() + .configure(|s| { + configure_data( + s, + index_scheduler.clone(), + auth_controller.clone(), + &opt, + analytics.clone(), + ) + }) + .configure(routes::configure) + .configure(|s| dashboard(s, enable_dashboard)); + #[cfg(feature = "metrics")] + let app = app.configure(|s| configure_metrics_route(s, opt.enable_metrics_route)); + let app = app + .wrap( + Cors::default() + .send_wildcard() + .allow_any_header() + .allow_any_origin() + .allow_any_method() + .max_age(86_400), // 24h + ) + .wrap(middleware::Logger::default()) + .wrap(middleware::Compress::default()) + .wrap(middleware::NormalizePath::new( + middleware::TrailingSlash::Trim, + )); + #[cfg(feature = "metrics")] + let app = app.wrap(Condition::new( + opt.enable_metrics_route, + route_metrics::RouteMetrics, + )); + app +} + // TODO: TAMO: Finish setting up things pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthController)> { // we don't want to create anything in the data.ms yet, thus we @@ -75,8 +128,6 @@ pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthContr opt.max_index_size.get_bytes() as usize, (&opt.indexer_options).try_into()?, true, - #[cfg(test)] - todo!("We'll see later"), ) }; let meilisearch_builder = || -> anyhow::Result<_> { diff --git a/meilisearch-http/src/main.rs b/meilisearch-http/src/main.rs index aeafb5da1..a6d20086e 100644 --- a/meilisearch-http/src/main.rs +++ b/meilisearch-http/src/main.rs @@ -2,15 +2,14 @@ use std::env; use std::path::PathBuf; use std::sync::Arc; -use actix_cors::Cors; use actix_web::http::KeepAlive; use actix_web::web::Data; -use actix_web::{middleware, HttpServer}; +use actix_web::HttpServer; use clap::Parser; use index_scheduler::IndexScheduler; use meilisearch_auth::AuthController; use meilisearch_http::analytics::Analytics; -use meilisearch_http::{analytics, configure_data, dashboard, routes}; +use meilisearch_http::{analytics, create_app}; use meilisearch_http::{setup_meilisearch, Opt}; #[global_allocator] @@ -77,44 +76,13 @@ async fn run_http( let index_scheduler = Data::new(index_scheduler); let http_server = HttpServer::new(move || { - let app = actix_web::App::new() - .configure(|s| { - configure_data( - s, - index_scheduler.clone(), - auth_controller.clone(), - &opt, - analytics.clone(), - ) - }) - .configure(routes::configure) - .configure(|s| dashboard(s, enable_dashboard)); - - #[cfg(feature = "metrics")] - let app = app.configure(|s| configure_metrics_route(s, opt.enable_metrics_route)); - - let app = app - .wrap( - Cors::default() - .send_wildcard() - .allow_any_header() - .allow_any_origin() - .allow_any_method() - .max_age(86_400), // 24h - ) - .wrap(middleware::Logger::default()) - .wrap(middleware::Compress::default()) - .wrap(middleware::NormalizePath::new( - middleware::TrailingSlash::Trim, - )); - - #[cfg(feature = "metrics")] - let app = app.wrap(Condition::new( - opt.enable_metrics_route, - route_metrics::RouteMetrics, - )); - - app + create_app( + index_scheduler.clone(), + auth_controller.clone(), + opt.clone(), + analytics.clone(), + enable_dashboard, + ) }) // Disable signals allows the server to terminate immediately when a user enter CTRL-C .disable_signals() diff --git a/meilisearch-http/tests/common/server.rs b/meilisearch-http/tests/common/server.rs index f243a11b9..812f6d820 100644 --- a/meilisearch-http/tests/common/server.rs +++ b/meilisearch-http/tests/common/server.rs @@ -6,14 +6,13 @@ use std::path::Path; use actix_web::http::StatusCode; use byte_unit::{Byte, ByteUnit}; use meilisearch_auth::AuthController; -use meilisearch_http::setup_meilisearch; -use meilisearch_lib::options::{IndexerOpts, MaxMemory}; use once_cell::sync::Lazy; use serde_json::Value; use tempfile::TempDir; +use meilisearch_http::option::{IndexerOpts, MaxMemory, Opt}; +use meilisearch_http::setup_meilisearch; use crate::common::encoder::Encoder; -use meilisearch_http::option::Opt; use super::index::Index; use super::service::Service; diff --git a/meilisearch-http/tests/common/service.rs b/meilisearch-http/tests/common/service.rs index 3a3f6021c..ce33ef1d0 100644 --- a/meilisearch-http/tests/common/service.rs +++ b/meilisearch-http/tests/common/service.rs @@ -1,15 +1,15 @@ use actix_web::http::header::ContentType; use actix_web::test::TestRequest; use actix_web::{http::StatusCode, test}; +use index_scheduler::IndexScheduler; use meilisearch_auth::AuthController; -use meilisearch_lib::MeiliSearch; use serde_json::Value; use crate::common::encoder::Encoder; use meilisearch_http::{analytics, create_app, Opt}; pub struct Service { - pub meilisearch: MeiliSearch, + pub index_scheduler: IndexScheduler, pub auth: AuthController, pub options: Opt, pub api_key: Option,