rest-server: make all ApiConfig methods builder-style

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2023-01-26 09:32:47 +01:00
parent e2ac53e3de
commit 310310c650

View File

@ -60,31 +60,31 @@ impl ApiConfig {
} }
/// Set the authentication handler. /// Set the authentication handler.
pub fn with_auth_handler(mut self, auth_handler: AuthHandler) -> Self { pub fn auth_handler(mut self, auth_handler: AuthHandler) -> Self {
self.auth_handler = Some(auth_handler); self.auth_handler = Some(auth_handler);
self self
} }
/// Set the authentication handler from a function. /// Set the authentication handler from a function.
pub fn with_auth_handler_func<Func>(self, func: Func) -> Self pub fn auth_handler_func<Func>(self, func: Func) -> Self
where where
Func: for<'a> Fn(&'a HeaderMap, &'a Method) -> CheckAuthFuture<'a> + Send + Sync + 'static, Func: for<'a> Fn(&'a HeaderMap, &'a Method) -> CheckAuthFuture<'a> + Send + Sync + 'static,
{ {
self.with_auth_handler(AuthHandler::from_fn(func)) self.auth_handler(AuthHandler::from_fn(func))
} }
/// Set the index handler. /// Set the index handler.
pub fn with_index_handler(mut self, index_handler: IndexHandler) -> Self { pub fn index_handler(mut self, index_handler: IndexHandler) -> Self {
self.index_handler = Some(index_handler); self.index_handler = Some(index_handler);
self self
} }
/// Set the index handler from a function. /// Set the index handler from a function.
pub fn with_index_handler_func<Func>(self, func: Func) -> Self pub fn index_handler_func<Func>(self, func: Func) -> Self
where where
Func: Fn(RestEnvironment, Parts) -> IndexFuture + Send + Sync + 'static, Func: Fn(RestEnvironment, Parts) -> IndexFuture + Send + Sync + 'static,
{ {
self.with_index_handler(IndexHandler::from_fn(func)) self.index_handler(IndexHandler::from_fn(func))
} }
pub(crate) async fn get_index( pub(crate) async fn get_index(
@ -133,16 +133,17 @@ impl ApiConfig {
/// ``` /// ```
/// use proxmox_rest_server::ApiConfig; /// use proxmox_rest_server::ApiConfig;
/// // let mut config = ApiConfig::new(...); /// // let mut config = ApiConfig::new(...);
/// # fn fake(config: &mut ApiConfig) { /// # fn fake(config: ApiConfig) {
/// config.add_alias("extjs", "/usr/share/javascript/extjs"); /// config.alias("extjs", "/usr/share/javascript/extjs");
/// # } /// # }
/// ``` /// ```
pub fn add_alias<S, P>(&mut self, alias: S, path: P) pub fn alias<S, P>(mut self, alias: S, path: P) -> Self
where where
S: Into<String>, S: Into<String>,
P: Into<PathBuf>, P: Into<PathBuf>,
{ {
self.aliases.insert(alias.into(), path.into()); self.aliases.insert(alias.into(), path.into());
self
} }
pub(crate) fn env_type(&self) -> RpcEnvironmentType { pub(crate) fn env_type(&self) -> RpcEnvironmentType {
@ -153,11 +154,12 @@ impl ApiConfig {
/// ///
/// Those templates cane be use with [render_template](Self::render_template) to generate pages. /// Those templates cane be use with [render_template](Self::render_template) to generate pages.
#[cfg(feature = "templates")] #[cfg(feature = "templates")]
pub fn register_template<P>(&self, name: &str, path: P) -> Result<(), Error> pub fn register_template<P>(self, name: &str, path: P) -> Result<Self, Error>
where where
P: Into<PathBuf>, P: Into<PathBuf>,
{ {
self.templates.register(name, path) self.templates.register(name, path)?;
Ok(self)
} }
/// Checks if the template was modified since the last rendering /// Checks if the template was modified since the last rendering
@ -176,12 +178,12 @@ impl ApiConfig {
/// This function also registers a `api-access-log-reopen` /// This function also registers a `api-access-log-reopen`
/// command one the [CommandSocket]. /// command one the [CommandSocket].
pub fn enable_access_log<P>( pub fn enable_access_log<P>(
&mut self, mut self,
path: P, path: P,
dir_opts: Option<CreateOptions>, dir_opts: Option<CreateOptions>,
file_opts: Option<CreateOptions>, file_opts: Option<CreateOptions>,
commando_sock: &mut CommandSocket, commando_sock: &mut CommandSocket,
) -> Result<(), Error> ) -> Result<Self, Error>
where where
P: Into<PathBuf>, P: Into<PathBuf>,
{ {
@ -206,7 +208,7 @@ impl ApiConfig {
Ok(serde_json::Value::Null) Ok(serde_json::Value::Null)
})?; })?;
Ok(()) Ok(self)
} }
/// Enable the authentication log feature /// Enable the authentication log feature
@ -215,12 +217,12 @@ impl ApiConfig {
/// specified file. This function also registers a /// specified file. This function also registers a
/// `api-auth-log-reopen` command one the [CommandSocket]. /// `api-auth-log-reopen` command one the [CommandSocket].
pub fn enable_auth_log<P>( pub fn enable_auth_log<P>(
&mut self, mut self,
path: P, path: P,
dir_opts: Option<CreateOptions>, dir_opts: Option<CreateOptions>,
file_opts: Option<CreateOptions>, file_opts: Option<CreateOptions>,
commando_sock: &mut CommandSocket, commando_sock: &mut CommandSocket,
) -> Result<(), Error> ) -> Result<Self, Error>
where where
P: Into<PathBuf>, P: Into<PathBuf>,
{ {
@ -246,7 +248,7 @@ impl ApiConfig {
Ok(serde_json::Value::Null) Ok(serde_json::Value::Null)
})?; })?;
Ok(()) Ok(self)
} }
pub(crate) fn get_access_log(&self) -> Option<&Arc<Mutex<FileLogger>>> { pub(crate) fn get_access_log(&self) -> Option<&Arc<Mutex<FileLogger>>> {
@ -263,26 +265,26 @@ impl ApiConfig {
.find(|handler| path_components.strip_prefix(handler.prefix).is_some()) .find(|handler| path_components.strip_prefix(handler.prefix).is_some())
} }
pub fn add_default_api2_handler(&mut self, router: &'static Router) -> &mut Self { pub fn default_api2_handler(mut self, router: &'static Router) -> Self {
self.handlers.push(Handler::default_api2_handler(router)); self.handlers.push(Handler::default_api2_handler(router));
self self
} }
pub fn add_formatted_router( pub fn formatted_router(
&mut self, mut self,
prefix: &'static [&'static str], prefix: &'static [&'static str],
router: &'static Router, router: &'static Router,
) -> &mut Self { ) -> Self {
self.handlers self.handlers
.push(Handler::formatted_router(prefix, router)); .push(Handler::formatted_router(prefix, router));
self self
} }
pub fn add_unformatted_router( pub fn unformatted_router(
&mut self, mut self,
prefix: &'static [&'static str], prefix: &'static [&'static str],
router: &'static Router, router: &'static Router,
) -> &mut Self { ) -> Self {
self.handlers self.handlers
.push(Handler::unformatted_router(prefix, router)); .push(Handler::unformatted_router(prefix, router));
self self