upend/cli/src/serve.rs

88 lines
3.0 KiB
Rust

use crate::routes;
use actix_web_lab::web::spa;
pub fn get_app<S>(
ui_enabled: bool,
allowed_origins: S,
state: crate::routes::State,
) -> actix_web::App<
impl actix_web::dev::ServiceFactory<
actix_web::dev::ServiceRequest,
Response = actix_web::dev::ServiceResponse<impl actix_web::body::MessageBody>,
Config = (),
InitError = (),
Error = actix_web::Error,
>,
>
where
S: IntoIterator<Item = String> + Clone,
{
let allowed_origins: Vec<String> = allowed_origins.into_iter().collect();
let cors = actix_cors::Cors::default()
.allowed_origin("http://localhost")
.allowed_origin("http://127.0.0.1")
.allowed_origin_fn(|origin, _req_head| {
origin.as_bytes().starts_with(b"http://localhost:")
|| origin.as_bytes().starts_with(b"http://127.0.0.1:")
|| origin.as_bytes().starts_with(b"moz-extension://")
})
.allowed_origin_fn(move |origin, _req_head| {
allowed_origins
.clone()
.into_iter()
.any(|allowed_origin| allowed_origin == "*" || *origin == allowed_origin)
})
.allowed_header("content-type")
.allow_any_method();
let app = actix_web::App::new()
.wrap(cors)
.wrap(
actix_web::middleware::DefaultHeaders::new()
.add(("UPEND-VERSION", crate::common::build::PKG_VERSION)),
)
.app_data(actix_web::web::PayloadConfig::new(4_294_967_296))
.app_data(actix_web::web::Data::new(state))
.wrap(actix_web::middleware::Logger::default().exclude("/api/jobs"))
.service(routes::login)
.service(routes::register)
.service(routes::get_raw)
.service(routes::head_raw)
.service(routes::get_thumbnail)
.service(routes::get_query)
.service(routes::get_object)
.service(routes::put_object)
.service(routes::put_blob)
.service(routes::put_object_attribute)
.service(routes::delete_object)
.service(routes::get_address)
.service(routes::get_all_attributes)
.service(routes::api_refresh)
.service(routes::list_hier)
.service(routes::list_hier_roots)
.service(routes::vault_stats)
.service(routes::store_stats)
.service(routes::get_jobs)
.service(routes::get_info)
.service(routes::get_options)
.service(routes::put_options)
.service(routes::get_user_entries);
if ui_enabled {
return app.service(
spa()
.index_file(crate::common::WEBUI_PATH.to_str().unwrap().to_owned() + "/index.html")
.static_resources_location(crate::common::WEBUI_PATH.to_str().unwrap())
.finish(),
);
}
#[actix_web::get("/")]
async fn unavailable_index() -> actix_web::HttpResponse {
actix_web::HttpResponse::ServiceUnavailable().body("Web UI not enabled.")
}
app.service(unavailable_index)
}