Skip to content

Commit

Permalink
Structural tuning and server binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Stridsvagn69420 committed Dec 4, 2022
1 parent e89b77f commit b49ab10
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 223 deletions.
191 changes: 82 additions & 109 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ documentation = "https://docs.rs/cyrkensia"
authors = ["Stridsvagn69420"]
categories = []
keywords = []
default-run = "cyrkensia"

[dependencies]
# Main crate
serde = { version = "1", features = ["derive"] }
serde_json = "1"
uuid = { version = "1.2", features = ["v4", "serde"] }
dirs = "4"
kagero = { version = "0.4", default-features = false, features = ["printer"] }

# Server only
actix-web = { version = "4", optional = true, features = ["rustls"] }
actix-web = { version = "4", optional = true }
actix-cors = { version = "0.6", optional = true }
blake3 = { version = "1.3", optional = true }

Expand All @@ -30,9 +33,9 @@ argon2 = { version = "0.4", optional = true, features = ["std", "password-hash"]
rand = { version = "0.8", optional = true, features = ["std"] }

[features]
default = ["server"]
default = ["server", "accounts"]
accounts = ["argon2", "rand"]
server = ["actix-web", "actix-cors", "blake3", "accounts"]
server = ["actix-web", "actix-cors", "blake3"]

[build-dependencies]
chrono = "0.4"
Expand Down
26 changes: 24 additions & 2 deletions src/account.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::fmt::Display;
use std::path::Path;
use std::{io, fs};
use std::{io, fs, env};
use dirs::home_dir;
use serde::{Deserialize, Serialize};
use serde_json::from_str;
use argon2::password_hash::rand_core::OsRng;
use argon2::password_hash::{SaltString, Result};
use argon2::{Argon2, PasswordHasher, PasswordVerifier, PasswordHash};
use super::meta;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Account {
Expand Down Expand Up @@ -55,6 +57,26 @@ impl Account {
let data = fs::read_to_string(file)?;
Ok(from_str(data.as_str())?)
}

/// Cascade Loading
///
/// Attempts to load a user file by:
/// 1. First command-line argument
/// 2. `CYRKENSIA_USERS` environment variable
/// 3. `~/.config/cyrkensia/users.json` file
pub fn load_cascade(cmdarg: Option<&String>, pathcfg: Option<&String>) -> io::Result<Vec<Account>> {
// Select extra path
let envvar = env::var(meta::USERS_ENVVAR);

// Read users from extra location
if let Some(path) = cmdarg.or_else(|| envvar.as_ref().ok()).or(pathcfg) {
return Account::load(path);
}

// Read with default path
let localpath = home_dir().unwrap_or_default().join(meta::USERS_PATH);
Account::load(localpath)
}
}

impl Display for Account {
Expand Down Expand Up @@ -89,4 +111,4 @@ macro_rules! get_argon2 {
Argon2::default()
};
}
pub use get_argon2;
pub use get_argon2;
Loading

0 comments on commit b49ab10

Please sign in to comment.