upend/cli/src/serve.rs

83 lines
2.8 KiB
Rust

use crate::routes;
use std::path::Path;
pub fn get_routes() {}
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()
.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::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)
.service(routes::vault_stats)
.service(routes::store_stats)
.service(routes::get_jobs)
.service(routes::get_info)
.service(routes::get_user_entries);
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)
}