extract the create_app function for the tests

This commit is contained in:
Tamo 2022-10-18 11:57:00 +02:00 committed by Clément Renault
parent d1a6fb2971
commit 634eb52926
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
5 changed files with 69 additions and 51 deletions

View File

@ -23,8 +23,8 @@ zip = { version = "0.6.2", optional = true }
[dependencies] [dependencies]
actix-cors = "0.6.3" 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-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 } 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"] } anyhow = { version = "1.0.65", features = ["backtrace"] }
async-stream = "0.3.3" async-stream = "0.3.3"

View File

@ -21,8 +21,10 @@ use std::{
}; };
use crate::error::MeilisearchHttpError; use crate::error::MeilisearchHttpError;
use actix_web::error::JsonPayloadError; use actix_cors::Cors;
use actix_web::web::Data; use actix_http::body::MessageBody;
use actix_web::{dev::ServiceFactory, error::JsonPayloadError, middleware};
use actix_web::{dev::ServiceResponse, web::Data};
use analytics::Analytics; use analytics::Analytics;
use anyhow::bail; use anyhow::bail;
use error::PayloadError; use error::PayloadError;
@ -61,6 +63,57 @@ fn is_empty_db(db_path: impl AsRef<Path>) -> bool {
} }
} }
pub fn create_app(
index_scheduler: Data<IndexScheduler>,
auth_controller: AuthController,
opt: Opt,
analytics: Arc<dyn Analytics>,
enable_dashboard: bool,
) -> actix_web::App<
impl ServiceFactory<
actix_web::dev::ServiceRequest,
Config = (),
Response = ServiceResponse<impl MessageBody>,
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 // TODO: TAMO: Finish setting up things
pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthController)> { pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthController)> {
// we don't want to create anything in the data.ms yet, thus we // 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.max_index_size.get_bytes() as usize,
(&opt.indexer_options).try_into()?, (&opt.indexer_options).try_into()?,
true, true,
#[cfg(test)]
todo!("We'll see later"),
) )
}; };
let meilisearch_builder = || -> anyhow::Result<_> { let meilisearch_builder = || -> anyhow::Result<_> {

View File

@ -2,15 +2,14 @@ use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use actix_cors::Cors;
use actix_web::http::KeepAlive; use actix_web::http::KeepAlive;
use actix_web::web::Data; use actix_web::web::Data;
use actix_web::{middleware, HttpServer}; use actix_web::HttpServer;
use clap::Parser; use clap::Parser;
use index_scheduler::IndexScheduler; use index_scheduler::IndexScheduler;
use meilisearch_auth::AuthController; use meilisearch_auth::AuthController;
use meilisearch_http::analytics::Analytics; 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}; use meilisearch_http::{setup_meilisearch, Opt};
#[global_allocator] #[global_allocator]
@ -77,44 +76,13 @@ async fn run_http(
let index_scheduler = Data::new(index_scheduler); let index_scheduler = Data::new(index_scheduler);
let http_server = HttpServer::new(move || { let http_server = HttpServer::new(move || {
let app = actix_web::App::new() create_app(
.configure(|s| { index_scheduler.clone(),
configure_data( auth_controller.clone(),
s, opt.clone(),
index_scheduler.clone(), analytics.clone(),
auth_controller.clone(), enable_dashboard,
&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
}) })
// Disable signals allows the server to terminate immediately when a user enter CTRL-C // Disable signals allows the server to terminate immediately when a user enter CTRL-C
.disable_signals() .disable_signals()

View File

@ -6,14 +6,13 @@ use std::path::Path;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use byte_unit::{Byte, ByteUnit}; use byte_unit::{Byte, ByteUnit};
use meilisearch_auth::AuthController; use meilisearch_auth::AuthController;
use meilisearch_http::setup_meilisearch;
use meilisearch_lib::options::{IndexerOpts, MaxMemory};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use serde_json::Value; use serde_json::Value;
use tempfile::TempDir; use tempfile::TempDir;
use meilisearch_http::option::{IndexerOpts, MaxMemory, Opt};
use meilisearch_http::setup_meilisearch;
use crate::common::encoder::Encoder; use crate::common::encoder::Encoder;
use meilisearch_http::option::Opt;
use super::index::Index; use super::index::Index;
use super::service::Service; use super::service::Service;

View File

@ -1,15 +1,15 @@
use actix_web::http::header::ContentType; use actix_web::http::header::ContentType;
use actix_web::test::TestRequest; use actix_web::test::TestRequest;
use actix_web::{http::StatusCode, test}; use actix_web::{http::StatusCode, test};
use index_scheduler::IndexScheduler;
use meilisearch_auth::AuthController; use meilisearch_auth::AuthController;
use meilisearch_lib::MeiliSearch;
use serde_json::Value; use serde_json::Value;
use crate::common::encoder::Encoder; use crate::common::encoder::Encoder;
use meilisearch_http::{analytics, create_app, Opt}; use meilisearch_http::{analytics, create_app, Opt};
pub struct Service { pub struct Service {
pub meilisearch: MeiliSearch, pub index_scheduler: IndexScheduler,
pub auth: AuthController, pub auth: AuthController,
pub options: Opt, pub options: Opt,
pub api_key: Option<String>, pub api_key: Option<String>,