mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 10:37:41 +08:00
refactor create_app macro
This commit is contained in:
parent
f2b2ca6d55
commit
71226feb74
@ -13,48 +13,34 @@ pub mod analytics;
|
|||||||
pub use self::data::Data;
|
pub use self::data::Data;
|
||||||
pub use option::Opt;
|
pub use option::Opt;
|
||||||
|
|
||||||
#[macro_export]
|
use actix_web::{HttpResponse, web};
|
||||||
macro_rules! create_app {
|
|
||||||
($data:expr, $enable_frontend:expr) => {
|
pub fn configure_data(config: &mut web::ServiceConfig, data: Data) {
|
||||||
{
|
let http_payload_size_limit = data.http_payload_size_limit();
|
||||||
use actix_cors::Cors;
|
config
|
||||||
use actix_web::middleware::TrailingSlash;
|
.data(data)
|
||||||
use actix_web::{App, HttpResponse};
|
.app_data(
|
||||||
use actix_web::{middleware, web};
|
web::JsonConfig::default()
|
||||||
use meilisearch_http::error::payload_error_handler;
|
.limit(dbg!(http_payload_size_limit))
|
||||||
use meilisearch_http::routes::*;
|
.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")]
|
#[cfg(feature = "mini-dashboard")]
|
||||||
|
pub fn dashboard(config: &mut web::ServiceConfig, enable_frontend: bool) {
|
||||||
use actix_web_static_files::Resource;
|
use actix_web_static_files::Resource;
|
||||||
|
|
||||||
#[cfg(feature = "mini-dashboard")]
|
|
||||||
mod dashboard {
|
mod dashboard {
|
||||||
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
|
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let app = App::new()
|
if enable_frontend {
|
||||||
.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;
|
|
||||||
let generated = dashboard::generate();
|
let generated = dashboard::generate();
|
||||||
let mut scope = web::scope("/");
|
let mut scope = web::scope("/");
|
||||||
// Generate routes for mini-dashboard assets
|
// Generate routes for mini-dashboard assets
|
||||||
@ -62,7 +48,7 @@ macro_rules! create_app {
|
|||||||
let Resource {mime_type, data, ..} = resource;
|
let Resource {mime_type, data, ..} = resource;
|
||||||
// Redirect index.html to /
|
// Redirect index.html to /
|
||||||
if path == "index.html" {
|
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)
|
HttpResponse::Ok().content_type(mime_type).body(data)
|
||||||
})));
|
})));
|
||||||
} else {
|
} else {
|
||||||
@ -71,15 +57,39 @@ macro_rules! create_app {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
app.service(scope)
|
config.service(scope);
|
||||||
} else {
|
} else {
|
||||||
app.service(running)
|
config.service(routes::running);
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "mini-dashboard"))]
|
#[cfg(not(feature = "mini-dashboard"))]
|
||||||
let app = app.service(running);
|
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()
|
Cors::default()
|
||||||
.send_wildcard()
|
.send_wildcard()
|
||||||
.allowed_headers(vec!["content-type", "x-meili-api-key"])
|
.allowed_headers(vec!["content-type", "x-meili-api-key"])
|
||||||
@ -89,9 +99,6 @@ macro_rules! create_app {
|
|||||||
)
|
)
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.wrap(middleware::Compress::default())
|
.wrap(middleware::Compress::default())
|
||||||
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
|
.wrap(middleware::NormalizePath::new(middleware::TrailingSlash::Trim))
|
||||||
.default_service(
|
}};
|
||||||
web::route().to(|| HttpResponse::NotFound()))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use std::env;
|
|||||||
|
|
||||||
use actix_web::HttpServer;
|
use actix_web::HttpServer;
|
||||||
use main_error::MainError;
|
use main_error::MainError;
|
||||||
use meilisearch_http::{create_app, Data, Opt};
|
use meilisearch_http::{Data, Opt, create_app};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
#[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>> {
|
async fn run_http(data: Data, opt: Opt) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let _enable_dashboard = &opt.env == "development";
|
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 allows the server to terminate immediately when a user enter CTRL-C
|
||||||
.disable_signals();
|
.disable_signals();
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use actix_web::web::Payload;
|
|
||||||
use actix_web::{delete, get, post, put};
|
use actix_web::{delete, get, post, put};
|
||||||
use actix_web::{web, HttpResponse};
|
use actix_web::{web, HttpResponse};
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
@ -130,7 +129,7 @@ async fn add_documents(
|
|||||||
data: web::Data<Data>,
|
data: web::Data<Data>,
|
||||||
path: web::Path<IndexParam>,
|
path: web::Path<IndexParam>,
|
||||||
params: web::Query<UpdateDocumentsQuery>,
|
params: web::Query<UpdateDocumentsQuery>,
|
||||||
body: Payload,
|
body: web::Payload,
|
||||||
) -> Result<HttpResponse, ResponseError> {
|
) -> Result<HttpResponse, ResponseError> {
|
||||||
let update_status = data
|
let update_status = data
|
||||||
.add_documents(
|
.add_documents(
|
||||||
|
Loading…
Reference in New Issue
Block a user