mirror of
https://github.com/ZeusWPI/ZNS.git
synced 2024-10-29 21:14:27 +01:00
use OnceLock for global config state
This commit is contained in:
parent
95ea1a434f
commit
17b2fea0d3
4 changed files with 44 additions and 16 deletions
28
src/config.rs
Normal file
28
src/config.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use std::{env, sync::OnceLock};
|
||||
|
||||
use dotenvy::dotenv;
|
||||
|
||||
static CONFIG: OnceLock<Config> = OnceLock::new();
|
||||
|
||||
pub struct Config {
|
||||
pub zauth_url: String,
|
||||
pub db_uri: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn initialize() {
|
||||
assert!(CONFIG.get().is_none());
|
||||
|
||||
Config::get();
|
||||
}
|
||||
|
||||
pub fn get() -> &'static Config {
|
||||
CONFIG.get_or_init(|| {
|
||||
dotenv().ok();
|
||||
Config {
|
||||
db_uri: env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
|
||||
zauth_url: env::var("ZAUTH_URL").expect("ZAUTH_URL must be set"),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
use diesel::prelude::*;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use std::env;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
pub fn establish_connection() -> SqliteConnection {
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
let database_url = Config::get().db_uri.clone();
|
||||
SqliteConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", Config::get().db_uri))
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
use std::env;
|
||||
|
||||
use reqwest::Error;
|
||||
|
||||
use crate::errors::AuthenticationError;
|
||||
use crate::{config::Config, errors::AuthenticationError};
|
||||
|
||||
use super::sig::{PublicKey, Sig};
|
||||
|
||||
|
@ -38,11 +36,12 @@ pub(super) async fn authenticate(sig: &Sig, zone: &Vec<String>) -> Result<bool>
|
|||
}
|
||||
|
||||
async fn get_keys(username: &String) -> std::result::Result<SSHKeys, Error> {
|
||||
let zauth_url = env::var("ZAUTH_URL").expect("ZAUTH_URL must be set");
|
||||
Ok(
|
||||
reqwest::get(format!("{}/users/keys/{}", zauth_url, username))
|
||||
Ok(reqwest::get(format!(
|
||||
"{}/users/keys/{}",
|
||||
Config::get().zauth_url,
|
||||
username
|
||||
))
|
||||
.await?
|
||||
.json::<SSHKeys>()
|
||||
.await?,
|
||||
)
|
||||
.await?)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use std::{error::Error, net::SocketAddr};
|
||||
|
||||
use dotenvy::dotenv;
|
||||
use config::Config;
|
||||
|
||||
use crate::resolver::resolver_listener_loop;
|
||||
|
||||
mod config;
|
||||
mod db;
|
||||
mod errors;
|
||||
mod handlers;
|
||||
|
@ -16,8 +17,7 @@ mod utils;
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
dotenv().ok();
|
||||
|
||||
Config::initialize();
|
||||
let resolver_add = SocketAddr::from(([127, 0, 0, 1], 8080));
|
||||
let _ = tokio::join!(resolver_listener_loop(resolver_add),);
|
||||
Ok(())
|
||||
|
|
Loading…
Reference in a new issue