meilisearch/meilisearch-http/src/lib.rs

137 lines
4.5 KiB
Rust
Raw Normal View History

2020-12-12 20:32:06 +08:00
pub mod data;
2021-06-15 23:55:27 +08:00
#[macro_use]
2020-12-12 20:32:06 +08:00
pub mod error;
2021-06-24 20:22:12 +08:00
#[macro_use]
2021-06-23 20:56:02 +08:00
pub mod extractors;
2020-12-12 20:32:06 +08:00
pub mod helpers;
2021-03-04 18:23:41 +08:00
mod index;
2021-01-14 00:50:36 +08:00
mod index_controller;
2021-03-16 01:11:10 +08:00
pub mod option;
pub mod routes;
2020-12-12 20:32:06 +08:00
2021-06-16 23:12:49 +08:00
#[cfg(all(not(debug_assertions), feature = "analytics"))]
pub mod analytics;
2021-06-24 20:22:12 +08:00
use crate::extractors::authentication::AuthConfig;
2020-12-12 20:32:06 +08:00
pub use self::data::Data;
2021-03-16 01:11:10 +08:00
pub use option::Opt;
2021-03-10 18:56:51 +08:00
2021-06-23 20:56:02 +08:00
use actix_web::web;
use extractors::payload::PayloadConfig;
2021-06-24 20:22:12 +08:00
use extractors::authentication::policies::*;
2021-06-23 19:21:48 +08:00
pub fn configure_data(config: &mut web::ServiceConfig, data: Data) {
let http_payload_size_limit = data.http_payload_size_limit();
config
2021-06-24 20:22:12 +08:00
.data(data.clone())
.app_data(data)
2021-06-23 19:21:48 +08:00
.app_data(
web::JsonConfig::default()
2021-06-23 21:02:00 +08:00
.limit(http_payload_size_limit)
2021-06-23 19:21:48 +08:00
.content_type(|_mime| true) // Accept all mime types
.error_handler(|err, _req| error::payload_error_handler(err).into()),
)
2021-06-23 19:58:22 +08:00
.app_data(PayloadConfig::new(http_payload_size_limit))
2021-06-23 19:21:48 +08:00
.app_data(
web::QueryConfig::default()
.error_handler(|err, _req| error::payload_error_handler(err).into()),
);
}
2021-03-10 18:56:51 +08:00
2021-06-24 20:22:12 +08:00
pub fn configure_auth(config: &mut web::ServiceConfig, data: &Data) {
let keys = data.api_keys();
let auth_config = if let Some(ref master_key) = keys.master {
let private_key = keys.private.as_ref().unwrap();
let public_key = keys.public.as_ref().unwrap();
let mut policies = init_policies!(Public, Private, Admin);
create_users!(
policies,
master_key.as_bytes() => { Admin, Private, Public },
private_key.as_bytes() => { Private, Public },
public_key.as_bytes() => { Public }
);
AuthConfig::Auth(policies)
} else {
AuthConfig::NoAuth
};
config.app_data(auth_config);
2021-06-24 01:35:26 +08:00
}
2021-06-23 19:21:48 +08:00
#[cfg(feature = "mini-dashboard")]
pub fn dashboard(config: &mut web::ServiceConfig, enable_frontend: bool) {
use actix_web_static_files::Resource;
2021-06-23 19:55:16 +08:00
use actix_web::HttpResponse;
2021-04-21 19:49:21 +08:00
2021-06-23 20:48:33 +08:00
mod generated {
2021-06-23 19:21:48 +08:00
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
}
2021-04-21 19:49:21 +08:00
2021-06-23 19:21:48 +08:00
if enable_frontend {
2021-06-23 20:48:33 +08:00
let generated = generated::generate();
2021-06-23 19:21:48 +08:00
let mut scope = web::scope("/");
// Generate routes for mini-dashboard assets
for (path, resource) in generated.into_iter() {
let Resource {mime_type, data, ..} = resource;
// Redirect index.html to /
if path == "index.html" {
config.service(web::resource("/").route(web::get().to(move || {
HttpResponse::Ok().content_type(mime_type).body(data)
})));
} else {
scope = scope.service(web::resource(path).route(web::get().to(move || {
HttpResponse::Ok().content_type(mime_type).body(data)
})));
2021-06-23 03:48:51 +08:00
}
2021-06-23 19:21:48 +08:00
}
config.service(scope);
} else {
2021-06-24 21:40:04 +08:00
config.route("/", web::get().to(routes::running));
2021-06-23 19:21:48 +08:00
}
}
#[cfg(not(feature = "mini-dashboard"))]
pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
2021-06-24 21:40:04 +08:00
config.route("/", web::get().to(routes::running));
2021-06-23 19:21:48 +08:00
}
2021-04-21 19:49:21 +08:00
2021-06-23 19:21:48 +08:00
#[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::*;
2021-06-24 20:22:12 +08:00
use meilisearch_http::{configure_data, dashboard, configure_auth};
2021-04-21 19:49:21 +08:00
2021-06-23 19:21:48 +08:00
App::new()
.configure(|s| configure_data(s, $data.clone()))
2021-06-24 20:22:12 +08:00
.configure(|s| configure_auth(s, &$data))
2021-06-23 19:21:48 +08:00
.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(
2021-06-23 03:48:51 +08:00
Cors::default()
2021-06-23 20:48:33 +08:00
.send_wildcard()
.allowed_headers(vec!["content-type", "x-meili-api-key"])
.allow_any_origin()
.allow_any_method()
.max_age(86_400), // 24h
2021-06-23 03:48:51 +08:00
)
2021-06-23 19:21:48 +08:00
.wrap(middleware::Logger::default())
.wrap(middleware::Compress::default())
2021-06-23 20:48:33 +08:00
.wrap(middleware::NormalizePath::new(
middleware::TrailingSlash::Trim,
))
2021-06-23 19:21:48 +08:00
}};
2021-03-10 18:56:51 +08:00
}