From a4bfa2bb8b4922e0ae8180e39ebe554de8c4ef91 Mon Sep 17 00:00:00 2001 From: Johanna Dorothea Reichmann Date: Sat, 1 Jul 2023 00:17:01 +0200 Subject: [PATCH] chore: migrate from OnceCell to Arc --- src/api.rs | 16 ++++++++++++---- src/main.rs | 15 ++++++--------- src/settings.rs | 14 +++++++------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/api.rs b/src/api.rs index 56d385d..977a9b9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -6,12 +6,15 @@ use axum::Json; use serde::{Deserialize,Serialize}; use crate::*; use crate::PowerDnsOidcTsigkeyError; +use url::Url; pub async fn list_keys( State(state): State>, ) -> Result>, PowerDnsOidcTsigkeyError> { - let req = state.http_client.get::((config_cell.get().unwrap().powerdns.url.to_string() + "/servers/localhost/tsigkeys").into()) - .header("X-API-Key", config_cell.get().unwrap().powerdns.api_token.clone()); + let req = state.http_client.get::( + get_url(state.config.powerdns.url.clone(), "localhost".to_owned(), format!("tsigkeys")) + ) + .header("X-API-Key", state.config.powerdns.api_token.clone()); let response = req .send() .await?; @@ -30,9 +33,11 @@ pub async fn list_key( ) -> PowerDnsOidcTsigkeyResult> { let key: TsigKey = parse_json::( state.http_client.get::( - (config_cell.get().unwrap().powerdns.url.to_string() + format!("/servers/localhost/tsigkeys/{}", key_id).as_str()).into() + get_url(state.config.powerdns.url.clone(), + "localhost".to_owned(), + format!("tsigkeys/{}", key_id)) ) - .header("X-API-Key", config_cell.get().unwrap().powerdns.api_token.clone()) + .header("X-API-Key", state.config.powerdns.api_token.clone()) .send() .await? ) @@ -57,6 +62,9 @@ pub async fn list_key( // //} +fn get_url(powerdns_url: Url, server: String, endpoint: String) -> String { + format!("{}/servers/{}/{}", powerdns_url.to_string(), server, endpoint).as_str().into() +} #[derive(Serialize, Debug)] pub struct TsigKeyList { diff --git a/src/main.rs b/src/main.rs index 1868c34..a10e551 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,6 @@ use std::{ fmt::Display, }; -use tokio::sync::OnceCell; - use axum::{ routing::{get}, Router, @@ -125,24 +123,23 @@ async fn parse_json(res: ReqwestResponse) -> PowerDnsOidc #[derive(Clone, Debug)] pub struct AppState { http_client: Client, + config: PowerDnsOidcTsigkeyConfig, } -static config_cell: OnceCell = OnceCell::const_new(); - #[tokio::main] async fn main() { match settings::PowerDnsOidcTsigkeyConfig::load("config.yaml") { Ok(config) => { - config_cell.set(config).unwrap(); - run().await; + println!("Configuration loaded!"); + run(config).await; }, Err(e) => println!("Failed to load config.yaml: {:?}", e), }; } -async fn run() { - let addr: SocketAddr = (config_cell.get().unwrap().server.bind_address, config_cell.get().unwrap().server.port).into(); - let state = AppState { http_client: reqwest::Client::new() }; +async fn run(config: PowerDnsOidcTsigkeyConfig) { + let addr: SocketAddr = (config.server.bind_address, config.server.port).into(); + let state = AppState { http_client: reqwest::Client::new(), config: config.clone() }; // let router = create_router(state); let auth: JwtAuthorizer = JwtAuthorizer::from_oidc(&config_cell.get().unwrap().oidc.issuer.clone().to_string()) .validation(Validation::new() diff --git a/src/settings.rs b/src/settings.rs index 2ca28b6..9b5205b 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -4,7 +4,7 @@ use url::Url; use serde::{Deserialize}; use config::{Config, ConfigError, Environment, File}; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct PowerDnsOidcTsigkeyConfig { /// OIDC Provider pub oidc: OidcConfig, @@ -16,7 +16,7 @@ pub struct PowerDnsOidcTsigkeyConfig { pub server: ServerConfig, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct PowerDnsConfig { /// URL where PowerDNS API can be reached pub url: Url, @@ -24,13 +24,13 @@ pub struct PowerDnsConfig { pub api_token: String, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct LogConfig { /// The log level pub level: String, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct ServerConfig { /// IpAddress to listen on pub bind_address: IpAddr, @@ -40,12 +40,12 @@ pub struct ServerConfig { pub tls: ServerTlsConfig, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct ServerTlsConfig { } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct OidcConfig { pub issuer: Url, pub client_id: String, @@ -55,7 +55,7 @@ pub struct OidcConfig { pub validation: OidcValidationConfig, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct OidcValidationConfig { pub issuer: Vec, pub audience: Vec,