warp_api.rs 14.99 KiB
use crate::api_model::BulkAction;
use crate::api_model::CreateItem;
use crate::api_model::GetFile;
use crate::api_model::InsertTreeItem;
use crate::api_model::PayloadWrapper;
use crate::api_model::RunDownloader;
use crate::api_model::RunImporter;
use crate::api_model::RunIndexer;
use crate::api_model::SearchByFields;
use crate::api_model::UpdateItem;
use crate::command_line_interface::CLIOptions;
use crate::internal_api;
use crate::warp_endpoints;
use bytes::Bytes;
use log::error;
use log::info;
use log::warn;
use std::collections::HashSet;
use std::net::IpAddr;
use std::net::SocketAddr;
use std::ops::Deref;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::RwLock;
use warp::http;
use warp::http::header::HeaderMap;
use warp::http::header::HeaderValue;
use warp::http::status::StatusCode;
use warp::reply::Response;
use warp::Filter;
use warp::Reply;
/// Start web framework with specified APIs.
pub async fn run_server(cli_options: &CLIOptions) {
    let package_name = env!("CARGO_PKG_NAME").to_uppercase();
    info!("Starting {} HTTP server", package_name);
    let mut headers = HeaderMap::new();
    if cli_options.insecure_http_headers {
        info!("Adding insecure http headers Access-Control-Allow-Origin header as per CLI option");
        headers.insert("Access-Control-Allow-Origin", HeaderValue::from_static("*"));
    let headers = warp::reply::with::headers(headers);
    let items_api = warp::path("v2")
        .and(warp::body::content_length_limit(5 * 1024 * 1024))
        .and(warp::post());
    let services_api = warp::path("v2")
        .and(warp::body::content_length_limit(32 * 1024))
        .and(warp::post());
    let file_api = warp::path("v2")
        .and(warp::body::content_length_limit(500 * 1024 * 1024))
        .and(warp::post());
    let initialized_databases_arc = Arc::new(RwLock::new(HashSet::<String>::new()));
    let version = warp::path("version")
        .and(warp::path::end())
        .and(warp::get())
        .map(internal_api::get_project_version);
    let init_db = initialized_databases_arc.clone();
    let get_item = items_api
        .and(warp::path!(String / "get_item"))
        .and(warp::path::end())
        .and(warp::body::json())
        .map(move |owner: String, body: PayloadWrapper<i64>| {
            let result = warp_endpoints::get_item(owner, init_db.deref(), body);
            let result = result.map(|result| warp::reply::json(&result));
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
respond_with_result(result) }); let init_db = initialized_databases_arc.clone(); let get_all_items = items_api .and(warp::path!(String / "get_all_items")) .and(warp::path::end()) .and(warp::body::json()) .map(move |owner: String, body: PayloadWrapper<()>| { let result = warp_endpoints::get_all_items(owner, init_db.deref(), body); let result = result.map(|result| warp::reply::json(&result)); respond_with_result(result) }); let init_db = initialized_databases_arc.clone(); let create_item = items_api .and(warp::path!(String / "create_item")) .and(warp::path::end()) .and(warp::body::json()) .map(move |owner: String, body: PayloadWrapper<CreateItem>| { let result = warp_endpoints::create_item(owner, init_db.deref(), body); let result = result.map(|result| warp::reply::json(&result)); respond_with_result(result) }); let init_db = initialized_databases_arc.clone(); let update_item = items_api .and(warp::path!(String / "update_item")) .and(warp::path::end()) .and(warp::body::json()) .map(move |owner: String, body: PayloadWrapper<UpdateItem>| { let result = warp_endpoints::update_item(owner, init_db.deref(), body); let result = result.map(|()| warp::reply::json(&serde_json::json!({}))); respond_with_result(result) }); let init_db = initialized_databases_arc.clone(); let bulk_action = items_api .and(warp::path!(String / "bulk_action")) .and(warp::path::end()) .and(warp::body::json()) .map(move |owner: String, body: PayloadWrapper<BulkAction>| { let result = warp_endpoints::bulk_action(owner, init_db.deref(), body); let result = result.map(|()| warp::reply::json(&serde_json::json!({}))); respond_with_result(result) }); let init_db = initialized_databases_arc.clone(); let delete_item = items_api .and(warp::path!(String / "delete_item")) .and(warp::path::end()) .and(warp::body::json()) .map(move |owner: String, body: PayloadWrapper<i64>| { let result = warp_endpoints::delete_item(owner, init_db.deref(), body); let result = result.map(|()| warp::reply::json(&serde_json::json!({}))); respond_with_result(result) }); let init_db = initialized_databases_arc.clone(); let cli_options_arc = Arc::new(cli_options.clone()); let insert_tree = items_api .and(warp::path!(String / "insert_tree")) .and(warp::path::end()) .and(warp::body::json()) .map(move |owner: String, body: PayloadWrapper<InsertTreeItem>| { let shared_server = cli_options_arc.shared_server; let result = warp_endpoints::insert_tree(owner, init_db.deref(), body, shared_server); let result = result.map(|result| warp::reply::json(&result)); respond_with_result(result) });