From c2a12b661a1ef9719416958abc4cc9e1945ee76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 31 Oct 2019 15:09:23 +0100 Subject: [PATCH] Make it a runnable server --- meilidb-http/Cargo.toml | 1 + meilidb-http/src/main.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/meilidb-http/Cargo.toml b/meilidb-http/Cargo.toml index fc8919ad0..062d4e5e9 100644 --- a/meilidb-http/Cargo.toml +++ b/meilidb-http/Cargo.toml @@ -18,6 +18,7 @@ http = "0.1.19" indexmap = { version = "1.3.0", features = ["serde-1"] } jemallocator = "0.3.2" log = "0.4.8" +main_error = "0.1.0" meilidb-core = { path = "../meilidb-core", version = "0.6.0" } meilidb-schema = { path = "../meilidb-schema", version = "0.6.0" } pretty-bytes = "0.2.2" diff --git a/meilidb-http/src/main.rs b/meilidb-http/src/main.rs index e7a11a969..cbb262e6a 100644 --- a/meilidb-http/src/main.rs +++ b/meilidb-http/src/main.rs @@ -1,3 +1,36 @@ -fn main() { - println!("Hello, world!"); +use http::header::HeaderValue; +use log::info; +use main_error::MainError; +use tide::middleware::{CorsMiddleware, CorsOrigin}; +use tide_log::RequestLogger; + +use meilidb_http::data::Data; +use meilidb_http::option::Opt; +use meilidb_http::routes; + +#[cfg(not(target_os = "macos"))] +#[global_allocator] +static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; + +pub fn main() -> Result<(), MainError> { + let opt = Opt::new(); + + let data = Data::new(opt.clone()); + 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(()) }