♻️ refactor: extract gen_key pure function

main
_ 2021-04-09 19:30:45 -05:00
parent d9669a7073
commit 240cd8dff1
1 changed files with 9 additions and 3 deletions

View File

@ -48,12 +48,18 @@ pub struct ConfigFile {
pub file_server_root: Option <PathBuf>,
}
fn gen_key (path: &Path) -> anyhow::Result <()> {
/// Generates 64 bytes of entropy and returns it as Base64
fn gen_key () -> String {
use rand::RngCore;
let mut buffer = vec! [0_u8; 64];
rand::thread_rng ().fill_bytes (&mut buffer);
let api_key = base64::encode (&buffer);
base64::encode (&buffer)
}
fn gen_and_save_key (path: &Path) -> anyhow::Result <()> {
let api_key = gen_key ();
{
let mut f = File::create (path)?;
@ -89,7 +95,7 @@ async fn main () -> Result <(), anyhow::Error> {
let config_file: ConfigFile = match load_toml::load (&path) {
Err (ptth_server::errors::LoadTomlError::Io (_)) => if opt.auto_gen_key {
gen_key (&path)?;
gen_and_save_key (&path)?;
load_toml::load (&path)?
}