httproxide 0.2.0

Rusted HTTP router reverse-proxy
Documentation
use http::StatusCode;
use serde::{Deserialize, Serialize};
use tower_http::services::fs::{ServeDir as TowerServeDir, ServeFile as TowerServeFile};

use crate::error_responders::get_error_responder;
use crate::target::IntoTarget;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct ServeDirConfig {
    path: String,
    #[serde(default)]
    append_index_html_on_directories: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct ServeFileConfig {
    path: String,
}

pub fn serve_dir_from_config(config: ServeDirConfig) -> anyhow::Result<impl IntoTarget> {
    Ok(TowerServeDir::new(config.path)
        .append_index_html_on_directories(config.append_index_html_on_directories)
        .not_found_service(
            get_error_responder(StatusCode::NOT_FOUND)
        ))
}

pub fn serve_file_from_config(config: ServeFileConfig) -> anyhow::Result<impl IntoTarget> {
    Ok(TowerServeFile::new(config.path))
}