From 6bcf20c70ec0af423603037723f0d2665e7ea963 Mon Sep 17 00:00:00 2001 From: Marin Postma Date: Wed, 21 Apr 2021 13:49:21 +0200 Subject: [PATCH] serve static site --- Cargo.lock | 90 ++++++++++++++++++++++++++--------- meilisearch-error/Cargo.toml | 2 +- meilisearch-http/Cargo.toml | 16 ++++--- meilisearch-http/build.rs | 2 + meilisearch-http/src/error.rs | 10 ++-- meilisearch-http/src/lib.rs | 29 +++++++++-- meilisearch-http/src/main.rs | 1 + 7 files changed, 114 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f007a0179..8065d7ae4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,8 +19,7 @@ dependencies = [ [[package]] name = "actix-cors" version = "0.6.0-beta.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa50c395d15e5946cf04bccb1edef583e9fd42aa6710a1f89a725c5e2c4c5503" +source = "git+https://github.com/MarinPostma/actix-extras.git?rev=2dac1a4#2dac1a421619bf7b386dea63d3ae25a3bc4abc43" dependencies = [ "actix-service", "actix-web", @@ -33,9 +32,9 @@ dependencies = [ [[package]] name = "actix-http" -version = "3.0.0-beta.5" +version = "3.0.0-beta.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb9c5d7ceb490d6565156ae1d4d467db17da1759425c65a2e36ac5e182e014e2" +checksum = "59d51c2ba06062e698a5d212d860e9fb2afc931c285ede687aaae896c8150347" dependencies = [ "actix-codec", "actix-rt", @@ -48,7 +47,6 @@ dependencies = [ "brotli2", "bytes 1.0.1", "bytestring", - "cookie", "derive_more", "encoding_rs", "flate2", @@ -63,14 +61,13 @@ dependencies = [ "log", "mime", "once_cell", + "paste", "percent-encoding", "pin-project", "pin-project-lite 0.2.6", "rand 0.8.3", "regex", "serde", - "serde_json", - "serde_urlencoded", "sha-1 0.9.4", "smallvec", "time 0.2.26", @@ -113,9 +110,9 @@ dependencies = [ [[package]] name = "actix-server" -version = "2.0.0-beta.4" +version = "2.0.0-beta.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0872f02a1871257ef09c5a269dce5dc5fea5ccf502adbf5d39f118913b61411c" +checksum = "26369215fcc3b0176018b3b68756a8bcc275bb000e6212e454944913a1f9bf87" dependencies = [ "actix-rt", "actix-service", @@ -130,11 +127,12 @@ dependencies = [ [[package]] name = "actix-service" -version = "2.0.0-beta.5" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf82340ad9f4e4caf43737fd3bbc999778a268015cdc54675f60af6240bd2b05" +checksum = "77f5f9d66a8730d0fae62c26f3424f5751e5518086628a40b7ab6fca4a705034" dependencies = [ "futures-core", + "paste", "pin-project-lite 0.2.6", ] @@ -169,9 +167,9 @@ dependencies = [ [[package]] name = "actix-web" -version = "4.0.0-beta.5" +version = "4.0.0-beta.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6de19cc341c2e68b1ee126de171e86b610b5bbcecc660d1250ebed73e0257bd6" +checksum = "ff12e933051557d700b0fcad20fe25b9ca38395cc87bbc5aeaddaef17b937a2f" dependencies = [ "actix-codec", "actix-http", @@ -185,11 +183,13 @@ dependencies = [ "actix-web-codegen", "ahash 0.7.2", "bytes 1.0.1", + "cookie", "derive_more", "either", "encoding_rs", "futures-core", "futures-util", + "itoa", "language-tags", "log", "mime", @@ -216,6 +216,20 @@ dependencies = [ "syn 1.0.70", ] +[[package]] +name = "actix-web-static-files" +version = "3.0.5" +source = "git+https://github.com/MarinPostma/actix-web-static-files.git?branch=actix-web-4#6db8c3e2940d61659581492b5e9c9b9062567613" +dependencies = [ + "actix-service", + "actix-web", + "change-detection", + "derive_more", + "futures", + "mime_guess", + "path-slash", +] + [[package]] name = "addr2line" version = "0.14.1" @@ -583,6 +597,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "change-detection" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "159fa412eae48a1d94d0b9ecdb85c97ce56eb2a347c62394d3fdbf221adabc1a" +dependencies = [ + "path-matchers", + "path-slash", +] + [[package]] name = "character_converter" version = "1.0.0" @@ -635,9 +659,9 @@ checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" [[package]] name = "cookie" -version = "0.14.4" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03a5d7b21829bc7b4bf4754a978a241ae54ea55a40f92bb20216e54096f4b951" +checksum = "ffdf8865bac3d9a3bde5bde9088ca431b11f5d37c7a578b8086af77248b76627" dependencies = [ "percent-encoding", "time 0.2.26", @@ -1329,6 +1353,12 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" +[[package]] +name = "httpdate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05842d0d43232b23ccb7060ecb0f0626922c21f30012e97b767b30afd4a5d4b9" + [[package]] name = "human_format" version = "1.0.3" @@ -1364,7 +1394,7 @@ dependencies = [ "http", "http-body 0.3.1", "httparse", - "httpdate", + "httpdate 0.3.2", "itoa", "pin-project", "socket2 0.3.19", @@ -1376,9 +1406,9 @@ dependencies = [ [[package]] name = "hyper" -version = "0.14.5" +version = "0.14.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bf09f61b52cfcf4c00de50df88ae423d6c02354e385a86341133b5338630ad1" +checksum = "5f006b8784cfb01fe7aa9c46f5f5cd4cf5c85a8c612a0653ec97642979062665" dependencies = [ "bytes 1.0.1", "futures-channel", @@ -1388,7 +1418,7 @@ dependencies = [ "http", "http-body 0.4.1", "httparse", - "httpdate", + "httpdate 1.0.0", "itoa", "pin-project", "socket2 0.4.0", @@ -1421,7 +1451,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f9f7a97316d44c0af9b0301e65010573a853a9fc97046d7331d7f6bc0fd5a64" dependencies = [ "futures-util", - "hyper 0.14.5", + "hyper 0.14.6", "log", "rustls 0.19.1", "tokio 1.5.0", @@ -1719,6 +1749,7 @@ dependencies = [ "actix-rt", "actix-service", "actix-web", + "actix-web-static-files", "anyhow", "assert-json-diff", "async-compression", @@ -2126,6 +2157,21 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58" +[[package]] +name = "path-matchers" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36cd9b72a47679ec193a5f0229d9ab686b7bd45e1fbc59ccf953c9f3d83f7b2b" +dependencies = [ + "glob", +] + +[[package]] +name = "path-slash" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cacbb3c4ff353b534a67fb8d7524d00229da4cb1dc8c79f4db96e375ab5b619" + [[package]] name = "percent-encoding" version = "2.1.0" @@ -2634,7 +2680,7 @@ dependencies = [ "futures-util", "http", "http-body 0.4.1", - "hyper 0.14.5", + "hyper 0.14.6", "hyper-rustls 0.22.1", "ipnet", "js-sys", @@ -2812,7 +2858,7 @@ dependencies = [ "env_logger 0.7.1", "failure", "hostname", - "httpdate", + "httpdate 0.3.2", "im", "lazy_static", "libc", diff --git a/meilisearch-error/Cargo.toml b/meilisearch-error/Cargo.toml index 7673e1391..1340b0020 100644 --- a/meilisearch-error/Cargo.toml +++ b/meilisearch-error/Cargo.toml @@ -5,4 +5,4 @@ authors = ["marin "] edition = "2018" [dependencies] -actix-http = "=3.0.0-beta.5" +actix-http = "=3.0.0-beta.6" diff --git a/meilisearch-http/Cargo.toml b/meilisearch-http/Cargo.toml index 019a1a3f8..e2edbccca 100644 --- a/meilisearch-http/Cargo.toml +++ b/meilisearch-http/Cargo.toml @@ -5,11 +5,14 @@ edition = "2018" license = "MIT" name = "meilisearch-http" version = "0.21.0-alpha.3" +build = "build.rs" + [[bin]] name = "meilisearch" path = "src/main.rs" [build-dependencies] +actix-web-static-files = { git = "https://github.com/MarinPostma/actix-web-static-files.git", branch = "actix-web-4" } anyhow = "*" cargo_toml = "0.9.0" hex = "0.4.3" @@ -19,10 +22,11 @@ vergen = "3.1.0" zip = "0.5.12" [dependencies] -actix-cors = "0.6.0-beta.1" -actix-http = { version = "=3.0.0-beta.5" } -actix-service = "=2.0.0-beta.5" -actix-web = { version = "=4.0.0-beta.5", features = ["rustls"] } +actix-cors = { git = "https://github.com/MarinPostma/actix-extras.git", rev = "2dac1a4"} +actix-http = { version = "=3.0.0-beta.6" } +actix-service = "2.0.0" +actix-web = { version = "=4.0.0-beta.6", features = ["rustls"] } +actix-web-static-files = { git = "https://github.com/MarinPostma/actix-web-static-files.git", branch = "actix-web-4", optional = true } anyhow = "1.0.36" async-compression = { version = "0.3.6", features = ["gzip", "tokio-02"] } async-stream = "0.3.0" @@ -51,6 +55,7 @@ memmap = "0.7.0" milli = { git = "https://github.com/meilisearch/milli.git", tag = "v0.1.1" } mime = "0.3.16" once_cell = "1.5.2" +oxidized-json-checker = "0.3.2" parking_lot = "0.11.1" rand = "0.7.3" rayon = "1.5.0" @@ -67,7 +72,6 @@ tempfile = "3.1.0" thiserror = "1.0.24" tokio = { version = "1", features = ["full"] } uuid = "0.8.2" -oxidized-json-checker = "0.3.2" walkdir = "2.3.2" obkv = "0.1.1" @@ -97,7 +101,7 @@ tempdir = "0.3.7" urlencoding = "1.1.1" [features] -mini-dashboard = ["default"] +mini-dashboard = ["default", "actix-web-static-files"] default = ["sentry"] [target.'cfg(target_os = "linux")'.dependencies] diff --git a/meilisearch-http/build.rs b/meilisearch-http/build.rs index 542ae20e3..ef3b67271 100644 --- a/meilisearch-http/build.rs +++ b/meilisearch-http/build.rs @@ -6,6 +6,7 @@ use std::path::PathBuf; use anyhow::Context; use sha1::{Sha1, Digest}; use reqwest::blocking::get; +use actix_web_static_files::resource_dir; use vergen::{generate_cargo_keys, ConstantsFlags}; use cargo_toml::Manifest; @@ -54,5 +55,6 @@ fn setup_mini_dashboard() -> anyhow::Result<()> { let cursor = Cursor::new(&dashboard_assets_bytes); let mut zip = zip::read::ZipArchive::new(cursor)?; zip.extract(&dashboard_dir)?; + resource_dir(&dashboard_dir).build()?; Ok(()) } diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index 8bfdb3573..6489716ca 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -2,7 +2,8 @@ use std::error; use std::fmt; use actix_web as aweb; -use actix_web::dev::HttpResponseBuilder; +use actix_web::body::Body; +use actix_web::dev::BaseHttpResponseBuilder; use actix_web::error::{JsonPayloadError, QueryPayloadError}; use actix_web::http::Error as HttpError; use actix_web::http::StatusCode; @@ -71,8 +72,9 @@ impl Serialize for ResponseError { } impl aweb::error::ResponseError for ResponseError { - fn error_response(&self) -> aweb::HttpResponse { - HttpResponseBuilder::new(self.status_code()).json(&self) + fn error_response(&self) -> aweb::BaseHttpResponse { + let json = serde_json::to_vec(self).unwrap(); + BaseHttpResponseBuilder::new(self.status_code()).body(json) } fn status_code(&self) -> StatusCode { @@ -297,6 +299,7 @@ impl From for Error { JsonPayloadError::Payload(err) => { Error::BadRequest(format!("Problem while decoding the request: {}", err)) } + e => Error::Internal(format!("Unexpected Json error: {}", e)) } } } @@ -307,6 +310,7 @@ impl From for Error { QueryPayloadError::Deserialize(err) => { Error::BadRequest(format!("Invalid query parameters: {}", err)) } + e => Error::Internal(format!("Unexpected query payload error: {}", e)) } } } diff --git a/meilisearch-http/src/lib.rs b/meilisearch-http/src/lib.rs index 7665b5694..fcbd6e6cb 100644 --- a/meilisearch-http/src/lib.rs +++ b/meilisearch-http/src/lib.rs @@ -9,6 +9,7 @@ pub mod routes; pub use self::data::Data; pub use option::Opt; + #[macro_export] macro_rules! create_app { ($data:expr, $enable_frontend:expr) => {{ @@ -19,6 +20,14 @@ macro_rules! create_app { use meilisearch_http::error::payload_error_handler; use meilisearch_http::routes::*; + #[cfg(feature = "mini-dashboard")] + use actix_web_static_files::ResourceFiles; + + #[cfg(feature = "mini-dashboard")] + mod dashboard { + include!(concat!(env!("OUT_DIR"), "/generated.rs")); + } + let app = App::new() .data($data.clone()) .app_data( @@ -40,16 +49,28 @@ macro_rules! create_app { .configure(stats::services) .configure(key::services); //.configure(routes::dump::services); + #[cfg(feature = "mini-dashboard")] let app = if $enable_frontend { - app.service(load_html).service(load_css) + let generated = dashboard::generate(); + let keys = generated.keys().collect::>(); + println!("served files {:?}", keys); + let service = ResourceFiles::new("/", generated); + app.service(service) } else { app.service(running) }; + + #[cfg(not(feature = "mini-dashboard"))] + let app = app.service(running); + + println!("here\n\n\n"); app.wrap( Cors::default() - .send_wildcard() - .allowed_headers(vec!["content-type", "x-meili-api-key"]) - .max_age(86_400), // 24h + .send_wildcard() + .allowed_headers(vec!["content-type", "x-meili-api-key"]) + .allow_any_origin() + .allow_any_method() + .max_age(86_400) // 24h ) .wrap(middleware::Logger::default()) .wrap(middleware::Compress::default()) diff --git a/meilisearch-http/src/main.rs b/meilisearch-http/src/main.rs index eab7f4ac6..1163c4f24 100644 --- a/meilisearch-http/src/main.rs +++ b/meilisearch-http/src/main.rs @@ -79,6 +79,7 @@ async fn main() -> Result<(), MainError> { Ok(()) } +#[allow(unused_variables)] async fn run_http( data: Data, opt: Opt,