mirror of
https://github.com/ZeusWPI/ZNS.git
synced 2024-11-22 05:41:11 +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::prelude::*;
|
||||||
use diesel::sqlite::SqliteConnection;
|
use diesel::sqlite::SqliteConnection;
|
||||||
use std::env;
|
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
pub fn establish_connection() -> SqliteConnection {
|
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)
|
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 reqwest::Error;
|
||||||
|
|
||||||
use crate::errors::AuthenticationError;
|
use crate::{config::Config, errors::AuthenticationError};
|
||||||
|
|
||||||
use super::sig::{PublicKey, Sig};
|
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> {
|
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!(
|
||||||
Ok(
|
"{}/users/keys/{}",
|
||||||
reqwest::get(format!("{}/users/keys/{}", zauth_url, username))
|
Config::get().zauth_url,
|
||||||
.await?
|
username
|
||||||
.json::<SSHKeys>()
|
))
|
||||||
.await?,
|
.await?
|
||||||
)
|
.json::<SSHKeys>()
|
||||||
|
.await?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use std::{error::Error, net::SocketAddr};
|
use std::{error::Error, net::SocketAddr};
|
||||||
|
|
||||||
use dotenvy::dotenv;
|
use config::Config;
|
||||||
|
|
||||||
use crate::resolver::resolver_listener_loop;
|
use crate::resolver::resolver_listener_loop;
|
||||||
|
|
||||||
|
mod config;
|
||||||
mod db;
|
mod db;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
|
@ -16,8 +17,7 @@ mod utils;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
dotenv().ok();
|
Config::initialize();
|
||||||
|
|
||||||
let resolver_add = SocketAddr::from(([127, 0, 0, 1], 8080));
|
let resolver_add = SocketAddr::from(([127, 0, 0, 1], 8080));
|
||||||
let _ = tokio::join!(resolver_listener_loop(resolver_add),);
|
let _ = tokio::join!(resolver_listener_loop(resolver_add),);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue