2023-06-07 21:09:33 +02:00
|
|
|
use crate::routes;
|
2023-06-19 16:45:55 +02:00
|
|
|
use std::path::Path;
|
2023-06-07 21:09:33 +02:00
|
|
|
|
|
|
|
pub fn get_app<P, S>(
|
|
|
|
ui_path: Option<P>,
|
|
|
|
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
|
|
|
|
P: AsRef<Path> + Clone,
|
|
|
|
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()
|
2023-06-25 15:29:52 +02:00
|
|
|
.add(("UPEND-VERSION", crate::common::build::PKG_VERSION)),
|
2023-06-07 21:09:33 +02:00
|
|
|
)
|
|
|
|
.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::get_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)
|
2023-06-19 16:45:55 +02:00
|
|
|
.service(routes::vault_stats)
|
|
|
|
.service(routes::store_stats)
|
2023-06-07 21:09:33 +02:00
|
|
|
.service(routes::get_jobs)
|
2023-07-03 17:18:55 +02:00
|
|
|
.service(routes::get_info)
|
|
|
|
.service(routes::get_user_entries);
|
2023-06-07 21:09:33 +02:00
|
|
|
|
|
|
|
if let Some(ui_path) = ui_path {
|
|
|
|
return app
|
|
|
|
.service(actix_files::Files::new("/", ui_path.as_ref()).index_file("index.html"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::get("/")]
|
|
|
|
async fn unavailable_index() -> actix_web::HttpResponse {
|
|
|
|
actix_web::HttpResponse::ServiceUnavailable().body("Web UI not enabled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
app.service(unavailable_index)
|
|
|
|
}
|