chore: migrate from OnceCell to Arc<State>
This commit is contained in:
		
							
								
								
									
										16
									
								
								src/api.rs
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								src/api.rs
									
									
									
									
									
								
							@@ -6,12 +6,15 @@ use axum::Json;
 | 
				
			|||||||
use serde::{Deserialize,Serialize};
 | 
					use serde::{Deserialize,Serialize};
 | 
				
			||||||
use crate::*;
 | 
					use crate::*;
 | 
				
			||||||
use crate::PowerDnsOidcTsigkeyError;
 | 
					use crate::PowerDnsOidcTsigkeyError;
 | 
				
			||||||
 | 
					use url::Url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub async fn list_keys(
 | 
					pub async fn list_keys(
 | 
				
			||||||
    State(state):  State<Arc<AppState>>,
 | 
					    State(state):  State<Arc<AppState>>,
 | 
				
			||||||
) -> Result<Json<Vec<TsigKey>>, PowerDnsOidcTsigkeyError> {
 | 
					) -> Result<Json<Vec<TsigKey>>, PowerDnsOidcTsigkeyError> {
 | 
				
			||||||
    let req = state.http_client.get::<String>((config_cell.get().unwrap().powerdns.url.to_string() + "/servers/localhost/tsigkeys").into())
 | 
					    let req = state.http_client.get::<String>(
 | 
				
			||||||
        .header("X-API-Key", config_cell.get().unwrap().powerdns.api_token.clone());
 | 
					            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
 | 
					    let response = req
 | 
				
			||||||
        .send()
 | 
					        .send()
 | 
				
			||||||
        .await?;
 | 
					        .await?;
 | 
				
			||||||
@@ -30,9 +33,11 @@ pub async fn list_key(
 | 
				
			|||||||
) -> PowerDnsOidcTsigkeyResult<Json<TsigKey>> {
 | 
					) -> PowerDnsOidcTsigkeyResult<Json<TsigKey>> {
 | 
				
			||||||
    let key: TsigKey = parse_json::<PowerDnsTsigKey>(
 | 
					    let key: TsigKey = parse_json::<PowerDnsTsigKey>(
 | 
				
			||||||
        state.http_client.get::<String>(
 | 
					        state.http_client.get::<String>(
 | 
				
			||||||
            (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()
 | 
					        .send()
 | 
				
			||||||
        .await?
 | 
					        .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)]
 | 
					#[derive(Serialize, Debug)]
 | 
				
			||||||
pub struct TsigKeyList {
 | 
					pub struct TsigKeyList {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										15
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -7,8 +7,6 @@ use std::{
 | 
				
			|||||||
        fmt::Display,
 | 
					        fmt::Display,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use tokio::sync::OnceCell;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
use axum::{
 | 
					use axum::{
 | 
				
			||||||
    routing::{get},
 | 
					    routing::{get},
 | 
				
			||||||
    Router,
 | 
					    Router,
 | 
				
			||||||
@@ -125,24 +123,23 @@ async fn parse_json<Out: DeserializeOwned>(res: ReqwestResponse) -> PowerDnsOidc
 | 
				
			|||||||
#[derive(Clone, Debug)]
 | 
					#[derive(Clone, Debug)]
 | 
				
			||||||
pub struct AppState {
 | 
					pub struct AppState {
 | 
				
			||||||
    http_client: Client,
 | 
					    http_client: Client,
 | 
				
			||||||
 | 
					    config: PowerDnsOidcTsigkeyConfig,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static config_cell: OnceCell<PowerDnsOidcTsigkeyConfig> = OnceCell::const_new();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[tokio::main]
 | 
					#[tokio::main]
 | 
				
			||||||
async fn main() {
 | 
					async fn main() {
 | 
				
			||||||
    match settings::PowerDnsOidcTsigkeyConfig::load("config.yaml") {
 | 
					    match settings::PowerDnsOidcTsigkeyConfig::load("config.yaml") {
 | 
				
			||||||
        Ok(config) => {
 | 
					        Ok(config) => {
 | 
				
			||||||
            config_cell.set(config).unwrap();
 | 
					            println!("Configuration loaded!");
 | 
				
			||||||
            run().await;
 | 
					            run(config).await;
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        Err(e) => println!("Failed to load config.yaml: {:?}", e),
 | 
					        Err(e) => println!("Failed to load config.yaml: {:?}", e),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn run() {
 | 
					async fn run(config: PowerDnsOidcTsigkeyConfig) {
 | 
				
			||||||
    let addr: SocketAddr = (config_cell.get().unwrap().server.bind_address, config_cell.get().unwrap().server.port).into();
 | 
					    let addr: SocketAddr = (config.server.bind_address, config.server.port).into();
 | 
				
			||||||
    let state = AppState { http_client: reqwest::Client::new() };
 | 
					    let state = AppState { http_client: reqwest::Client::new(), config: config.clone() };
 | 
				
			||||||
//    let router = create_router(state);
 | 
					//    let router = create_router(state);
 | 
				
			||||||
    let auth: JwtAuthorizer = JwtAuthorizer::from_oidc(&config_cell.get().unwrap().oidc.issuer.clone().to_string())
 | 
					    let auth: JwtAuthorizer = JwtAuthorizer::from_oidc(&config_cell.get().unwrap().oidc.issuer.clone().to_string())
 | 
				
			||||||
        .validation(Validation::new()
 | 
					        .validation(Validation::new()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,7 +4,7 @@ use url::Url;
 | 
				
			|||||||
use serde::{Deserialize};
 | 
					use serde::{Deserialize};
 | 
				
			||||||
use config::{Config, ConfigError, Environment, File};
 | 
					use config::{Config, ConfigError, Environment, File};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Deserialize)]
 | 
					#[derive(Debug, Deserialize, Clone)]
 | 
				
			||||||
pub struct PowerDnsOidcTsigkeyConfig {
 | 
					pub struct PowerDnsOidcTsigkeyConfig {
 | 
				
			||||||
    /// OIDC Provider
 | 
					    /// OIDC Provider
 | 
				
			||||||
    pub oidc: OidcConfig,
 | 
					    pub oidc: OidcConfig,
 | 
				
			||||||
@@ -16,7 +16,7 @@ pub struct PowerDnsOidcTsigkeyConfig {
 | 
				
			|||||||
    pub server: ServerConfig,
 | 
					    pub server: ServerConfig,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Deserialize)]
 | 
					#[derive(Debug, Deserialize, Clone)]
 | 
				
			||||||
pub struct PowerDnsConfig {
 | 
					pub struct PowerDnsConfig {
 | 
				
			||||||
    /// URL where PowerDNS API can be reached
 | 
					    /// URL where PowerDNS API can be reached
 | 
				
			||||||
    pub url: Url,
 | 
					    pub url: Url,
 | 
				
			||||||
@@ -24,13 +24,13 @@ pub struct PowerDnsConfig {
 | 
				
			|||||||
    pub api_token: String,
 | 
					    pub api_token: String,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Deserialize)]
 | 
					#[derive(Debug, Deserialize, Clone)]
 | 
				
			||||||
pub struct LogConfig {
 | 
					pub struct LogConfig {
 | 
				
			||||||
    /// The log level
 | 
					    /// The log level
 | 
				
			||||||
    pub level: String,
 | 
					    pub level: String,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Deserialize)]
 | 
					#[derive(Debug, Deserialize, Clone)]
 | 
				
			||||||
pub struct ServerConfig {
 | 
					pub struct ServerConfig {
 | 
				
			||||||
    /// IpAddress to listen on
 | 
					    /// IpAddress to listen on
 | 
				
			||||||
    pub bind_address: IpAddr,
 | 
					    pub bind_address: IpAddr,
 | 
				
			||||||
@@ -40,12 +40,12 @@ pub struct ServerConfig {
 | 
				
			|||||||
    pub tls: ServerTlsConfig,
 | 
					    pub tls: ServerTlsConfig,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Deserialize)]
 | 
					#[derive(Debug, Deserialize, Clone)]
 | 
				
			||||||
pub struct ServerTlsConfig {
 | 
					pub struct ServerTlsConfig {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Deserialize)]
 | 
					#[derive(Debug, Deserialize, Clone)]
 | 
				
			||||||
pub struct OidcConfig {
 | 
					pub struct OidcConfig {
 | 
				
			||||||
    pub issuer: Url,
 | 
					    pub issuer: Url,
 | 
				
			||||||
    pub client_id: String,
 | 
					    pub client_id: String,
 | 
				
			||||||
@@ -55,7 +55,7 @@ pub struct OidcConfig {
 | 
				
			|||||||
    pub validation: OidcValidationConfig,
 | 
					    pub validation: OidcValidationConfig,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Deserialize)]
 | 
					#[derive(Debug, Deserialize, Clone)]
 | 
				
			||||||
pub struct OidcValidationConfig {
 | 
					pub struct OidcValidationConfig {
 | 
				
			||||||
    pub issuer: Vec<Url>,
 | 
					    pub issuer: Vec<Url>,
 | 
				
			||||||
    pub audience: Vec<String>,
 | 
					    pub audience: Vec<String>,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user