meilisearch/meilisearch-http/src/main.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

2019-11-22 02:15:33 +08:00
use std::env::VarError::NotPresent;
use std::{env, thread};
2019-10-31 22:09:23 +08:00
use http::header::HeaderValue;
use log::info;
use main_error::MainError;
use structopt::StructOpt;
2019-10-31 22:09:23 +08:00
use tide::middleware::{CorsMiddleware, CorsOrigin};
use tide_log::RequestLogger;
2019-11-26 18:06:55 +08:00
use meilisearch_http::data::Data;
use meilisearch_http::option::Opt;
use meilisearch_http::routes;
use meilisearch_http::routes::index::index_update_callback;
2019-10-31 22:09:23 +08:00
2019-11-22 02:15:33 +08:00
mod analytics;
2019-11-25 21:51:47 +08:00
#[cfg(target_os = "linux")]
2019-10-31 22:09:23 +08:00
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
pub fn main() -> Result<(), MainError> {
2019-11-12 02:03:38 +08:00
env_logger::init();
let opt = Opt::from_args();
2019-10-31 22:09:23 +08:00
let data = Data::new(opt.clone());
2019-11-22 02:15:33 +08:00
if env::var("MEILI_NO_ANALYTICS") == Err(NotPresent) {
thread::spawn(|| analytics::analytics_sender());
}
let data_cloned = data.clone();
data.db.set_update_callback(Box::new(move |name, status| {
index_update_callback(name, &data_cloned, status);
}));
2019-10-31 22:09:23 +08:00
let mut app = tide::App::with_state(data);
app.middleware(
CorsMiddleware::new()
.allow_origin(CorsOrigin::from("*"))
.allow_methods(HeaderValue::from_static("GET, POST, OPTIONS")),
);
app.middleware(RequestLogger::new());
app.middleware(tide_compression::Compression::new());
app.middleware(tide_compression::Decompression::new());
routes::load_routes(&mut app);
info!("Server HTTP enabled");
app.run(opt.http_addr)?;
Ok(())
2019-10-31 22:00:36 +08:00
}