refactor create_app macro

This commit is contained in:
marin postma 2021-06-23 13:21:48 +02:00
parent f2b2ca6d55
commit 71226feb74
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9
3 changed files with 83 additions and 77 deletions

View File

@ -13,48 +13,34 @@ pub mod analytics;
pub use self::data::Data;
pub use option::Opt;
#[macro_export]
macro_rules! create_app {
($data:expr, $enable_frontend:expr) => {
{
use actix_cors::Cors;
use actix_web::middleware::TrailingSlash;
use actix_web::{App, HttpResponse};
use actix_web::{middleware, web};
use meilisearch_http::error::payload_error_handler;
use meilisearch_http::routes::*;
use actix_web::{HttpResponse, web};
#[cfg(feature = "mini-dashboard")]
pub fn configure_data(config: &mut web::ServiceConfig, data: Data) {
let http_payload_size_limit = data.http_payload_size_limit();
config
.data(data)
.app_data(
web::JsonConfig::default()
.limit(dbg!(http_payload_size_limit))
.content_type(|_mime| true) // Accept all mime types
.error_handler(|err, _req| error::payload_error_handler(err).into()),
)
.app_data(web::PayloadConfig::new(http_payload_size_limit))
.app_data(
web::QueryConfig::default()
.error_handler(|err, _req| error::payload_error_handler(err).into()),
);
}
#[cfg(feature = "mini-dashboard")]
pub fn dashboard(config: &mut web::ServiceConfig, enable_frontend: bool) {
use actix_web_static_files::Resource;
#[cfg(feature = "mini-dashboard")]
mod dashboard {
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
}
let app = App::new()
.data($data.clone())
.app_data(
web::JsonConfig::default()
.limit($data.http_payload_size_limit())
.content_type(|_mime| true) // Accept all mime types
.error_handler(|err, _req| payload_error_handler(err).into()),
)
.app_data(
web::QueryConfig::default()
.error_handler(|err, _req| payload_error_handler(err).into()),
)
.configure(document::services)
.configure(index::services)
.configure(search::services)
.configure(settings::services)
.configure(health::services)
.configure(stats::services)
.configure(key::services)
.configure(dump::services);
#[cfg(feature = "mini-dashboard")]
let app = if $enable_frontend {
let mut app = app;
if enable_frontend {
let generated = dashboard::generate();
let mut scope = web::scope("/");
// Generate routes for mini-dashboard assets
@ -62,7 +48,7 @@ macro_rules! create_app {
let Resource {mime_type, data, ..} = resource;
// Redirect index.html to /
if path == "index.html" {
app = app.service(web::resource("/").route(web::get().to(move || {
config.service(web::resource("/").route(web::get().to(move || {
HttpResponse::Ok().content_type(mime_type).body(data)
})));
} else {
@ -71,15 +57,39 @@ macro_rules! create_app {
})));
}
}
app.service(scope)
config.service(scope);
} else {
app.service(running)
};
config.service(routes::running);
}
}
#[cfg(not(feature = "mini-dashboard"))]
let app = app.service(running);
#[cfg(not(feature = "mini-dashboard"))]
pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
config.service(routes::running);
}
app.wrap(
#[macro_export]
macro_rules! create_app {
($data:expr, $enable_frontend:expr) => {{
use actix_cors::Cors;
use actix_web::middleware::TrailingSlash;
use actix_web::App;
use actix_web::{middleware, web};
use meilisearch_http::routes::*;
use meilisearch_http::{dashboard, configure_data};
App::new()
.configure(|s| configure_data(s, $data.clone()))
.configure(document::services)
.configure(index::services)
.configure(search::services)
.configure(settings::services)
.configure(health::services)
.configure(stats::services)
.configure(key::services)
.configure(dump::services)
.configure(|s| dashboard(s, $enable_frontend))
.wrap(
Cors::default()
.send_wildcard()
.allowed_headers(vec!["content-type", "x-meili-api-key"])
@ -89,9 +99,6 @@ macro_rules! create_app {
)
.wrap(middleware::Logger::default())
.wrap(middleware::Compress::default())
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
.default_service(
web::route().to(|| HttpResponse::NotFound()))
}
};
.wrap(middleware::NormalizePath::new(middleware::TrailingSlash::Trim))
}};
}

View File

@ -2,7 +2,7 @@ use std::env;
use actix_web::HttpServer;
use main_error::MainError;
use meilisearch_http::{create_app, Data, Opt};
use meilisearch_http::{Data, Opt, create_app};
use structopt::StructOpt;
#[cfg(all(not(debug_assertions), feature = "analytics"))]
@ -74,7 +74,7 @@ async fn main() -> Result<(), MainError> {
async fn run_http(data: Data, opt: Opt) -> Result<(), Box<dyn std::error::Error>> {
let _enable_dashboard = &opt.env == "development";
let http_server = HttpServer::new(move || create_app!(&data, _enable_dashboard))
let http_server = HttpServer::new(move || create_app!(data, _enable_dashboard))
// Disable signals allows the server to terminate immediately when a user enter CTRL-C
.disable_signals();

View File

@ -1,4 +1,3 @@
use actix_web::web::Payload;
use actix_web::{delete, get, post, put};
use actix_web::{web, HttpResponse};
use indexmap::IndexMap;
@ -130,7 +129,7 @@ async fn add_documents(
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<UpdateDocumentsQuery>,
body: Payload,
body: web::Payload,
) -> Result<HttpResponse, ResponseError> {
let update_status = data
.add_documents(