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 crate::*;
 | 
			
		||||
use crate::PowerDnsOidcTsigkeyError;
 | 
			
		||||
use url::Url;
 | 
			
		||||
 | 
			
		||||
pub async fn list_keys(
 | 
			
		||||
    State(state):  State<Arc<AppState>>,
 | 
			
		||||
) -> Result<Json<Vec<TsigKey>>, PowerDnsOidcTsigkeyError> {
 | 
			
		||||
    let req = state.http_client.get::<String>((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::<String>(
 | 
			
		||||
            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<Json<TsigKey>> {
 | 
			
		||||
    let key: TsigKey = parse_json::<PowerDnsTsigKey>(
 | 
			
		||||
        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()
 | 
			
		||||
        .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 {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								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<Out: DeserializeOwned>(res: ReqwestResponse) -> PowerDnsOidc
 | 
			
		||||
#[derive(Clone, Debug)]
 | 
			
		||||
pub struct AppState {
 | 
			
		||||
    http_client: Client,
 | 
			
		||||
    config: PowerDnsOidcTsigkeyConfig,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static config_cell: OnceCell<PowerDnsOidcTsigkeyConfig> = 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()
 | 
			
		||||
 
 | 
			
		||||
@@ -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<Url>,
 | 
			
		||||
    pub audience: Vec<String>,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user