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

View file

@ -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]

View file

@ -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(())
}

View file

@ -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<Body> {
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<JsonPayloadError> 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<QueryPayloadError> for Error {
QueryPayloadError::Deserialize(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 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::<Vec<_>>();
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())

View file

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