mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-03 20:07:09 +02:00
serve static site
This commit is contained in:
parent
bb79695e44
commit
6bcf20c70e
7 changed files with 114 additions and 36 deletions
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -79,6 +79,7 @@ async fn main() -> Result<(), MainError> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
async fn run_http(
|
||||
data: Data,
|
||||
opt: Opt,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue