MeiliSearch/meilisearch-http/src/lib.rs

163 lines
5.1 KiB
Rust
Raw Normal View History

2020-12-12 13:32:06 +01:00
pub mod data;
2021-06-15 17:55:27 +02:00
#[macro_use]
2020-12-12 13:32:06 +01:00
pub mod error;
pub mod helpers;
2021-03-04 11:23:41 +01:00
mod index;
2021-01-13 17:50:36 +01:00
mod index_controller;
2021-03-15 18:11:10 +01:00
pub mod option;
pub mod routes;
2020-12-12 13:32:06 +01:00
2021-06-16 17:12:49 +02:00
#[cfg(all(not(debug_assertions), feature = "analytics"))]
pub mod analytics;
2021-06-23 13:55:16 +02:00
use std::{pin::Pin, task::{Context, Poll}};
2020-12-12 13:32:06 +01:00
pub use self::data::Data;
2021-06-23 13:55:16 +02:00
use futures::{Stream, future::{Ready, ready}};
2021-03-15 18:11:10 +01:00
pub use option::Opt;
2021-03-10 11:56:51 +01:00
2021-06-23 13:55:16 +02:00
use actix_web::{FromRequest, HttpRequest, dev, error::PayloadError, web};
2021-06-23 13:21:48 +02:00
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()),
);
}
2021-03-10 11:56:51 +01:00
2021-06-23 13:21:48 +02:00
#[cfg(feature = "mini-dashboard")]
pub fn dashboard(config: &mut web::ServiceConfig, enable_frontend: bool) {
use actix_web_static_files::Resource;
2021-06-23 13:55:16 +02:00
use actix_web::HttpResponse;
2021-04-21 13:49:21 +02:00
2021-06-23 13:21:48 +02:00
mod dashboard {
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
}
2021-04-21 13:49:21 +02:00
2021-06-23 13:21:48 +02:00
if enable_frontend {
let generated = dashboard::generate();
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-22 21:48:51 +02:00
}
2021-06-23 13:21:48 +02:00
}
config.service(scope);
} else {
config.service(routes::running);
}
}
#[cfg(not(feature = "mini-dashboard"))]
pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
config.service(routes::running);
}
2021-04-21 13:49:21 +02:00
2021-06-23 13:21:48 +02: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::*;
use meilisearch_http::{dashboard, configure_data};
2021-04-21 13:49:21 +02:00
2021-06-23 13:21:48 +02:00
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(
2021-06-22 21:48:51 +02:00
Cors::default()
2021-05-31 16:03:39 +02: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-22 21:48:51 +02:00
)
2021-06-23 13:21:48 +02:00
.wrap(middleware::Logger::default())
.wrap(middleware::Compress::default())
.wrap(middleware::NormalizePath::new(middleware::TrailingSlash::Trim))
}};
2021-03-10 11:56:51 +01:00
}
2021-06-23 13:55:16 +02:00
pub struct Payload {
payload: dev::Payload,
limit: usize,
}
pub struct PayloadConfig {
limit: usize,
}
impl Default for PayloadConfig {
fn default() -> Self {
Self { limit: 256 * 1024 }
}
}
impl FromRequest for Payload {
type Config = PayloadConfig;
type Error = PayloadError;
type Future = Ready<Result<Payload, Self::Error>>;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
let limit = req.app_data::<PayloadConfig>().map(|c| c.limit).unwrap_or(Self::Config::default().limit);
ready(Ok(Payload { payload: payload.take(), limit }))
}
}
impl Stream for Payload {
type Item = Result<web::Bytes, PayloadError>;
#[inline]
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match Pin::new(&mut self.payload).poll_next(cx) {
Poll::Ready(Some(result)) => {
match result {
Ok(bytes) => {
match self.limit.checked_sub(bytes.len()) {
Some(new_limit) => {
self.limit = new_limit;
Poll::Ready(Some(Ok(bytes)))
}
None => Poll::Ready(Some(Err(PayloadError::Overflow))),
}
}
x => Poll::Ready(Some(x)),
}
},
otherwise => otherwise,
}
}
}