serve static site

This commit is contained in:
Marin Postma 2021-04-21 13:49:21 +02:00
parent bb79695e44
commit 6bcf20c70e
No known key found for this signature in database
GPG Key ID: D5241F0C0C865F30
7 changed files with 114 additions and 36 deletions

90
Cargo.lock generated
View File

@ -19,8 +19,7 @@ dependencies = [
[[package]] [[package]]
name = "actix-cors" name = "actix-cors"
version = "0.6.0-beta.1" version = "0.6.0-beta.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/MarinPostma/actix-extras.git?rev=2dac1a4#2dac1a421619bf7b386dea63d3ae25a3bc4abc43"
checksum = "aa50c395d15e5946cf04bccb1edef583e9fd42aa6710a1f89a725c5e2c4c5503"
dependencies = [ dependencies = [
"actix-service", "actix-service",
"actix-web", "actix-web",
@ -33,9 +32,9 @@ dependencies = [
[[package]] [[package]]
name = "actix-http" 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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb9c5d7ceb490d6565156ae1d4d467db17da1759425c65a2e36ac5e182e014e2" checksum = "59d51c2ba06062e698a5d212d860e9fb2afc931c285ede687aaae896c8150347"
dependencies = [ dependencies = [
"actix-codec", "actix-codec",
"actix-rt", "actix-rt",
@ -48,7 +47,6 @@ dependencies = [
"brotli2", "brotli2",
"bytes 1.0.1", "bytes 1.0.1",
"bytestring", "bytestring",
"cookie",
"derive_more", "derive_more",
"encoding_rs", "encoding_rs",
"flate2", "flate2",
@ -63,14 +61,13 @@ dependencies = [
"log", "log",
"mime", "mime",
"once_cell", "once_cell",
"paste",
"percent-encoding", "percent-encoding",
"pin-project", "pin-project",
"pin-project-lite 0.2.6", "pin-project-lite 0.2.6",
"rand 0.8.3", "rand 0.8.3",
"regex", "regex",
"serde", "serde",
"serde_json",
"serde_urlencoded",
"sha-1 0.9.4", "sha-1 0.9.4",
"smallvec", "smallvec",
"time 0.2.26", "time 0.2.26",
@ -113,9 +110,9 @@ dependencies = [
[[package]] [[package]]
name = "actix-server" 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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0872f02a1871257ef09c5a269dce5dc5fea5ccf502adbf5d39f118913b61411c" checksum = "26369215fcc3b0176018b3b68756a8bcc275bb000e6212e454944913a1f9bf87"
dependencies = [ dependencies = [
"actix-rt", "actix-rt",
"actix-service", "actix-service",
@ -130,11 +127,12 @@ dependencies = [
[[package]] [[package]]
name = "actix-service" name = "actix-service"
version = "2.0.0-beta.5" version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf82340ad9f4e4caf43737fd3bbc999778a268015cdc54675f60af6240bd2b05" checksum = "77f5f9d66a8730d0fae62c26f3424f5751e5518086628a40b7ab6fca4a705034"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"paste",
"pin-project-lite 0.2.6", "pin-project-lite 0.2.6",
] ]
@ -169,9 +167,9 @@ dependencies = [
[[package]] [[package]]
name = "actix-web" 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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6de19cc341c2e68b1ee126de171e86b610b5bbcecc660d1250ebed73e0257bd6" checksum = "ff12e933051557d700b0fcad20fe25b9ca38395cc87bbc5aeaddaef17b937a2f"
dependencies = [ dependencies = [
"actix-codec", "actix-codec",
"actix-http", "actix-http",
@ -185,11 +183,13 @@ dependencies = [
"actix-web-codegen", "actix-web-codegen",
"ahash 0.7.2", "ahash 0.7.2",
"bytes 1.0.1", "bytes 1.0.1",
"cookie",
"derive_more", "derive_more",
"either", "either",
"encoding_rs", "encoding_rs",
"futures-core", "futures-core",
"futures-util", "futures-util",
"itoa",
"language-tags", "language-tags",
"log", "log",
"mime", "mime",
@ -216,6 +216,20 @@ dependencies = [
"syn 1.0.70", "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]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.14.1" version = "0.14.1"
@ -583,6 +597,16 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 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]] [[package]]
name = "character_converter" name = "character_converter"
version = "1.0.0" version = "1.0.0"
@ -635,9 +659,9 @@ checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]] [[package]]
name = "cookie" name = "cookie"
version = "0.14.4" version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03a5d7b21829bc7b4bf4754a978a241ae54ea55a40f92bb20216e54096f4b951" checksum = "ffdf8865bac3d9a3bde5bde9088ca431b11f5d37c7a578b8086af77248b76627"
dependencies = [ dependencies = [
"percent-encoding", "percent-encoding",
"time 0.2.26", "time 0.2.26",
@ -1329,6 +1353,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
[[package]]
name = "httpdate"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05842d0d43232b23ccb7060ecb0f0626922c21f30012e97b767b30afd4a5d4b9"
[[package]] [[package]]
name = "human_format" name = "human_format"
version = "1.0.3" version = "1.0.3"
@ -1364,7 +1394,7 @@ dependencies = [
"http", "http",
"http-body 0.3.1", "http-body 0.3.1",
"httparse", "httparse",
"httpdate", "httpdate 0.3.2",
"itoa", "itoa",
"pin-project", "pin-project",
"socket2 0.3.19", "socket2 0.3.19",
@ -1376,9 +1406,9 @@ dependencies = [
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.14.5" version = "0.14.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bf09f61b52cfcf4c00de50df88ae423d6c02354e385a86341133b5338630ad1" checksum = "5f006b8784cfb01fe7aa9c46f5f5cd4cf5c85a8c612a0653ec97642979062665"
dependencies = [ dependencies = [
"bytes 1.0.1", "bytes 1.0.1",
"futures-channel", "futures-channel",
@ -1388,7 +1418,7 @@ dependencies = [
"http", "http",
"http-body 0.4.1", "http-body 0.4.1",
"httparse", "httparse",
"httpdate", "httpdate 1.0.0",
"itoa", "itoa",
"pin-project", "pin-project",
"socket2 0.4.0", "socket2 0.4.0",
@ -1421,7 +1451,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f9f7a97316d44c0af9b0301e65010573a853a9fc97046d7331d7f6bc0fd5a64" checksum = "5f9f7a97316d44c0af9b0301e65010573a853a9fc97046d7331d7f6bc0fd5a64"
dependencies = [ dependencies = [
"futures-util", "futures-util",
"hyper 0.14.5", "hyper 0.14.6",
"log", "log",
"rustls 0.19.1", "rustls 0.19.1",
"tokio 1.5.0", "tokio 1.5.0",
@ -1719,6 +1749,7 @@ dependencies = [
"actix-rt", "actix-rt",
"actix-service", "actix-service",
"actix-web", "actix-web",
"actix-web-static-files",
"anyhow", "anyhow",
"assert-json-diff", "assert-json-diff",
"async-compression", "async-compression",
@ -2126,6 +2157,21 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58" 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]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.1.0" version = "2.1.0"
@ -2634,7 +2680,7 @@ dependencies = [
"futures-util", "futures-util",
"http", "http",
"http-body 0.4.1", "http-body 0.4.1",
"hyper 0.14.5", "hyper 0.14.6",
"hyper-rustls 0.22.1", "hyper-rustls 0.22.1",
"ipnet", "ipnet",
"js-sys", "js-sys",
@ -2812,7 +2858,7 @@ dependencies = [
"env_logger 0.7.1", "env_logger 0.7.1",
"failure", "failure",
"hostname", "hostname",
"httpdate", "httpdate 0.3.2",
"im", "im",
"lazy_static", "lazy_static",
"libc", "libc",

View File

@ -5,4 +5,4 @@ authors = ["marin <postma.marin@protonmail.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
actix-http = "=3.0.0-beta.5" actix-http = "=3.0.0-beta.6"

View File

@ -5,11 +5,14 @@ edition = "2018"
license = "MIT" license = "MIT"
name = "meilisearch-http" name = "meilisearch-http"
version = "0.21.0-alpha.3" version = "0.21.0-alpha.3"
build = "build.rs"
[[bin]] [[bin]]
name = "meilisearch" name = "meilisearch"
path = "src/main.rs" path = "src/main.rs"
[build-dependencies] [build-dependencies]
actix-web-static-files = { git = "https://github.com/MarinPostma/actix-web-static-files.git", branch = "actix-web-4" }
anyhow = "*" anyhow = "*"
cargo_toml = "0.9.0" cargo_toml = "0.9.0"
hex = "0.4.3" hex = "0.4.3"
@ -19,10 +22,11 @@ vergen = "3.1.0"
zip = "0.5.12" zip = "0.5.12"
[dependencies] [dependencies]
actix-cors = "0.6.0-beta.1" actix-cors = { git = "https://github.com/MarinPostma/actix-extras.git", rev = "2dac1a4"}
actix-http = { version = "=3.0.0-beta.5" } actix-http = { version = "=3.0.0-beta.6" }
actix-service = "=2.0.0-beta.5" actix-service = "2.0.0"
actix-web = { version = "=4.0.0-beta.5", features = ["rustls"] } 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" anyhow = "1.0.36"
async-compression = { version = "0.3.6", features = ["gzip", "tokio-02"] } async-compression = { version = "0.3.6", features = ["gzip", "tokio-02"] }
async-stream = "0.3.0" 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" } milli = { git = "https://github.com/meilisearch/milli.git", tag = "v0.1.1" }
mime = "0.3.16" mime = "0.3.16"
once_cell = "1.5.2" once_cell = "1.5.2"
oxidized-json-checker = "0.3.2"
parking_lot = "0.11.1" parking_lot = "0.11.1"
rand = "0.7.3" rand = "0.7.3"
rayon = "1.5.0" rayon = "1.5.0"
@ -67,7 +72,6 @@ tempfile = "3.1.0"
thiserror = "1.0.24" thiserror = "1.0.24"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
uuid = "0.8.2" uuid = "0.8.2"
oxidized-json-checker = "0.3.2"
walkdir = "2.3.2" walkdir = "2.3.2"
obkv = "0.1.1" obkv = "0.1.1"
@ -97,7 +101,7 @@ tempdir = "0.3.7"
urlencoding = "1.1.1" urlencoding = "1.1.1"
[features] [features]
mini-dashboard = ["default"] mini-dashboard = ["default", "actix-web-static-files"]
default = ["sentry"] default = ["sentry"]
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]

View File

@ -6,6 +6,7 @@ use std::path::PathBuf;
use anyhow::Context; use anyhow::Context;
use sha1::{Sha1, Digest}; use sha1::{Sha1, Digest};
use reqwest::blocking::get; use reqwest::blocking::get;
use actix_web_static_files::resource_dir;
use vergen::{generate_cargo_keys, ConstantsFlags}; use vergen::{generate_cargo_keys, ConstantsFlags};
use cargo_toml::Manifest; use cargo_toml::Manifest;
@ -54,5 +55,6 @@ fn setup_mini_dashboard() -> anyhow::Result<()> {
let cursor = Cursor::new(&dashboard_assets_bytes); let cursor = Cursor::new(&dashboard_assets_bytes);
let mut zip = zip::read::ZipArchive::new(cursor)?; let mut zip = zip::read::ZipArchive::new(cursor)?;
zip.extract(&dashboard_dir)?; zip.extract(&dashboard_dir)?;
resource_dir(&dashboard_dir).build()?;
Ok(()) Ok(())
} }

View File

@ -2,7 +2,8 @@ use std::error;
use std::fmt; use std::fmt;
use actix_web as aweb; 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::error::{JsonPayloadError, QueryPayloadError};
use actix_web::http::Error as HttpError; use actix_web::http::Error as HttpError;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
@ -71,8 +72,9 @@ impl Serialize for ResponseError {
} }
impl aweb::error::ResponseError for ResponseError { impl aweb::error::ResponseError for ResponseError {
fn error_response(&self) -> aweb::HttpResponse { fn error_response(&self) -> aweb::BaseHttpResponse<Body> {
HttpResponseBuilder::new(self.status_code()).json(&self) let json = serde_json::to_vec(self).unwrap();
BaseHttpResponseBuilder::new(self.status_code()).body(json)
} }
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {
@ -297,6 +299,7 @@ impl From<JsonPayloadError> for Error {
JsonPayloadError::Payload(err) => { JsonPayloadError::Payload(err) => {
Error::BadRequest(format!("Problem while decoding the request: {}", err)) Error::BadRequest(format!("Problem while decoding the request: {}", err))
} }
e => Error::Internal(format!("Unexpected Json error: {}", e))
} }
} }
} }
@ -307,6 +310,7 @@ impl From<QueryPayloadError> for Error {
QueryPayloadError::Deserialize(err) => { QueryPayloadError::Deserialize(err) => {
Error::BadRequest(format!("Invalid query parameters: {}", err)) Error::BadRequest(format!("Invalid query parameters: {}", err))
} }
e => Error::Internal(format!("Unexpected query payload error: {}", e))
} }
} }
} }

View File

@ -9,6 +9,7 @@ pub mod routes;
pub use self::data::Data; pub use self::data::Data;
pub use option::Opt; pub use option::Opt;
#[macro_export] #[macro_export]
macro_rules! create_app { macro_rules! create_app {
($data:expr, $enable_frontend:expr) => {{ ($data:expr, $enable_frontend:expr) => {{
@ -19,6 +20,14 @@ macro_rules! create_app {
use meilisearch_http::error::payload_error_handler; use meilisearch_http::error::payload_error_handler;
use meilisearch_http::routes::*; 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() let app = App::new()
.data($data.clone()) .data($data.clone())
.app_data( .app_data(
@ -40,16 +49,28 @@ macro_rules! create_app {
.configure(stats::services) .configure(stats::services)
.configure(key::services); .configure(key::services);
//.configure(routes::dump::services); //.configure(routes::dump::services);
#[cfg(feature = "mini-dashboard")]
let app = if $enable_frontend { let app = if $enable_frontend {
app.service(load_html).service(load_css) let generated = dashboard::generate();
let keys = generated.keys().collect::<Vec<_>>();
println!("served files {:?}", keys);
let service = ResourceFiles::new("/", generated);
app.service(service)
} else { } else {
app.service(running) app.service(running)
}; };
#[cfg(not(feature = "mini-dashboard"))]
let app = app.service(running);
println!("here\n\n\n");
app.wrap( app.wrap(
Cors::default() Cors::default()
.send_wildcard() .send_wildcard()
.allowed_headers(vec!["content-type", "x-meili-api-key"]) .allowed_headers(vec!["content-type", "x-meili-api-key"])
.max_age(86_400), // 24h .allow_any_origin()
.allow_any_method()
.max_age(86_400) // 24h
) )
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.wrap(middleware::Compress::default()) .wrap(middleware::Compress::default())

View File

@ -79,6 +79,7 @@ async fn main() -> Result<(), MainError> {
Ok(()) Ok(())
} }
#[allow(unused_variables)]
async fn run_http( async fn run_http(
data: Data, data: Data,
opt: Opt, opt: Opt,