forked from proxmox-mirrors/proxmox
notify: use std::sync::OnceCell instead of lazy_static!
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com> Tested-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
This commit is contained in:
parent
d0b1502803
commit
7035d57312
@ -11,7 +11,6 @@ exclude.workspace = true
|
||||
anyhow.workspace = true
|
||||
const_format.workspace = true
|
||||
handlebars = { workspace = true }
|
||||
lazy_static.workspace = true
|
||||
lettre = { workspace = true, optional = true }
|
||||
log.workspace = true
|
||||
mail-parser = { workspace = true, optional = true }
|
||||
|
@ -1,4 +1,4 @@
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use proxmox_schema::{ApiType, ObjectSchema};
|
||||
use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
|
||||
@ -9,9 +9,16 @@ use crate::matcher::{MatcherConfig, MATCHER_TYPENAME};
|
||||
use crate::schema::BACKEND_NAME_SCHEMA;
|
||||
use crate::Error;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref CONFIG: SectionConfig = config_init();
|
||||
pub static ref PRIVATE_CONFIG: SectionConfig = private_config_init();
|
||||
/// Section config schema for the public config file.
|
||||
pub fn config_parser() -> &'static SectionConfig {
|
||||
static CONFIG: OnceLock<SectionConfig> = OnceLock::new();
|
||||
CONFIG.get_or_init(|| config_init())
|
||||
}
|
||||
|
||||
/// Section config schema for the private config file.
|
||||
pub fn private_config_parser() -> &'static SectionConfig {
|
||||
static CONFIG: OnceLock<SectionConfig> = OnceLock::new();
|
||||
CONFIG.get_or_init(|| private_config_init())
|
||||
}
|
||||
|
||||
fn config_init() -> SectionConfig {
|
||||
@ -108,7 +115,7 @@ fn private_config_init() -> SectionConfig {
|
||||
|
||||
pub fn config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error> {
|
||||
let digest = openssl::sha::sha256(raw_config.as_bytes());
|
||||
let mut data = CONFIG
|
||||
let mut data = config_parser()
|
||||
.parse("notifications.cfg", raw_config)
|
||||
.map_err(|err| Error::ConfigDeserialization(err.into()))?;
|
||||
|
||||
@ -139,20 +146,20 @@ pub fn config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error>
|
||||
|
||||
pub fn private_config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error> {
|
||||
let digest = openssl::sha::sha256(raw_config.as_bytes());
|
||||
let data = PRIVATE_CONFIG
|
||||
let data = private_config_parser()
|
||||
.parse("priv/notifications.cfg", raw_config)
|
||||
.map_err(|err| Error::ConfigDeserialization(err.into()))?;
|
||||
Ok((data, digest))
|
||||
}
|
||||
|
||||
pub fn write(config: &SectionConfigData) -> Result<String, Error> {
|
||||
CONFIG
|
||||
config_parser()
|
||||
.write("notifications.cfg", config)
|
||||
.map_err(|err| Error::ConfigSerialization(err.into()))
|
||||
}
|
||||
|
||||
pub fn write_private(config: &SectionConfigData) -> Result<String, Error> {
|
||||
PRIVATE_CONFIG
|
||||
private_config_parser()
|
||||
.write("priv/notifications.cfg", config)
|
||||
.map_err(|err| Error::ConfigSerialization(err.into()))
|
||||
}
|
||||
|
@ -13,12 +13,11 @@ use proxmox_section_config::SectionConfigData;
|
||||
use proxmox_uuid::Uuid;
|
||||
|
||||
pub mod matcher;
|
||||
use crate::config::CONFIG;
|
||||
use matcher::{MatcherConfig, MATCHER_TYPENAME};
|
||||
|
||||
pub mod api;
|
||||
pub mod context;
|
||||
pub mod config;
|
||||
pub mod context;
|
||||
pub mod endpoints;
|
||||
pub mod filter;
|
||||
pub mod group;
|
||||
@ -280,7 +279,7 @@ impl Config {
|
||||
|
||||
let default_config = context().default_config();
|
||||
|
||||
let builtin_config = CONFIG
|
||||
let builtin_config = config::config_parser()
|
||||
.parse("<builtin>", default_config)
|
||||
.map_err(|err| Error::ConfigDeserialization(err.into()))?;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user